Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/treelite/c_api_error.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*!
* Copyright (c) 2017-2020 by Contributors
* Copyright (c) 2017-2021 by Contributors
* \file c_api_error.h
* \author Hyunsu Cho
* \brief Error handling for C API.
*/
#ifndef TREELITE_C_API_ERROR_H_
#define TREELITE_C_API_ERROR_H_

#include <dmlc/logging.h>
#include <treelite/c_api_common.h>
#include <stdexcept>

Expand Down
3 changes: 1 addition & 2 deletions include/treelite/frontend.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*!
* Copyright (c) 2017-2020 by Contributors
* Copyright (c) 2017-2021 by Contributors
* \file frontend.h
* \brief Collection of front-end methods to load or construct ensemble model
* \author Hyunsu Cho
*/
#ifndef TREELITE_FRONTEND_H_
#define TREELITE_FRONTEND_H_

#include <dmlc/logging.h>
#include <treelite/base.h>
#include <string>
#include <memory>
Expand Down
135 changes: 135 additions & 0 deletions include/treelite/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,144 @@

#include <treelite/thread_local.h>
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <memory>
#include <cstdio>
#include <ctime>

namespace treelite {

/*!
* \brief Exception class that will be thrown by Treelite
*/
struct Error : public std::runtime_error {
explicit Error(const std::string& s) : std::runtime_error(s) {}
};

template <typename X, typename Y>
std::unique_ptr<std::string> LogCheckFormat(const X& x, const Y& y) {
std::ostringstream os;
os << " (" << x << " vs. " << y << ") ";
/* CHECK_XX(x, y) requires x and y can be serialized to string. Use CHECK(x OP y) otherwise. */
return std::make_unique<std::string>(os.str());
}

#if defined(__GNUC__) || defined(__clang__)
#define TREELITE_ALWAYS_INLINE inline __attribute__((__always_inline__))
#elif defined(_MSC_VER)
#define TREELITE_ALWAYS_INLINE __forceinline
#else
#define TREELITE_ALWAYS_INLINE inline
#endif

#define DEFINE_CHECK_FUNC(name, op) \
template <typename X, typename Y> \
TREELITE_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name(const X& x, const Y& y) { \
if (x op y) return nullptr; \
return LogCheckFormat(x, y); \
} \
TREELITE_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name(int x, int y) { \
return LogCheck##name<int, int>(x, y); \
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
DEFINE_CHECK_FUNC(_LT, <)
DEFINE_CHECK_FUNC(_GT, >)
DEFINE_CHECK_FUNC(_LE, <=)
DEFINE_CHECK_FUNC(_GE, >=)
DEFINE_CHECK_FUNC(_EQ, ==)
DEFINE_CHECK_FUNC(_NE, !=)
#pragma GCC diagnostic pop


#define CHECK_BINARY_OP(name, op, x, y) \
if (auto __treelite__log__err = ::treelite::LogCheck##name(x, y)) \
::treelite::LogMessageFatal(__FILE__, __LINE__).stream() \
<< "Check failed: " << #x " " #op " " #y << *__treelite__log__err << ": "
#define CHECK(x) \
if (!(x)) \
::treelite::LogMessageFatal(__FILE__, __LINE__).stream() \
<< "Check failed: " #x << ": "
#define CHECK_LT(x, y) CHECK_BINARY_OP(_LT, <, x, y)
Comment thread
chyunsu3 marked this conversation as resolved.
Outdated
#define CHECK_GT(x, y) CHECK_BINARY_OP(_GT, >, x, y)
#define CHECK_LE(x, y) CHECK_BINARY_OP(_LE, <=, x, y)
#define CHECK_GE(x, y) CHECK_BINARY_OP(_GE, >=, x, y)
#define CHECK_EQ(x, y) CHECK_BINARY_OP(_EQ, ==, x, y)
#define CHECK_NE(x, y) CHECK_BINARY_OP(_NE, !=, x, y)

#define LOG_INFO ::treelite::LogMessage(__FILE__, __LINE__)
#define LOG_ERROR LOG_INFO
#define LOG_FATAL ::treelite::LogMessageFatal(__FILE__, __LINE__)
#define LOG(severity) LOG_##severity.stream()

class DateLogger {
public:
DateLogger() {
#if defined(_MSC_VER)
_tzset();
#endif // defined(_MSC_VER)
}
const char* HumanDate() {
#if defined(_MSC_VER)
_strtime_s(buffer_, sizeof(buffer_));
#else // defined(_MSC_VER)
time_t time_value = std::time(nullptr);
struct tm* pnow;
#if !defined(_WIN32)
struct tm now;
pnow = localtime_r(&time_value, &now);
#else // !defined(_WIN32)
pnow = std::localtime(&time_value); // NOLINT(*)
#endif // !defined(_WIN32)
std::snprintf(buffer_, sizeof(buffer_), "%02d:%02d:%02d",
Comment thread
chyunsu3 marked this conversation as resolved.
Outdated
pnow->tm_hour, pnow->tm_min, pnow->tm_sec);
#endif // defined(_MSC_VER)
return buffer_;
}

private:
char buffer_[9];
};

class LogMessageFatal {
public:
LogMessageFatal(const char* file, int line) {
log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":" << line << ": ";
}
LogMessageFatal(const LogMessageFatal&) = delete;
void operator=(const LogMessageFatal&) = delete;

std::ostringstream& stream() {
return log_stream_;
}
~LogMessageFatal() noexcept(false) {
throw Error(log_stream_.str());
}

private:
std::ostringstream log_stream_;
DateLogger pretty_date_;
};

class LogMessage {
public:
LogMessage(const char* file, int line) {
log_stream_ << "[" << DateLogger().HumanDate() << "] " << file << ":"
<< line << ": ";
}
~LogMessage() {
Log(log_stream_.str());
}
std::ostream& stream() { return log_stream_; }
static void Log(const std::string& msg);

private:
std::ostringstream log_stream_;
};

class LogCallbackRegistry {
public:
using Callback = void (*)(const char*);
Expand Down
4 changes: 2 additions & 2 deletions include/treelite/predictor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2017-2020 by Contributors
* Copyright (c) 2017-2021 by Contributors
* \file predictor.h
* \author Hyunsu Cho
* \brief Load prediction function exported as a shared library
Expand All @@ -8,7 +8,7 @@
#define TREELITE_PREDICTOR_H_

#include <dmlc/common.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <treelite/typeinfo.h>
#include <treelite/c_api_runtime.h>
#include <treelite/data.h>
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ foreach(lib objtreelite objtreelite_runtime objtreelite_common)
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>)
target_link_libraries(${lib} PUBLIC dmlc)
target_compile_definitions(${lib} PRIVATE -DDMLC_LOG_CUSTOMIZE)
if(MSVC)
target_compile_options(${lib} PRIVATE /MP)
target_compile_definitions(${lib} PRIVATE -DNOMINMAX)
Expand Down
1 change: 1 addition & 0 deletions src/annotator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* \brief Branch annotation tools
*/

#include <treelite/logging.h>
#include <treelite/annotator.h>
#include <treelite/math.h>
#include <treelite/omp.h>
Expand Down
1 change: 1 addition & 0 deletions src/c_api/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <treelite/tree.h>
#include <treelite/math.h>
#include <treelite/gtil.h>
#include <treelite/logging.h>
#include <dmlc/io.h>
#include <memory>
#include <algorithm>
Expand Down
1 change: 1 addition & 0 deletions src/compiler/ast/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* \brief Generate text representation of AST
*/
#include <dmlc/registry.h>
#include <treelite/logging.h>
#include "./builder.h"

namespace {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/ast/fold_code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* \author Hyunsu Cho
*/
#include <dmlc/registry.h>
#include <cmath>
#include <treelite/logging.h>
#include <limits>
#include <cmath>
#include "./builder.h"

namespace treelite {
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/ast/quantize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
#include <treelite/math.h>
#include <dmlc/registry.h>
#include <treelite/logging.h>
#include <set>
#include <cmath>
#include "./builder.h"

Expand Down
1 change: 1 addition & 0 deletions src/compiler/ast/split.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* \brief Split prediction subroutine into multiple translation units (files)
*/
#include <dmlc/registry.h>
#include <treelite/logging.h>
#include "./builder.h"

namespace treelite {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/common/code_folding_util.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*!
* Copyright (c) 2018-2020 by Contributors
* Copyright (c) 2018-2021 by Contributors
* \file code_folding_util.h
* \author Hyunsu Cho
* \brief Utilities for code folding
*/
#ifndef TREELITE_COMPILER_COMMON_CODE_FOLDING_UTIL_H_
#define TREELITE_COMPILER_COMMON_CODE_FOLDING_UTIL_H_

#include <dmlc/logging.h>
#include <fmt/format.h>
#include <treelite/logging.h>
#include <unordered_map>
#include <queue>
#include <set>
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
#include <treelite/compiler.h>
#include <treelite/compiler_param.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <rapidjson/document.h>
#include <limits>
#include "./ast_native.h"
Expand Down
1 change: 1 addition & 0 deletions src/compiler/elf/elf_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* \author Hyunsu Cho
* \brief Generate a relocatable object file containing a constant, read-only array
*/
#include <treelite/logging.h>
#include <dmlc/registry.h>
#include <fstream>
#include <iterator>
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/failsafe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <treelite/tree.h>
#include <treelite/compiler.h>
#include <treelite/compiler_param.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <fmt/format.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/native/pred_transform.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2017-2020 by Contributors
* Copyright (c) 2017-2021 by Contributors
* \file pred_transform.h
* \author Hyunsu Cho
* \brief template for pred_transform() function in generated C code
Expand All @@ -8,7 +8,7 @@
#ifndef TREELITE_COMPILER_NATIVE_PRED_TRANSFORM_H_
#define TREELITE_COMPILER_NATIVE_PRED_TRANSFORM_H_

#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <fmt/format.h>
#include <string>
#include "./typeinfo_ctypes.h"
Expand Down
4 changes: 2 additions & 2 deletions src/filesystem.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*!
* Copyright (c) 2020 by Contributors
* Copyright (c) 2020-2021 by Contributors
* \file filesystem.cc
* \author Hyunsu Cho
* \brief Cross-platform wrapper for common filesystem functions
*/

#include <treelite/filesystem.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <fstream>

#ifdef _WIN32
Expand Down
1 change: 1 addition & 0 deletions src/frontend/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <dmlc/registry.h>
#include <treelite/frontend.h>
#include <treelite/tree.h>
#include <treelite/logging.h>
#include <memory>
#include <queue>

Expand Down
1 change: 1 addition & 0 deletions src/frontend/lightgbm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <dmlc/data.h>
#include <treelite/logging.h>
#include <treelite/frontend.h>
#include <treelite/tree.h>
#include <unordered_map>
Expand Down
1 change: 1 addition & 0 deletions src/frontend/sklearn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* \brief Frontend for scikit-learn models
* \author Hyunsu Cho
*/
#include <treelite/logging.h>
#include <treelite/frontend.h>
#include <treelite/tree.h>
#include <memory>
Expand Down
1 change: 1 addition & 0 deletions src/frontend/xgboost.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "xgboost/xgboost.h"
#include <treelite/frontend.h>
#include <treelite/tree.h>
#include <treelite/logging.h>
#include <algorithm>
#include <memory>
#include <queue>
Expand Down
1 change: 1 addition & 0 deletions src/frontend/xgboost_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <treelite/tree.h>
#include <treelite/frontend.h>
#include <treelite/math.h>
#include <treelite/logging.h>

#include <algorithm>
#include <cstdio>
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/xgboost_util.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*!
* Copyright (c) 2020 by Contributors
* Copyright (c) 2020-2021 by Contributors
* \file xgboost_util.cc
* \brief Common utilities for XGBoost frontends
* \author Hyunsu Cho
*/

#include <treelite/tree.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <cstring>
#include "xgboost/xgboost.h"

Expand Down
3 changes: 1 addition & 2 deletions src/gtil/pred_transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
*/

#include "./pred_transform.h"
#include <treelite/gtil.h>
#include <treelite/tree.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <string>
#include <unordered_map>
#include <cmath>
Expand Down
2 changes: 1 addition & 1 deletion src/gtil/predict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <treelite/gtil.h>
#include <treelite/tree.h>
#include <treelite/data.h>
#include <dmlc/logging.h>
#include <treelite/logging.h>
#include <limits>
#include <vector>
#include <cmath>
Expand Down
Loading