log.h 2.35 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>
Qiwei Ye's avatar
Qiwei Ye committed
8
#include <fstream>
Guolin Ke's avatar
Guolin Ke committed
9
10
11
12

namespace LightGBM {


Qiwei Ye's avatar
Qiwei Ye committed
13
14
15
16
#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
17
#endif
Qiwei Ye's avatar
Qiwei Ye committed
18
19
20
21

#ifndef CHECK_NOTNULL
#define CHECK_NOTNULL(pointer)                             \
  if ((pointer) == nullptr) LightGBM::Log::Fatal(#pointer " Can't be NULL\n");
Guolin Ke's avatar
Guolin Ke committed
22
#endif
Qiwei Ye's avatar
Qiwei Ye committed
23
24
25

// A enumeration type of log message levels. The values are ordered:
// Debug < Info < Error < Fatal.
Guolin Ke's avatar
Guolin Ke committed
26
27
28
enum class LogLevel: int {
  Fatal = -1,
  Error = 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
35
36
37
38
39
40
41
42
43
44

/*!
* \brief The Log class is a static wrapper of a global Logger instance in
*        the scope of a process. Users can write logging messages easily
*        with the static methods.
*/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  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);
  }
  static void Error(const char *format, ...) {
    va_list val;
    va_start(val, format);
    Write(LogLevel::Error, "Error", format, val);
    va_end(val);
  }
  static void Fatal(const char *format, ...) {
    va_list val;
    va_start(val, format);
    Write(LogLevel::Fatal, "Fatal", format, val);
    va_end(val);
  }
Qiwei Ye's avatar
Qiwei Ye committed
73
74

private:
Guolin Ke's avatar
Guolin Ke committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

  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);
      fflush(stdout);

      if (level == LogLevel::Fatal) {
        exit(1);
      }
    }
  }

  // a trick to use static variable in header file. 
  // May be not good, but avoid to use an additional cpp file
  static LogLevel& GetLevel() {
    static LogLevel level;
    return level;
  };

Qiwei Ye's avatar
Qiwei Ye committed
96
};
Guolin Ke's avatar
Guolin Ke committed
97
98

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