forked from jimmythompson/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cpp
297 lines (269 loc) · 6.43 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
#include "Log.h"
#include <ctime>
#include <iostream>
/**
* @brief Constructor
*/
Log::Log()
: m_threshold( INFO ),
m_fileName(),
m_stack(),
m_stream() {
}
/**
* @brief Copy constructor
* @details Kept private in order to preserve singleton
*/
Log::Log(const Log&) {
}
/**
* @brief Copy operator
* @details Kept private in order to preserve singleton
*/
Log& Log::operator=(const Log&) {
return *this;
}
/**
* @brief Destructor
* @details Logs the shut down then closes the file stream
*/
Log::~Log() {
if( m_initialised ) {
info( "Log shutting down" );
m_stream.close();
}
}
/**
* @brief Get the singleton instance
*/
Log& Log::get() {
static Log log;
return log;
}
/**
* @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(Type type) {
switch(type) {
case FATAL:
return "FATAL";
case ERROR:
return "ERROR";
case WARN:
return "WARN";
case INFO:
return "INFO";
case DEBUG:
return "DEBUG";
default:
break;
}
return "UNKNOWN";
}
/**
* @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::app | std::ios_base::out );
log.m_initialised = true;
info( "Log initialised" );
return true;
}
return false;
}
/**
* @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 char* fileName ) {
return initialise( std::string( fileName ) );
}
/**
* @brief Writes the specified message to the console and the log file
*
* @param message The message to write
*/
void Log::write( const std::string& message ) {
std::cout << message << std::endl;
m_stream << message << std::endl;
}
/**
* @brief Writes the specified message to the console and the log file
*
* @param message The message to write
*/
void Log::write( const char* message ) {
write( std::string( message ) );
}
/**
* @brief Logs the specified message with a timestamp and category prefix
*
* @param type The category of message to write based on the enum Log::Type
* @param message The message to log
* @return True if the log was successful
*/
bool Log::log( const Type type, const std::string& message ) {
if( type <= m_threshold ) {
std::string prefix( "[ TIME ] " );
//prefix.append(m_stack.size(), ' ');
write( prefix + std::string( typeToString(type) ) + " " + message );
/* If it's an error (or worse), dump the stack.
if( type <= ERROR ) {
write( "====== Stack Trace ======" );
for( std::vector<std::string>::reverse_iterator i = m_stack.rbegin(); i != m_stack.rend(); ++i) {
write( *i );
}
write( "=========================" );
}
*/
return true;
}
return false;
}
/**
* @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( FATAL, message );
}
/**
* @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 char* message ) {
return fatal( std::string( message ) );
}
/**
* @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( ERROR, message );
}
/**
* @brief Writes an error to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::error( const char* message ) {
return error( std::string( message ) );
}
/**
* @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( WARN, message );
}
/**
* @brief Writes a warning to the log
*
* @param message The message to log
* @return True if the log was successful
*/
bool Log::warn( const char* message ) {
return warn( std::string( message ) );
}
/**
* @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( INFO, message );
}
/**
* @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 char* message ) {
return info( std::string( message ) );
}
/**
* @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( DEBUG, message );
}
/**
* @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 char* message ) {
return debug( std::string( message ) );
}
/**
* @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() ) {
info( input + " - BEGIN" );
Log::get().m_stack.push_back( input );
return true;
}
return false;
}
/**
* @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 char* input ) {
return push( std::string( input ) );
}
/**
* @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();
info( temp + " - END" );
return temp;
}
return std::string();
}