log.h 2.47 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
#ifndef LIGHTGBM_UTILS_LOG_H_
#define LIGHTGBM_UTILS_LOG_H_

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

namespace LightGBM {

14
15
16
17
18
#if defined(_MSC_VER)
#define THREAD_LOCAL __declspec(thread) 
#else
#define THREAD_LOCAL thread_local
#endif
Guolin Ke's avatar
Guolin Ke committed
19

Qiwei Ye's avatar
Qiwei Ye committed
20
21
22
23
#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
24
#endif
Qiwei Ye's avatar
Qiwei Ye committed
25
26
27

#ifndef CHECK_NOTNULL
#define CHECK_NOTNULL(pointer)                             \
Guolin Ke's avatar
Guolin Ke committed
28
  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
29
#endif
Qiwei Ye's avatar
Qiwei Ye committed
30

Guolin Ke's avatar
Guolin Ke committed
31

Guolin Ke's avatar
Guolin Ke committed
32
33
enum class LogLevel: int {
  Fatal = -1,
Qiwei Ye's avatar
Qiwei Ye committed
34
  Warning = 0,
Qiwei Ye's avatar
Qiwei Ye committed
35
  Info = 1,
Guolin Ke's avatar
Guolin Ke committed
36
  Debug = 2,
Guolin Ke's avatar
Guolin Ke committed
37
38
};

Qiwei Ye's avatar
Qiwei Ye committed
39
40

/*!
41
* \brief A static Log class
Qiwei Ye's avatar
Qiwei Ye committed
42
43
44
45
46
47
48
*/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  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
65
  static void Warning(const char *format, ...) {
Guolin Ke's avatar
Guolin Ke committed
66
67
    va_list val;
    va_start(val, format);
Qiwei Ye's avatar
Qiwei Ye committed
68
    Write(LogLevel::Warning, "Warning", format, val);
Guolin Ke's avatar
Guolin Ke committed
69
70
71
72
    va_end(val);
  }
  static void Fatal(const char *format, ...) {
    va_list val;
73
    char str_buf[1024];
Guolin Ke's avatar
Guolin Ke committed
74
    va_start(val, format);
75
76
77
78
79
#ifdef _MSC_VER
    vsprintf_s(str_buf, format, val);
#else
    vsprintf(str_buf, format, val);
#endif
Guolin Ke's avatar
Guolin Ke committed
80
    va_end(val);
Guolin Ke's avatar
Guolin Ke committed
81
82
    fprintf(stderr, "[LightGBM] [Fatal] %s\n", str_buf);
    fflush(stderr);
83
    throw std::runtime_error(std::string(str_buf));
Guolin Ke's avatar
Guolin Ke committed
84
  }
Qiwei Ye's avatar
Qiwei Ye committed
85
86

private:
Guolin Ke's avatar
Guolin Ke committed
87
88
89
90
91
92

  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);
93
      printf("\n");
Guolin Ke's avatar
Guolin Ke committed
94
95
96
97
      fflush(stdout);
    }
  }

98
  // a trick to use static variable in header file.
Guolin Ke's avatar
Guolin Ke committed
99
  // May be not good, but avoid to use an additional cpp file
100
  static LogLevel& GetLevel() { static THREAD_LOCAL LogLevel level = LogLevel::Info; return level; }
101

Qiwei Ye's avatar
Qiwei Ye committed
102
};
Guolin Ke's avatar
Guolin Ke committed
103
104

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