Skip to content

Commit

Permalink
When version mismatch happens, show versions (#325)
Browse files Browse the repository at this point in the history
* When version mismatch happens, show versions

* Add a version query function

* Add global string constant TREELITE_VERSION
  • Loading branch information
hcho3 authored Dec 20, 2021
1 parent 2994be4 commit 5d80909
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/treelite/c_api_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ TREELITE_DLL const char* TreeliteGetLastError(void);
*/
TREELITE_DLL int TreeliteRegisterLogCallback(void (*callback)(const char*));

/*!
* \brief Get the version string for the Treelite library.
* \return version string, of form MAJOR.MINOR.PATCH
*/
TREELITE_DLL const char* TreeliteQueryTreeliteVersion(void);

extern "C" {
extern const char* TREELITE_VERSION;
}

/*!
* \defgroup dmatrix
* Data matrix interface
Expand Down
8 changes: 7 additions & 1 deletion include/treelite/tree_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,13 @@ Model::DeserializeTemplate(HeaderPrimitiveFieldHandlerFunc header_primitive_fiel
header_primitive_field_handler(&minor_ver);
header_primitive_field_handler(&patch_ver);
if (major_ver != TREELITE_VER_MAJOR || minor_ver != TREELITE_VER_MINOR) {
throw std::runtime_error("Cannot deserialize model from a different version of Treelite");
std::ostringstream oss;
oss << "Cannot deserialize model from a different version of Treelite." << std::endl
<< "Currently running Treelite version " << TREELITE_VER_MAJOR << "."
<< TREELITE_VER_MINOR << "." << TREELITE_VER_PATCH << std::endl
<< "The model checkpoint was generated from Treelite version " << major_ver << "."
<< minor_ver << "." << patch_ver;
throw std::runtime_error(oss.str());
}
header_primitive_field_handler(&threshold_type);
header_primitive_field_handler(&leaf_output_type);
Expand Down
18 changes: 18 additions & 0 deletions src/c_api/c_api_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
*/
#include <treelite/thread_local.h>
#include <treelite/c_api_error.h>
#include <treelite/version.h>
#include <string>
#include <sstream>

// Stringify an integer macro constant
#define STR_IMPL_(x) #x
#define STR(x) STR_IMPL_(x)

namespace {

struct TreeliteAPIErrorEntry {
std::string last_error;
std::string version_str;
};

using TreeliteAPIErrorStore = treelite::ThreadLocalStore<TreeliteAPIErrorEntry>;
Expand All @@ -25,3 +32,14 @@ const char* TreeliteGetLastError() {
void TreeliteAPISetLastError(const char* msg) {
TreeliteAPIErrorStore::Get()->last_error = msg;
}

const char* TreeliteQueryTreeliteVersion() {
std::ostringstream oss;
oss << TREELITE_VER_MAJOR << "." << TREELITE_VER_MINOR << "." << TREELITE_VER_PATCH;
std::string& version_str = TreeliteAPIErrorStore::Get()->version_str;
version_str = oss.str();
return version_str.c_str();
}

const char* TREELITE_VERSION = "TREELITE_VERSION_" STR(TREELITE_VER_MAJOR) "."
STR(TREELITE_VER_MINOR) "." STR(TREELITE_VER_PATCH);

0 comments on commit 5d80909

Please sign in to comment.