log.h 2.41 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
#ifndef LIGHTGBM_UTILS_LOG_H_
#define LIGHTGBM_UTILS_LOG_H_

#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cstring>
8
9
#include <exception>
#include <stdexcept>
Guolin Ke's avatar
Guolin Ke committed
10
11
12
13

namespace LightGBM {


Qiwei Ye's avatar
Qiwei Ye committed
14
15
16
17
#ifndef CHECK
#define CHECK(condition)                                   \
  if (!(condition)) Log::Fatal("Check failed: " #condition \
     " at %s, line %d .\n", __FILE__,  __LINE__);
Guolin Ke's avatar
Guolin Ke committed
18
#endif
Qiwei Ye's avatar
Qiwei Ye committed
19
20
21

#ifndef CHECK_NOTNULL
#define CHECK_NOTNULL(pointer)                             \
Guolin Ke's avatar
Guolin Ke committed
22
  if ((pointer) == nullptr) LightGBM::Log::Fatal(#pointer " Can't be NULL at %s, line %d .\n", __FILE__,  __LINE__);
Guolin Ke's avatar
Guolin Ke committed
23
#endif
Qiwei Ye's avatar
Qiwei Ye committed
24

Guolin Ke's avatar
Guolin Ke committed
25

Guolin Ke's avatar
Guolin Ke committed
26
27
enum class LogLevel: int {
  Fatal = -1,
Qiwei Ye's avatar
Qiwei Ye committed
28
  Warning = 0,
Qiwei Ye's avatar
Qiwei Ye committed
29
  Info = 1,
Guolin Ke's avatar
Guolin Ke committed
30
  Debug = 2,
Guolin Ke's avatar
Guolin Ke committed
31
32
};

Qiwei Ye's avatar
Qiwei Ye committed
33
34

/*!
Guolin Ke's avatar
Guolin Ke committed
35
* \brief A static Log class 
Qiwei Ye's avatar
Qiwei Ye committed
36
37
38
39
40
41
42
*/
class Log {
public:
  /*!
  * \brief Resets the minimal log level. It is INFO by default.
  * \param level The new minimal log level.
  */
Guolin Ke's avatar
Guolin Ke committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  static void ResetLogLevel(LogLevel level) {
    GetLevel() = level;
  }

  static void Debug(const char *format, ...) {
    va_list val;
    va_start(val, format);
    Write(LogLevel::Debug, "Debug", format, val);
    va_end(val);
  }
  static void Info(const char *format, ...) {
    va_list val;
    va_start(val, format);
    Write(LogLevel::Info, "Info", format, val);
    va_end(val);
  }
Qiwei Ye's avatar
Qiwei Ye committed
59
  static void Warning(const char *format, ...) {
Guolin Ke's avatar
Guolin Ke committed
60
61
    va_list val;
    va_start(val, format);
Qiwei Ye's avatar
Qiwei Ye committed
62
    Write(LogLevel::Warning, "Warning", format, val);
Guolin Ke's avatar
Guolin Ke committed
63
64
65
66
    va_end(val);
  }
  static void Fatal(const char *format, ...) {
    va_list val;
67
    char str_buf[1024];
Guolin Ke's avatar
Guolin Ke committed
68
    va_start(val, format);
69
70
71
72
73
#ifdef _MSC_VER
    vsprintf_s(str_buf, format, val);
#else
    vsprintf(str_buf, format, val);
#endif
Guolin Ke's avatar
Guolin Ke committed
74
    va_end(val);
75
    throw std::runtime_error(std::string(str_buf));
Guolin Ke's avatar
Guolin Ke committed
76
  }
Qiwei Ye's avatar
Qiwei Ye committed
77
78

private:
Guolin Ke's avatar
Guolin Ke committed
79
80
81
82
83
84

  static void Write(LogLevel level, const char* level_str, const char *format, va_list val) {
    if (level <= GetLevel()) {  // omit the message with low level
      // write to STDOUT
      printf("[LightGBM] [%s] ", level_str);
      vprintf(format, val);
85
      printf("\n");
Guolin Ke's avatar
Guolin Ke committed
86
87
88
89
90
91
      fflush(stdout);
    }
  }

  // a trick to use static variable in header file. 
  // May be not good, but avoid to use an additional cpp file
Guolin Ke's avatar
Guolin Ke committed
92
93
94
#if defined(_MSC_VER)
  static LogLevel& GetLevel() { static __declspec(thread) LogLevel level = LogLevel::Info; return level; }
#else
Guolin Ke's avatar
Guolin Ke committed
95
  static LogLevel& GetLevel() { static thread_local LogLevel level = LogLevel::Info; return level; }
Guolin Ke's avatar
Guolin Ke committed
96
#endif
xuehui's avatar
xuehui committed
97
  
Qiwei Ye's avatar
Qiwei Ye committed
98
};
Guolin Ke's avatar
Guolin Ke committed
99
100

}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
101
#endif   // LightGBM_UTILS_LOG_H_