summaryrefslogtreecommitdiff
path: root/themes/src/Logger.cpp
blob: 1068d881b1e818363d393c89606ba53275dba74c (plain)
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
#include "Logger.h"
#include <chrono>
#include <cstdarg>
#include <cstdio>

namespace Logger {
    LogLevel gLogLevel = LogLevel_Debug;
    FILE* gFilePointer = NULL;

    void initialize(LoggerOptions options) {
        setLevel(options.level);
        if (options.logToFile) {
#ifdef WIN32
            fopen_s(&gFilePointer, options.filePath, "a");
#else
            gFilePointer = fopen(options.filePath, "a");
#endif
        }
    }

    void setLevel(LogLevel level) {
        gLogLevel = level;
    }

    LogLevel getLevel() {
        return gLogLevel;
    }

    void printHeader(const char* levelStr, const char* fileName, int lineNumber) {
        time_t t = time(0);
        tm now;
#ifdef WIN32
        localtime_s(&now, &t);
#else
        now = *localtime(&t);
#endif

        printf("%s:%d [%d-%d-%d %d:%d:%d] %s: ", fileName, lineNumber, (now.tm_year + 1900), (now.tm_mon + 1), now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, levelStr);
        if (gFilePointer != NULL) {
            fprintf(gFilePointer, "[%d-%d-%d %d:%d:%d] %s: ", (now.tm_year + 1900), (now.tm_mon + 1), now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, levelStr);
        }
    }

    void logInternal(const char* file, int lineNumber,LogLevel level, const char* format, va_list args) {
        if (level < gLogLevel) {
            return;
        }

	
        const char* levelStr;
        switch (level) {
        case LogLevel_Debug:
            levelStr = "Debug";
            break;
        case LogLevel_Info:
            levelStr = "Info";
            break;
        case LogLevel_Warn:
            levelStr = "Warning";
            break;
        case LogLevel_Error:
            levelStr = "Error";
            break;
        default:
            levelStr = "Unknown";
            break;
        }

	if (gFilePointer != NULL) {
	    va_list fileArgs;
	    va_copy(fileArgs, args);
	    vfprintf(gFilePointer, format, fileArgs);
	    fprintf(gFilePointer, "\n");
	}

        printHeader(levelStr, file, lineNumber);

        vprintf(format, args);
        printf("\n");
    }

    void doLog(const char* file, int lineNumber,LogLevel level, const char* format, ...) {
        va_list args;
        va_start(args, format);
        logInternal(file, lineNumber, level, format, args);
        va_end(args);
    }

    void doDebug(const char* file, int lineNumber,const char* format, ...) {
        va_list args;
        va_start(args, format);
        logInternal(file, lineNumber, LogLevel_Debug, format, args);
        va_end(args);
    }

    void doInfo(const char* file, int lineNumber,const char* format, ...) {
        va_list args;
        va_start(args, format);
        logInternal(file, lineNumber, LogLevel_Info, format, args);
        va_end(args);
    }

    void doWarning(const char* file, int lineNumber,const char* format, ...) {
        va_list args;
        va_start(args, format);
        logInternal(file, lineNumber, LogLevel_Warn, format, args);
        va_end(args);
    }

    void doError(const char* file, int lineNumber,const char* format, ...) {
        va_list args;
        va_start(args, format);
        logInternal(file, lineNumber, LogLevel_Error, format, args);
        va_end(args);
    }

    void free() {
        if (gFilePointer) {
            fclose(gFilePointer);
	    gFilePointer = NULL;
        }
    }
}