-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logger.cpp
152 lines (121 loc) · 3.58 KB
/
Logger.cpp
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
#include "Logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static const size_t TIME_BUFFER_SIZE = 80;
static const size_t LINE_BUFFER_SIZE = 320;
LogLevel::Level Logger::currentLevel = LogLevel::Debug;
wchar_t* wlevels [] = { L"Info", L"Debug", L"Error", L"Critical" };
char* levels [] = { "Info", "Debug", "Error", "Critical" };
// Ugly, but faster than returning a std::string
static void now(char* buffer) {
time_t rawtime;
struct tm timeinfo;
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
strftime(buffer, TIME_BUFFER_SIZE, "%d-%m-%Y %I:%M:%S", &timeinfo);
}
static void now(wchar_t* buffer) {
time_t rawtime;
struct tm timeinfo;
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
wcsftime(buffer, TIME_BUFFER_SIZE, L"%d-%m-%Y %I:%M:%S", &timeinfo);
}
static void log(const char* level, const char* message) {
char time_buffer[TIME_BUFFER_SIZE];
now(time_buffer);
fprintf(stderr, "%s - [%s] %s\n", time_buffer, level, message);
}
static void log(const wchar_t* level, const wchar_t* message) {
wchar_t time_buffer[TIME_BUFFER_SIZE];
now(time_buffer);
fwprintf(stderr, L"%s - [%s] %s\n", time_buffer, level, message);
fflush(stderr);
}
void Logger::Log(int level, const char* format, va_list ap) {
if (currentLevel > level)
return;
static char line_buffer[LINE_BUFFER_SIZE];
char* buffer;
int len = _vscprintf(format, ap) + 1; // _vscprintf doesn't count terminating '\0'
// If our static buffer (faster) is not long enough, allocate a bigger one
if (len > LINE_BUFFER_SIZE) {
buffer = (char*) malloc(len * sizeof(char));
}
else {
buffer = line_buffer;
}
vsprintf_s(buffer, len, format, ap);
log(levels[level], line_buffer);
// If we allocated it, free it
if (buffer != line_buffer)
free(buffer);
}
void Logger::Log(int level, const wchar_t* format, va_list ap) {
if (currentLevel > level)
return;
static wchar_t line_buffer[LINE_BUFFER_SIZE];
wchar_t* buffer;
int len = _vscwprintf(format, ap) + 1; // _vscprintf doesn't count terminating '\0'
// If our static buffer (faster) is not long enough, allocate a bigger one
if (len > LINE_BUFFER_SIZE) {
buffer = (wchar_t*) malloc(len * sizeof(wchar_t));
}
else {
buffer = line_buffer;
}
vswprintf_s(buffer, len, format, ap);
log(wlevels[level], line_buffer);
// If we allocated it, free it
if (buffer != line_buffer)
free(buffer);
}
void Logger::Info(const char* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Info, format, ap);
va_end(ap);
}
void Logger::Info(const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Info, format, ap);
va_end(ap);
}
void Logger::Debug(const char* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Debug, format, ap);
va_end(ap);
}
void Logger::Debug(const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Debug, format, ap);
va_end(ap);
}
void Logger::Error(const char* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Error, format, ap);
va_end(ap);
}
void Logger::Error(const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Error, format, ap);
va_end(ap);
}
void Logger::Critical(const char* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Critical, format, ap);
va_end(ap);
}
void Logger::Critical(const wchar_t* format, ...) {
va_list ap;
va_start(ap, format);
Log(LogLevel::Critical, format, ap);
va_end(ap);
}