forked from somma/_MyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
441 lines (386 loc) · 8.54 KB
/
log.cpp
File metadata and controls
441 lines (386 loc) · 8.54 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**----------------------------------------------------------------------------
* log.cpp
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by Noh,Yonghwan (fixbrain@gmail.com, unsorted@msn.com)
*-----------------------------------------------------------------------------
* 2015:1:12 16:54 created
**---------------------------------------------------------------------------*/
#include "stdafx.h"
#include "log.h"
/**
* @brief
**/
static slogger* _logger = NULL;
static bool _show_process_name = true;
static bool _show_pid_tid = true;
static bool _show_function_name = true;
/**
* @brief log 모듈을 초기화한다.
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool
initialize_log(
_In_ LogLevel log_level,
_In_opt_z_ const wchar_t* log_file_path
)
{
if (NULL != _logger) return true;
slogger* local_slogger = new slogger();
if (NULL == local_slogger)
{
OutputDebugStringA("[ERR ] initialize_log(), insufficient resource for slogger.\n");
return false;
}
if (true != local_slogger->slog_start(log_level, log_file_path))
{
OutputDebugStringA("[ERR ] initialize_log(), _logger->slog_start() failed.\n");
return false;
}
// exchange instance
InterlockedExchangePointer((PVOID*)&_logger, local_slogger);
local_slogger = NULL;
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void
finalize_log(
)
{
if (NULL == _logger) return;
_logger->slog_stop();
delete _logger; _logger = NULL;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void
set_log_format(
_In_ bool show_process_name,
_In_ bool show_pid_tid,
_In_ bool show_function_name
)
{
_show_process_name = show_function_name;
_show_pid_tid = show_pid_tid;
_show_function_name = show_function_name;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void
log_write(
_In_ LogLevel log_level,
_In_ uint32_t log_to,
_In_z_ const char* function,
_In_z_ const char* log_message
)
{
_ASSERTE(NULL != function);
_ASSERTE(NULL != log_message);
if (NULL == function || NULL == log_message) return;
// check base log level
if (NULL != _logger && _logger->slog_get_base_log_level() > log_level) return;
// show log level prefix
std::stringstream log_strm;
switch (log_level)
{
case log_level_debug: log_strm << "[DBG ] "; break;
case log_level_info: log_strm << "[INFO] "; break;
case log_level_warn: log_strm << "[WARN] "; break;
case log_level_error: log_strm << "[ERR ] "; break;
case log_level_msg: log_strm << ""; break;
default:
_ASSERTE(!"never reach here!");
return;
}
//> show process name
if (true == _show_process_name)
{
std::string module_file = WcsToMbsEx(get_current_module_fileEx().c_str());
log_strm << module_file;
}
//> show pid, tid
if (true == _show_pid_tid)
{
log_strm <<
boost::format( "(%+5u:%+5u) : " )
% GetCurrentProcessId()
% GetCurrentThreadId();
}
//> show function name
if (true == _show_function_name)
{
log_strm << boost::format( "%s : " ) % function;
}
//> add new line
log_strm << log_message;
log_strm << "\n";
if (NULL != _logger)
{
_logger->slog_write(log_level, log_to, log_strm.str().c_str());
}
else
{
if (log_to & log_to_con)
{
write_to_console(log_strm.str().c_str());
}
if (log_to & log_to_ods)
{
OutputDebugStringA(log_strm.str().c_str());
}
}
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
void
log_write_fmt(
_In_ LogLevel log_level,
_In_ uint32_t log_to,
_In_z_ const char* function,
_In_z_ const char* fmt,
_In_ ...
)
{
if (NULL == fmt) return;
char log_buffer[2048];
size_t remain = sizeof(log_buffer);
size_t remain_b = remain;
char* pos = log_buffer;
char* pos_b = pos;
va_list args;
va_start(args,fmt);
HRESULT hRes = StringCbVPrintfExA(
pos,
remain,
&pos,
&remain,
0,
fmt,
args
);
if (S_OK != hRes)
{
// invalid character 가 끼어있는 경우 발생 할 수 있음
StringCbPrintfExA(
pos_b,
remain_b,
&pos_b,
&remain_b,
0,
"invalid function call parameters"
);
remain = remain_b;
pos = pos_b;
}
va_end(args);
log_write(log_level, log_to, function, log_buffer);
}
/*****************************************************************************/
/* slogger class implementation */
/*****************************************************************************/
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
slogger::slogger()
: _stop_logger(true),
_lock(NULL),
_logger_thread(NULL),
_log_file_handle(INVALID_HANDLE_VALUE)
{
}
slogger::~slogger()
{
slog_stop();
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
bool
slogger::slog_start(
_In_ LogLevel base_log_level,
_In_opt_z_ const wchar_t *log_file_path
)
{
_ASSERTE(NULL == _lock);
_ASSERTE(NULL == _logger_thread);
if (NULL != _lock || NULL != _logger_thread) { return false; }
// if log file specified, create log file.
if (NULL != log_file_path)
{
_log_file_handle = open_file_to_write(log_file_path);
if (INVALID_HANDLE_VALUE == _log_file_handle) return false;
}
slog_set_base_log_level(base_log_level);
_lock = new AKCriticalSection();
if (TRUE != _lock->Init()) return false;
_stop_logger = false;
_logger_thread = new boost::thread( boost::bind(&slogger::slog_thread, this) );
return true;
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
void slogger::slog_stop()
{
if (true == _stop_logger) return;
_stop_logger = true;
if (NULL != _logger_thread)
{
_logger_thread->join();
//std::cout << boost::format("tid=0x%08x, %s logger thread joined. \n") % GetCurrentThreadId() % __FUNCTION__;
delete _logger_thread; _logger_thread = NULL;
}
if (INVALID_HANDLE_VALUE != _log_file_handle)
{
CloseHandle(_log_file_handle);
_log_file_handle = INVALID_HANDLE_VALUE;
}
if(NULL != _lock)
{
_lock->Terminate();
delete (_lock); _lock=NULL;
}
}
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
void
slogger::slog_write(
_In_ LogLevel level,
_In_ uint32_t log_to,
_In_z_ const char* log_message
)
{
_ASSERTE(NULL != log_message);
if (NULL == log_message) return;
// check log level
if (slog_get_base_log_level() > level) return;
// enqueue log
log_entry log(log_to, log_message);
_lock->Enter();
_log_queue.push(log);
_lock->Leave();
}
/**
* @`brief logger worker thread
* @param
* @see
* @remarks
* @code
* @endcode
* @return
*/
void slogger::slog_thread()
{
//std::cout << boost::format("tid=0x%08x, %s logger thread started \n") % GetCurrentThreadId() % __FUNCTION__;
while (true != _stop_logger)
{
// 큐에서 pop 을 하는 스레드는 현재 스레드가 유일하므로 empty 검사시에는
// lock 이 필요없다.
if (true == _log_queue.empty())
{
Sleep(10);
continue;
}
_lock->Enter();
log_entry log = _log_queue.pop();
_lock->Leave();
if (log._log_to & log_to_con)
{
write_to_console(log._msg.c_str());
}
if (log._log_to & log_to_file)
{
if (INVALID_HANDLE_VALUE != _log_file_handle)
{
write_to_filea(_log_file_handle, "%s", log._msg.c_str());
}
}
if (log._log_to & log_to_ods)
{
OutputDebugStringA(log._msg.c_str());
}
}
//std::cout << boost::format("tid=0x%08x, %s finalizing... \n") % GetCurrentThreadId() % __FUNCTION__;
_lock->Enter();
while(true != _log_queue.empty())
{
log_entry log = _log_queue.pop();
if (log._log_to & log_to_con)
{
write_to_console(log._msg.c_str());
}
if (log._log_to & log_to_file)
{
if (INVALID_HANDLE_VALUE != _log_file_handle)
{
write_to_filea(_log_file_handle, "%s", log._msg.c_str());
}
}
if (log._log_to & log_to_ods)
{
OutputDebugStringA(log._msg.c_str());
}
}
_lock->Leave();
//std::cout << boost::format("tid=0x%08x, %s logger thread terminated \n") % GetCurrentThreadId() % __FUNCTION__;
}