log.h 2.14 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

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

Guolin Ke's avatar
Guolin Ke committed
24

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

Qiwei Ye's avatar
Qiwei Ye committed
32
33

/*!
Guolin Ke's avatar
Guolin Ke committed
34
* \brief A static Log class 
Qiwei Ye's avatar
Qiwei Ye committed
35
36
37
38
39
40
41
*/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  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);
67
68
69
70
    fprintf(stderr, "[LightGBM] [Fatel] ");
    vfprintf(stderr, format, val);
    fprintf(stderr, "\n");
    fflush(stderr);
Guolin Ke's avatar
Guolin Ke committed
71
    va_end(val);
72
    exit(1);
Guolin Ke's avatar
Guolin Ke committed
73
  }
Qiwei Ye's avatar
Qiwei Ye committed
74
75

private:
Guolin Ke's avatar
Guolin Ke committed
76
77
78
79
80
81

  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);
82
      printf("\n");
Guolin Ke's avatar
Guolin Ke committed
83
84
85
86
87
88
89
90
91
92
93
      fflush(stdout);
    }
  }

  // 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
94
};
Guolin Ke's avatar
Guolin Ke committed
95
96

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