log.cpp 4.95 KB
Newer Older
Qiwei Ye's avatar
Qiwei Ye committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "LightGBM/utils/log.h"

#include <time.h>
#include <stdarg.h>

#include <string>

namespace LightGBM {
    // Creates a Logger intance writing messages into STDOUT.
    Logger::Logger(LogLevel level) {
        level_ = level;
        file_ = nullptr;
        is_kill_fatal_ = true;
    }

    // Creates a Logger instance writing messages into both STDOUT and log file.
    Logger::Logger(std::string filename, LogLevel level) {
        level_ = level;
        file_ = nullptr;
        ResetLogFile(filename);
    }

    Logger::~Logger() {
        CloseLogFile();
    }

    int Logger::ResetLogFile(std::string filename) {
        CloseLogFile();
        if (filename.size() > 0) {  // try to open the log file if it is specified
#ifdef _MSC_VER
            fopen_s(&file_, filename.c_str(), "w");
#else
            file_ = fopen(filename.c_str(), "w");
#endif
            if (file_ == nullptr) {
                Error("Cannot create log file %s\n", filename.c_str());
                return -1;
            }
        }
        return 0;
    }

    void Logger::Write(LogLevel level, const char *format, ...) {
        va_list val;
        va_start(val, format);
        Write(level, format, val);
        va_end(val);
    }

    void Logger::Debug(const char *format, ...) {
        va_list val;
        va_start(val, format);
        Write(LogLevel::Debug, format, val);
        va_end(val);
    }

    void Logger::Info(const char *format, ...) {
        va_list val;
        va_start(val, format);
        Write(LogLevel::Info, format, val);
        va_end(val);
    }

    void Logger::Error(const char *format, ...) {
        va_list val;
        va_start(val, format);
        Write(LogLevel::Error, format, val);
        va_end(val);
    }

    void Logger::Fatal(const char *format, ...) {
        va_list val;
        va_start(val, format);
        Write(LogLevel::Fatal, format, val);
        va_end(val);
    }

    inline void Logger::Write(LogLevel level, const char *format, va_list* val) {
        if (level >= level_) {  // omit the message with low level
            std::string level_str = GetLevelStr(level);
            std::string time_str = GetSystemTime();
            va_list val_copy;
            va_copy(val_copy, *val);
            // write to STDOUT
            printf("[%s] [%s] ", level_str.c_str(), time_str.c_str());
            vprintf(format, *val);
            fflush(stdout);
            // write to log file
            if (file_ != nullptr) {
                fprintf(file_, "[%s] [%s] ", level_str.c_str(), time_str.c_str());
                vfprintf(file_, format, val_copy);
                fflush(file_);
            }
            va_end(val_copy);

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

    // Closes the log file if it it not null.
    void Logger::CloseLogFile() {
        if (file_ != nullptr) {
            fclose(file_);
            file_ = nullptr;
        }
    }

    std::string Logger::GetSystemTime() {
        time_t t = time(0);
        char str[64];
#ifdef _MSC_VER
        tm time;
        localtime_s(&time, &t);
        strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &time);
#else
        strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", localtime(&t));
#endif
        return str;
    }

    std::string Logger::GetLevelStr(LogLevel level) {
        switch (level) {
        case LogLevel::Debug: return "DEBUG";
        case LogLevel::Info: return "INFO";
        case LogLevel::Error: return "ERROR";
        case LogLevel::Fatal: return "FATAL";
        default: return "UNKNOW";
        }
    }
    //-- End of Logger rountine ----------------------------------------------/

    Logger Log::logger_;    // global (in process) static Logger instance

    int Log::ResetLogFile(std::string filename) {
        return logger_.ResetLogFile(filename);
    }

    void Log::ResetLogLevel(LogLevel level) {
        logger_.ResetLogLevel(level);
    }

    void Log::ResetKillFatal(bool is_kill_fatal) {
        logger_.ResetKillFatal(is_kill_fatal);
    }

    void Log::Write(LogLevel level, const char *format, ...) {
        va_list val;
        va_start(val, format);
        logger_.Write(level, format, &val);
        va_end(val);
    }

    void Log::Debug(const char *format, ...) {
        va_list val;
        va_start(val, format);
        logger_.Write(LogLevel::Debug, format, &val);
        va_end(val);
    }

    void Log::Info(const char *format, ...) {
        va_list val;
        va_start(val, format);
        logger_.Write(LogLevel::Info, format, &val);
        va_end(val);
    }

    void Log::Error(const char *format, ...) {
        va_list val;
        va_start(val, format);
        logger_.Write(LogLevel::Error, format, &val);
        va_end(val);
    }

    void Log::Fatal(const char *format, ...) {
        va_list val;
        va_start(val, format);
        logger_.Write(LogLevel::Fatal, format, &val);
        va_end(val);
    }

}  // namespace lightGBM