7 #ifndef TREELITE_LOGGING_H_
8 #define TREELITE_LOGGING_H_
23 template <
typename X,
typename Y>
25 std::ostringstream os;
26 os <<
" (" << x <<
" vs. " << y <<
") ";
28 return std::make_unique<std::string>(os.str());
31 #if defined(__GNUC__) || defined(__clang__)
32 #define TREELITE_ALWAYS_INLINE inline __attribute__((__always_inline__))
33 #elif defined(_MSC_VER)
34 #define TREELITE_ALWAYS_INLINE __forceinline
36 #define TREELITE_ALWAYS_INLINE inline
39 #define DEFINE_CHECK_FUNC(name, op) \
40 template <typename X, typename Y> \
41 TREELITE_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name(const X& x, const Y& y) { \
44 return LogCheckFormat(x, y); \
46 TREELITE_ALWAYS_INLINE std::unique_ptr<std::string> LogCheck##name(int x, int y) { \
47 return LogCheck##name<int, int>(x, y); \
51 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Wsign-compare"
63 #pragma GCC diagnostic pop
66 #define TREELITE_CHECK_BINARY_OP(name, op, x, y) \
67 if (auto __treelite__log__err = ::treelite::LogCheck##name(x, y)) \
68 ::treelite::LogMessageFatal(__FILE__, __LINE__).stream() \
69 << "Check failed: " << #x " " #op " " #y << *__treelite__log__err << ": "
70 #define TREELITE_CHECK(x) \
72 ::treelite::LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x << ": "
73 #define TREELITE_CHECK_LT(x, y) TREELITE_CHECK_BINARY_OP(_LT, <, x, y)
74 #define TREELITE_CHECK_GT(x, y) TREELITE_CHECK_BINARY_OP(_GT, >, x, y)
75 #define TREELITE_CHECK_LE(x, y) TREELITE_CHECK_BINARY_OP(_LE, <=, x, y)
76 #define TREELITE_CHECK_GE(x, y) TREELITE_CHECK_BINARY_OP(_GE, >=, x, y)
77 #define TREELITE_CHECK_EQ(x, y) TREELITE_CHECK_BINARY_OP(_EQ, ==, x, y)
78 #define TREELITE_CHECK_NE(x, y) TREELITE_CHECK_BINARY_OP(_NE, !=, x, y)
80 #define TREELITE_LOG_INFO ::treelite::LogMessage(__FILE__, __LINE__)
81 #define TREELITE_LOG_ERROR TREELITE_LOG_INFO
82 #define TREELITE_LOG_WARNING ::treelite::LogMessageWarning(__FILE__, __LINE__)
83 #define TREELITE_LOG_FATAL ::treelite::LogMessageFatal(__FILE__, __LINE__)
84 #define TREELITE_LOG(severity) TREELITE_LOG_##severity.stream()
95 _strtime_s(buffer_,
sizeof(buffer_));
97 time_t time_value = std::time(
nullptr);
101 pnow = localtime_r(&time_value, &now);
103 pnow = std::localtime(&time_value);
106 buffer_,
sizeof(buffer_),
"%02d:%02d:%02d", pnow->tm_hour, pnow->tm_min, pnow->tm_sec);
118 log_stream_ <<
"[" << pretty_date_.
HumanDate() <<
"] " << file <<
":" << line <<
": ";
127 throw Error(log_stream_.str());
131 std::ostringstream log_stream_;
141 Log(log_stream_.str());
146 static void Log(std::string
const& msg);
149 std::ostringstream log_stream_;
158 Log(log_stream_.str());
163 static void Log(std::string
const& msg);
166 std::ostringstream log_stream_;
173 : log_callback_info_([](char const* msg) { std::cerr << msg << std::endl; }),
174 log_callback_warn_([](
char const* msg) { std::cerr << msg << std::endl; }) {}
176 this->log_callback_info_ = log_callback;
179 return log_callback_info_;
182 this->log_callback_warn_ = log_callback;
185 return log_callback_warn_;
char const * HumanDate()
Definition: logging.h:93
DateLogger()
Definition: logging.h:88
Definition: logging.h:169
LogCallbackRegistry()
Definition: logging.h:172
void RegisterCallBackLogInfo(Callback log_callback)
Definition: logging.h:175
void RegisterCallBackLogWarning(Callback log_callback)
Definition: logging.h:181
Callback GetCallbackLogInfo() const
Definition: logging.h:178
void(*)(char const *) Callback
Definition: logging.h:171
Callback GetCallbackLogWarning() const
Definition: logging.h:184
Definition: logging.h:115
void operator=(LogMessageFatal const &)=delete
LogMessageFatal(char const *file, int line)
Definition: logging.h:117
~LogMessageFatal() noexcept(false)
Definition: logging.h:126
LogMessageFatal(LogMessageFatal const &)=delete
std::ostringstream & stream()
Definition: logging.h:123
Definition: logging.h:152
std::ostream & stream()
Definition: logging.h:160
LogMessageWarning(char const *file, int line)
Definition: logging.h:154
static void Log(std::string const &msg)
~LogMessageWarning()
Definition: logging.h:157
Definition: logging.h:135
LogMessage(char const *file, int line)
Definition: logging.h:137
~LogMessage()
Definition: logging.h:140
static void Log(std::string const &msg)
std::ostream & stream()
Definition: logging.h:143
A thread-local storage.
Definition: thread_local.h:17
Exception class used throughout the Treelite codebase.
#define DEFINE_CHECK_FUNC(name, op)
Definition: logging.h:39
Definition: contiguous_array.h:14
std::unique_ptr< std::string > LogCheckFormat(X const &x, Y const &y)
Definition: logging.h:24
Exception class that will be thrown by Treelite.
Definition: error.h:18
Helper class for thread-local storage.