Medial Code Documentation
Loading...
Searching...
No Matches
log.h
1#ifndef LIGHTGBM_UTILS_LOG_H_
2#define LIGHTGBM_UTILS_LOG_H_
3
4#include <iostream>
5#include <cstdio>
6#include <cstdlib>
7#include <cstdarg>
8#include <cstring>
9#include <exception>
10#include <stdexcept>
11
12namespace LightGBM {
13
14#if defined(_MSC_VER)
15#define THREAD_LOCAL __declspec(thread)
16#else
17#define THREAD_LOCAL thread_local
18#endif
19
20#ifndef CHECK
21#define CHECK(condition) \
22 if (!(condition)) Log::Fatal("Check failed: " #condition \
23 " at %s, line %d .\n", __FILE__, __LINE__);
24#endif
25
26#ifndef CHECK_NOTNULL
27#define CHECK_NOTNULL(pointer) \
28 if ((pointer) == nullptr) LightGBM::Log::Fatal(#pointer " Can't be NULL at %s, line %d .\n", __FILE__, __LINE__);
29#endif
30
31
32enum class LogLevel: int {
33 Fatal = -1,
34 Warning = 0,
35 Info = 1,
36 Debug = 2,
37};
38
39
43class Log {
44public:
49 static void ResetLogLevel(LogLevel level) {
50 GetLevel() = level;
51 }
52
53 static void Debug(const char *format, ...) {
54 va_list val;
55 va_start(val, format);
56 Write(LogLevel::Debug, "Debug", format, val);
57 va_end(val);
58 }
59 static void Info(const char *format, ...) {
60 va_list val;
61 va_start(val, format);
62 Write(LogLevel::Info, "Info", format, val);
63 va_end(val);
64 }
65 static void Warning(const char *format, ...) {
66 va_list val;
67 va_start(val, format);
68 Write(LogLevel::Warning, "Warning", format, val);
69 va_end(val);
70 }
71 static void Fatal(const char *format, ...) {
72 va_list val;
73 char str_buf[1024];
74 va_start(val, format);
75#ifdef _MSC_VER
76 vsprintf_s(str_buf, format, val);
77#else
78 vsprintf(str_buf, format, val);
79#endif
80 va_end(val);
81 fprintf(stderr, "[LightGBM] [Fatal] %s\n", str_buf);
82 fflush(stderr);
83 throw std::runtime_error(std::string(str_buf));
84 }
85
86private:
87 static void Write(LogLevel level, const char* level_str, const char *format, va_list val) {
88 if (level <= GetLevel()) { // omit the message with low level
89 // write to STDOUT
90 printf("[LightGBM] [%s] ", level_str);
91 vprintf(format, val);
92 printf("\n");
93 fflush(stdout);
94 }
95 }
96
97 // a trick to use static variable in header file.
98 // May be not good, but avoid to use an additional cpp file
99 static LogLevel& GetLevel() { static THREAD_LOCAL LogLevel level = LogLevel::Info; return level; }
100};
101
102} // namespace LightGBM
103#endif // LightGBM_UTILS_LOG_H_
A static Log class.
Definition log.h:43
static void ResetLogLevel(LogLevel level)
Resets the minimal log level. It is INFO by default.
Definition log.h:49
desc and descl2 fields must be written in reStructuredText format
Definition application.h:10