-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cpp
355 lines (322 loc) · 8.58 KB
/
Log.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "Log.h"
#include <ctime>
#include <iostream>
/**
* @brief Gets the name of the log category from the enum value
*
* @param The enum value of the category
* @return The name of the category; returns the word UNKNOWN if not valid.
*/
const char* Log::TypeToString(const Type& type) {
switch (type) {
case LOG_TYPE_FATAL:
return "FATAL";
case LOG_TYPE_ERROR:
return "ERROR";
case LOG_TYPE_WARN:
return "WARN ";
case LOG_TYPE_INFO:
return "INFO ";
case LOG_TYPE_DEBUG:
return "DEBUG";
default:
break;
}
return "UNKWN";
}
/**
* @brief Initialises the file stream
*
* @param fileName The location of the file to create/append to
* @return True if the file was successfully initialised; false if already initialised
*/
bool Log::Initialise(const std::string& fileName) {
Log& log = Log::get();
if (!log.m_initialised) {
log.m_fileName = fileName;
log.m_stream.open(fileName.c_str(),
std::ios_base::trunc | std::ios_base::out);
log.m_initialised = true;
Info("LOG INITIALISED");
return true;
}
return false;
}
/**
* @brief Finalises the file stream
*
* @return True if the file was successfully finalised; false if not initialised
*/
bool Log::Finalise() {
Log& log = Log::get();
if (log.m_initialised) {
Info("LOG FINALISED");
log.m_stream.close();
return true;
}
return false;
}
/**
* @brief Sets the debugging threshold
* This is the debugging threshold to use when reporting bugs, useful for having
* lots of debugging information that sometimes you just want to turn off.
*
* @param type The given debugging threshold to use
*/
void Log::SetThreshold(const Type& type) {
Log& log = Log::get();
log.m_threshold = type;
}
/**
* @brief Writes a Fatal Error to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::Fatal(const std::string& message) {
return Log::get().log(LOG_TYPE_FATAL, message);
}
/**
* @brief Writes a Fatal Error to the log
*
* @param format The format of the message
* @param ... Variable arguments
* @return True if the log was successful
*/
bool Log::Fatal(const char* format, ...) {
va_list varArgs;
va_start(varArgs, format);
bool success = Log::get().log(LOG_TYPE_FATAL, format, varArgs);
va_end(varArgs);
return success;
}
/**
* @brief Writes an Error to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::Error(const std::string& message) {
return Log::get().log(LOG_TYPE_ERROR, message);
}
/**
* @brief Writes an Error to the log
*
* @param format The format of the message
* @param ... Variable arguments
* @return True if the log was successful
*/
bool Log::Error(const char* format, ...) {
va_list varArgs;
va_start(varArgs, format);
bool success = Log::get().log(LOG_TYPE_ERROR, format, varArgs);
va_end(varArgs);
return success;
}
/**
* @brief Writes a warning to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::Warn(const std::string& message) {
return Log::get().log(LOG_TYPE_WARN, message);
}
/**
* @brief Writes a warning to the log
*
* @param format The format of the message
* @param ... Variable arguments
* @return True if the log was successful
*/
bool Log::Warn(const char* format, ...) {
va_list varArgs;
va_start(varArgs, format);
bool success = Log::get().log(LOG_TYPE_WARN, format, varArgs);
va_end(varArgs);
return success;
}
/**
* @brief Writes an information message to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::Info(const std::string& message) {
return Log::get().log(LOG_TYPE_INFO, message);
}
/**
* @brief Writes an information message to the log
*
* @param format The format of the message
* @param ... Variable arguments
* @return True if the log was successful
*/
bool Log::Info(const char* format, ...) {
va_list varArgs;
va_start(varArgs, format);
bool success = Log::get().log(LOG_TYPE_INFO, format, varArgs);
va_end(varArgs);
return success;
}
/**
* @brief Writes a Debug message to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::Debug(const std::string& message) {
return Log::get().log(LOG_TYPE_DEBUG, message);
}
/**
* @brief Writes an Debug message to the log
*
* @param format The format of the message
* @param ... Variable arguments
* @return True if the log was successful
*/
bool Log::Debug(const char* format, ...) {
va_list varArgs;
va_start(varArgs, format);
bool success = Log::get().log(LOG_TYPE_DEBUG, format, varArgs);
va_end(varArgs);
return success;
}
/**
* @brief Peeks at the top element of the function stack
*
* @return The top element of the function stack
*/
std::string Log::Peek() {
return Log::get().m_stack.back();
};
/**
* @brief Pushes the function stack with the given message
*
* @param input The message to store in the stack (typically the name of the
* function)
* @return True if the stack was successfully pushed
*/
bool Log::Push(const std::string& input) {
if (!input.empty()) {
Debug(input + " BEGIN");
Log::get().m_stack.push_back(input);
return true;
}
return false;
}
/**
* @brief Pops the top element off the stack
*
* @return The message just popped off the stack
*/
std::string Log::Pop() {
Log& log = Log::get();
if (!log.m_stack.empty()) {
std::string temp(log.Peek());
log.m_stack.pop_back();
Debug(temp + " END");
return temp;
}
return std::string();
}
/**
* @brief Writes the stack to the log
*/
void Log::PrintStackTrace() {
Log& log = Log::get();
std::string temp = "---Stack Trace---\n";
for (std::vector<std::string>::reverse_iterator i = log.m_stack.rbegin();
i != log.m_stack.rend(); ++i) {
temp += "| " + *i + "\n";
}
temp += "-----------------";
log.write(temp.c_str());
}
/**
* @brief Constructor
*/
Log::Log()
: m_threshold(LOG_TYPE_INFO),
m_fileName(),
m_stack(),
m_stream() {
}
/**
* @brief Copy constructor
* Kept private in order to preserve singleton
*/
Log::Log(const Log&) {
}
/**
* @brief Destructor
* Logs the shut down then closes the file stream
*/
Log::~Log() {
Finalise();
}
/**
* @brief Get the singleton instance
*/
Log& Log::get() {
static Log log;
return log;
}
/**
* @brief Writes the specified message to the console and the log file
*
* @param format The format of the message
* @param ... Variable arguments
*/
void Log::write(const char* format, ...) {
char buffer[512];
va_list varArgs;
va_start(varArgs, format);
vsnprintf(buffer, sizeof(buffer), format, varArgs);
va_end(varArgs);
std::cout << buffer << std::endl;
m_stream << buffer << std::endl;
}
/**
* @brief Logs the specified message with a timestamp and category prefix
* The constant TIMESTAMP_BUFFER_SIZE was calculated as the maximum number of
* characters required for the timestamp "[HH:MM:SS MM/DD/YY] ".
*
* @param type The category of message to write based on the enum Log::Type
* @param message The message to be sent
* @return True if the log was successful
*/
bool Log::log(const Type& type, const std::string& message) {
if (type <= m_threshold) {
static const int TIMESTAMP_BUFFER_SIZE = 21;
char buffer[TIMESTAMP_BUFFER_SIZE];
time_t timestamp;
time(×tamp);
strftime(buffer, sizeof(buffer), "%X %x", localtime(×tamp));
write("[%s] %s - %s", buffer, TypeToString(type), message.c_str());
return true;
}
return false;
}
/**
* @brief Logs the specified message with a timestamp and category prefix
* The constant TIMESTAMP_BUFFER_SIZE was calculated as the maximum number of
* characters required for the timestamp "[HH:MM:SS MM/DD/YY] ".
*
* @param type The category of message to write based on the enum Log::Type
* @param format The format of the message
* @param ... Variable arguments
* @return True if the log was successful
*/
bool Log::log(const Type& type, const char* format, const va_list& varArgs) {
char buffer[512];
vsnprintf(buffer, sizeof(buffer), format, varArgs);
return log(type, buffer);
}
/**
* @brief Copy operator
* Kept private in order to preserve singleton
*/
Log& Log::operator=(const Log&) {
return *this;
}