-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cpp
238 lines (216 loc) · 5.91 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "Logger.h"
#include <ctime>
#include <iostream>
#include <filesystem>
#include <cstdarg>
#include <sstream>
#include <string>
#ifdef WIN32
#include <Windows.h>
#endif
using namespace std;
/* ----------------------------------------------------------------------------------------------------------
* Log Manager
* ----------------------------------------------------------------------------------------------------------
*/
LogManager LogManager::m_instance = LogManager();
LogManager::LogManager() :
m_logLevel(LogLevel::LL_DEBUG)
{
}
LogManager::~LogManager()
{
}
std::unique_ptr<Logger> LogManager::getLogger(const std::string& name)
{
auto logger = std::unique_ptr<Logger>(new Logger(name));
return logger;
}
LogManager& LogManager::Instance()
{
return m_instance;
}
void LogManager::setLevel(LogLevel level)
{
m_logLevel = level;
}
void LogManager::open(const std::string& name)
{
close();
try
{
std::filesystem::path logpath = std::filesystem::path(name).make_preferred(); // make_preferred cleanup slash and backslash
for (const auto& entry : std::filesystem::directory_iterator(logpath.parent_path()))
{
if (entry.path().extension() == ".log")
{
if (!std::filesystem::remove(entry.path()))
{
std::string msg = "Unable to remove: ";
msg += entry.path().string();
writeLog("LogManager", LogLevel::LL_ERROR, msg);
}
else
{
std::string msg = "Old log removed ";
msg += entry.path().string();
writeLog("LogManager", LogLevel::LL_INFO, msg);
}
}
}
}
catch (std::exception e)
{
std::string msg = "Unexpected error flushing old logs: ";
msg += e.what();
writeLog("LogManager", LogLevel::LL_ERROR, msg);
}
const string date = currentLogDate("%4.4d-%2.2d-%2.2d %2.2d-%2.2d-%2.2d");
string filename = name;
filename += "-";
filename += date;
filename += ".log";
m_logFile.open(filename);
}
void LogManager::flush()
{
m_logFile.flush();
}
void LogManager::close()
{
if (m_logFile.is_open())
{
m_logFile.close();
}
}
// convert a std::thread::id to something usable
uint64_t LogManager::getThreadId(const std::thread::id& id) {
stringstream ss;
ss << id;
string sid = ss.str();
try
{
return stoull(sid);
}
catch (std::invalid_argument ex)
{
return -1; // this happen when the thread id does not represent a running thread
}
}
bool LogManager::isLoggable(LogLevel level)
{
return (level <= m_logLevel);
}
/*
* send the log entry to the console and to a file
*/
void LogManager::writeLog(const string name, const LogLevel level, const string& message)
{
if (!isLoggable(level))
return;
const char* logFormat = "[%s] [%-8.8s] [%8.8llX] [%-8.8s] %s\n";
const string date = currentLogDate("%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d");
string strLevel = "DEBUG";
if (level == LogLevel::LL_ERROR)
strLevel = "ERROR";
if (level == LogLevel::LL_WARNING)
strLevel = "WARNING";
if (level == LogLevel::LL_INFO)
strLevel = "INFO";
uint64_t threadId = (unsigned long int)getThreadId(this_thread::get_id());
int finalSize = snprintf(NULL, 0, logFormat, date.c_str(), name.c_str(), threadId, strLevel.c_str(), message.c_str()) + 1;
char* logBuffer = new char[finalSize];
snprintf(logBuffer, finalSize, logFormat, date.c_str(), name.c_str(), threadId, strLevel.c_str(), message.c_str());
cout << logBuffer;
if (m_logFile.is_open())
{
m_logFile << logBuffer;
m_logFile.flush();
}
#ifdef WIN32
::OutputDebugStringA(logBuffer);
#endif
delete[] logBuffer;
logBuffer = nullptr;
}
/*
* the format must accept 6 values: "%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d"
* use %n to skip values
*/
const std::string LogManager::currentLogDate(const std::string& format)
{
time_t t = time(NULL);
struct tm tm;
#ifdef WIN32
gmtime_s(&tm, &t);
#else
gmtime_r(&t, &tm);
#endif
const int year = tm.tm_year + 1900;
const int month = tm.tm_mon + 1;
const int size = snprintf(NULL, 0, format.c_str(), year, month, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec) + 1;
char* buffer = new char[size];
snprintf(buffer, size, format.c_str(), year, month, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
std::string result = buffer;
delete[] buffer;
buffer = nullptr;
return result;
}
/* ----------------------------------------------------------------------------------------------------------
* Logger
* ----------------------------------------------------------------------------------------------------------
*/
// terminate the variadic template recursion
void Logger::log(LogLevel level, const char* format) const
{
if (!LogManager::Instance().isLoggable(level))
return;
LogManager::Instance().writeLog(m_name, level, this->format(format));
}
void Logger::warn(const char* format) const
{
log(LogLevel::LL_WARNING, format);
}
void Logger::info(const char* format) const
{
log(LogLevel::LL_INFO, format);
}
void Logger::error(const char* format) const
{
log(LogLevel::LL_ERROR, format);
}
void Logger::debug(const char* format) const
{
log(LogLevel::LL_DEBUG, format);
}
/*
* Format methods for each single parameters
*/
std::string Logger::format(const char* fmt, std::string& value) const
{
return format(fmt, value.c_str());
}
std::string Logger::format(const char* fmt, std::thread::id& value) const
{
return format(fmt, LogManager::Instance().getThreadId(value));
}
// NOTE: despite the "..." we have always one single parameter after fmt here.
std::string Logger::format(const char* fmt, ...) const
{
va_list args;
va_start(args, fmt);
const int size = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args);
char* buffer = new char[size];
va_start(args, fmt);
vsnprintf(buffer, size, fmt, args);
va_end(args);
std::string result = buffer;
delete[] buffer;
buffer = nullptr;
return result;
}
Logger::Logger(const std::string& name) :
m_name(name)
{
}