-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructuredLogger.cpp
More file actions
560 lines (496 loc) · 20.1 KB
/
structuredLogger.cpp
File metadata and controls
560 lines (496 loc) · 20.1 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include "structuredLogger.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/container/map.hpp"
#include "structuredLogger.h"
std::string FormatTime();
///
///\class structuredLogger
///<single logger for all logging within a program
///< counter is the error record number
///< scoped logging only works with macros as it needs to be in the actual scope
///< tagged records are much like scoped and are one time only which seems useless
//
//BOOST_LOG_ATTRIBUTE_KEYWORD(file,"File",std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(channel,"Channel",std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(token_attr, "Token", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(scope1, "Scope1", std::string )
BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id,"ThreadID",attrs::current_thread_id::value_type)
min_severity_filter structuredLogger::m_min_severity = expr::channel_severity_filter(channel,severity);
// This mutable constant will always lock exclusively
exclusive_mc structuredLogger::m_tokenattr("method");
///\fn getInstance
///\arg const char * logname
///\arg t_loggingStyle
///\brief get single instance logger
std::shared_ptr<structuredLogger> structuredLogger::getInstance(const char * logname, t_loggingStyle logStyle)
{
singleton<structuredLogger> s_instance ;
std::shared_ptr<structuredLogger> rVal = s_instance.getInstance();
rVal->init(logname,logStyle);
return rVal;
}
///\fn getInstanceByName
///\arg const char * logname
///\arg t_loggingStyle
///\brief multiple instances by name
std::shared_ptr<structuredLogger> structuredLogger::getInstanceByName(const char * logname, t_loggingStyle logStyle)
{
static boost::container::map<std::string, structuredLogger * > m_fileMap;
boost::container::map<std::string, structuredLogger*>::iterator it;
if (m_fileMap.empty()) {
m_fileMap.insert(std::pair<std::string,structuredLogger *>(std::string(logname),new structuredLogger(logname,logStyle)) );
}else {
it = m_fileMap.find(std::string(logname));
if (it == m_fileMap.end()) {
m_fileMap.insert(std::pair<std::string,structuredLogger *>(std::string(logname),new structuredLogger(logname,logStyle)) );
}
}
it = m_fileMap.find(std::string(logname));
return (std::shared_ptr<structuredLogger>(it->second));
}
structuredLogger::structuredLogger(const char *logname,t_loggingStyle logStyle)
{
init(logname,logStyle);
}
///\fn init
///\arg const char * logname/filespec [in]
///\arg t_loggingStyle logstyle [in]
///\< initialize the logger with a logname/filespec
///\< set the 3 possible logging favors debug, logs everything
///\< token, logs only those tokens that have been specified
///\< normal anything that is not abnormal
///\< open up 0-3 files "name.log", "name.error", and "name.debug"
void structuredLogger::init(const char * logname,t_loggingStyle logStyle)
{
if (!m_init) {
if (!(logStyle & (singleFile|allInfoFile|sepErrorFile|sepDebugFile))) {
struct badLoggingStyle b;
throw(b);
}
if (logname == nullptr)
logname = "program";
///set up log name for rotation
m_fname.append(logname);
m_fname.append(FormatTime());
m_init = true;
// set defaults
//m_debug = false;
m_enabledLoggers=logStyle;
/**
*
// full message looks like
// logRecordNumber yyyy-mm-dd hh:mm:ss <severity> [scope] [thread] [timing] message ** debug pluse timing
// logRecordNumber yyyy-mm-dd hh:mm:ss <severity> message ** normal logging
//
**/
debugFormatter();
tokenFormatter();
normalFormatter();
/// normal log on by default
/// allInfoFile covers severity normal through warning
if (m_enabledLoggers & allInfoFile)
{
std::string lname(m_fname);
lname += ".log";
/// boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >() ;
boost::shared_ptr< text_sink > sink(new text_sink());
///keywords::file_name=lname.c_str(),
///keywords::rotation_size = ROTATIONSIZE_1MB));
m_sinkNormal = sink;
sink->locked_backend()->add_stream(boost::make_shared< std::ofstream >(lname.c_str() ));
sink->locked_backend()->auto_flush(true);
if (m_enabledLoggers & tokenMsg) {
sink->set_formatter(m_fmtToken);
}else {
sink->set_formatter(m_fmtNormal); //&structuredLogger::formatter); //(fmt);
}
sink->set_filter(severity <= (e_severity_level) warning );
//sink->set_filter(structuredLogger::m_min_severity || severity >= normal );
logging::core::get()->add_sink(sink);
sink.reset();
}
/// error log
/// todo make this optional
/// add a separate <name><date>.error file covering severity error through stacktrace
if (m_enabledLoggers & sepErrorFile)
{
std::string lname(m_fname);
lname += ".error";
/// boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >() ;
boost::shared_ptr< text_sink > sink(new text_sink());
///keywords::file_name=lname.c_str(),
///keywords::rotation_size = ROTATIONSIZE_1MB)); std::string lename(logname);
///sink = boost::make_shared<text_sink>();
m_sinkError = sink;
sink->locked_backend()->add_stream(boost::make_shared< std::ofstream >(lname.c_str() ));
sink->locked_backend()->auto_flush(true);
sink->set_formatter(m_fmtToken);
sink->set_filter(severity >= error );
logging::core::get()->add_sink(sink);
sink.reset();
}
/// debug log
/// todo make this optional
/// separate file covering severity debug through stacktrace
if (m_enabledLoggers & sepDebugFile)
{
std::string lname(m_fname);
lname += ".debug";
/// boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >() ;
boost::shared_ptr< text_sink > sink(new text_sink());
///keywords::file_name=lname.c_str(),
///keywords::rotation_size = ROTATIONSIZE_1MB));
///sink = boost::make_shared<text_sink>();
m_sinkDebug = sink;
sink->locked_backend()->add_stream(boost::make_shared< std::ofstream >(lname.c_str() ));
sink->locked_backend()->auto_flush(true);
sink->set_formatter(m_fmtDebug);
sink->set_filter(severity >= debug );
logging::core::get()->add_sink(sink);
sink.reset();
}
/// single file covering severity normal through stacktrace
if (m_enabledLoggers & singleFile)
{
std::string lname(m_fname);
lname += ".everything";
/// boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >() ;
boost::shared_ptr< text_sink > sink(new text_sink());
///keywords::file_name=lname.c_str(),
///keywords::rotation_size = ROTATIONSIZE_1MB));
///sink = boost::make_shared<text_sink>();
m_sinkDebug = sink;
sink->locked_backend()->add_stream(boost::make_shared< std::ofstream >(lname.c_str() ));
sink->locked_backend()->auto_flush(true);
sink->set_formatter(m_fmtDebug);
sink->set_filter(severity <= stacktrace );
logging::core::get()->add_sink(sink);
sink.reset();
}
/*
attrs::mutable_constant<std::string> keyWordAttr("INFORMATION");
attrs::mutable_constant<std::string> scope1Attr("none");
m_keyWordAttr = &keyWordAttr;
m_scope1Attr = &scope1Attr;
logging::core::get()->add_global_attribute("Token",keyWordAttr);
logging::core::get()->add_global_attribute("Scope1",scope1Attr);
*/
/// Add attributes
logging::add_common_attributes(); /// LineID, TimeStamp, ProcessID ThreadID
attrs::mutable_constant<std::string> tokenAddr("method");
src::severity_logger_mt< severity_level > slg;
slg.add_attribute("Token",structuredLogger::m_tokenattr);
logging::core::get()->add_global_attribute("Token",structuredLogger::m_tokenattr);
// keywords::auto_flush=true;
// logging::core::get()->add_global_attribute("Scope", attrs::named_scope()); //ctor
// logging::core::get()->add_global_attribute("KeyWord",attrs::mutable_constant<std::string>m_keyWordAttr);
// logging::core::get()->add_global_attribute("Tag",attrs::tag_attr);
}/** end if not initialized */
}
///\fn debugFormatter
///\arg none
///\return none
///< creates the debug formatter with the following out put form
///< full message looks like
///< logRecordNumber yyyy-mm-dd hh:mm:ss <severity> [scope] [thread] [timing] message ** debug plus timing
void structuredLogger::debugFormatter() {
///logging::formatter
m_fmtDebug = expr::stream
<< line_id << " "
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<<" <" << severity << "> "
<< expr::if_(expr::has_attr(token_attr) ) // get method for call
[
expr::stream << "[" << token_attr << "] "
]
<< expr::if_(expr::has_attr(thread_id) ) // get thread id
[
expr::stream << "[" << thread_id << "] "
]
<< expr::if_(expr::has_attr(timeline))
[
expr::stream << "[" << timeline << "] "
]
// << expr::smessage; //<< std::endl
<< expr::if_(expr::has_attr(scope1))
[
expr::stream
<< expr::format_named_scope(
"Scopes",
keywords::format = "%n (%f:%l)",
keywords::iteration = expr::reverse)
]
<< expr::smessage; //<< std::endl
}
///\fn tokenFormatter
///\arg none
///\return none
///< logRecordNumber yyyy-mm-dd hh:mm:ss <severity> [token] message ** normal logging
void structuredLogger::tokenFormatter(){
///logging::formatter
m_fmtToken = expr::stream
<< line_id << " "
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<<" <" << severity << "> "
<< expr::if_(expr::has_attr(token_attr) ) // get method for call
[
expr::stream << "[" << token_attr << "] "
]
<< expr::smessage ;
}
///\fn normalFormatter
///\arg none
///\return none
///< logRecordNumber yyyy-mm-dd hh:mm:ss <severity> message ** normal logging
void structuredLogger::normalFormatter() {
///logging::formatter
m_fmtNormal = expr::stream
<< line_id << " "
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<<" <" << severity << "> "
<< expr::smessage ;
}
///\fn ~stucturedLogger
///\arg none
///\return none
///< just remove all sinks and set m_init to false
structuredLogger::~structuredLogger()
{
if (! m_deleteInProgress) {
m_deleteInProgress = true;
//logging::core::get()->remove_all_sinks();
boost::shared_ptr<logging::core> core = logging::core::get();
//std::cout << " normalSink count:" << m_sinkNormal.use_count() << std::endl;
if (m_sinkNormal.get() != nullptr){
m_sinkNormal->flush();
m_sinkNormal->stop();
//core->remove_sink(m_sinkNormal);
m_sinkNormal.reset();
}
// core->remove_sink(m_sinkDebug);
if (m_sinkDebug.get() != nullptr){
m_sinkDebug->flush();
m_sinkDebug->stop();
m_sinkDebug.reset();
}
//core->remove_sink(m_sinkError);
if (m_sinkError.get() != nullptr){
m_sinkError->flush();
m_sinkError->stop();
m_sinkError.reset();
}
m_init = false;
m_deleteInProgress = false;
}
}
///\fn logMsg
///\arg severity_level [in] severity_level
///\arg const char * msg [in] message to be logged
///< log a message , severity level will determine which file/s the message will be logged
void structuredLogger::logMsg(severity_level level, const char * msg)
{
src::severity_logger_mt< severity_level > slg;
BOOST_LOG_SEV(slg, level) << msg;
}
//[ example_tutorial_attributes_named_scope
// scope only takes a literal const char *& which can not be passed as a variable from the the macro __FUNCTION__
// With that in mind we will not use the macro BOOST_LOG_NAMED_SCOPE instead we will just add the
// default argument in as part of the string
///\fn named_scopeL
///\arg severity_level [in] severity_level
///\arg const char * msg [in] message to be logged
///\arg std::string & scope name
///< log a message , severity level will determine which file/s the message will be logged
void structuredLogger::named_scopeL(severity_level level, const char * msg,std::string &scope)
{
//src::severity_logger< severity_level > slg;
src::severity_logger_mt< severity_level > slg;
std::string tmag("[");
tmag.append(scope);
tmag.append("]");
// m_scope1Attr->set(scope);
BOOST_LOG_SEV(slg, level) << tmag.c_str() << msg;
}
//]
//[ example_tutorial_attributes_tagged_logging
///\fn tokenL
///\arg severity_level [in] severity_level
///\arg const char * msg [in] message to be logged
///\arg const char * method name
///< log a message , severity level will determine which file/s the message will be logged
///< log a token message ie by method name
void structuredLogger::tokenL(severity_level level,const char * msg, const char* method)
{
src::severity_logger_mt< severity_level > slg;
structuredLogger::m_tokenattr.set(method);
//m_keyWordAttr->set(tag);
BOOST_LOG_SEV(slg, level) << msg ;
//m_slg->delete_attribute("Tag")
}
//]
//[ example_tutorial_attributes_tagged_logging
///\fn debugL
///\arg severity_level [in] severity_level
///\arg const char * msg [in] message to be logged
///\arg const char * method name [in]
///< log a message , with a debug message
void structuredLogger::debugL(severity_level level,const char * msg,const char* method)
{
src::severity_logger_mt< severity_level > slg;
structuredLogger::m_tokenattr.set(method);
BOOST_LOG_SEV(slg, level) << msg ;
}
///\fn logMsg
///\arg severity_level [in] severity_level
///\arg const char * msg [in] message to be logged
///\arg const char * method name
///< log a message to the error log
void structuredLogger::errorL(severity_level l,const char * msg, const char * method)
{
src::severity_logger_mt< severity_level > slg;
structuredLogger::m_tokenattr.set(method);
BOOST_LOG_SEV(slg,l) << msg;
#if 0 //attrs::Severity.set(l);
structuredLogger::m_tokenattr.set(method);
//BOOST_LOG_SEV(slg,severityL) < msg;
logging::record rec = slg.open_record(keywords::severity =l); //keywords::severity = normal,keywords::channel);
if (rec)
{
logging::record_ostream strm(rec);
strm << msg;
strm.flush();
slg.push_record(boost::move(rec));
}
#endif
}
/**
* the following methods have not been tested
*/
//[ example_tutorial_attributes_timed_logging
void structuredLogger::timed_Start(const char* item, severity_level level)
{
BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
src::severity_logger_mt< severity_level > slg;
//src::severity_logger< severity_level > slg;
BOOST_LOG_SEV(slg, level) << "starting timing " << item;
}
void structuredLogger::timed_Stop( const char * item, severity_level level)
{
src::severity_logger_mt< severity_level > slg;
BOOST_LOG_SEV(slg, level) << "Stopping timing " << item;
}
/**
* general_logging called from inline generalL
**/
///\fn general_logging
///\arg string msg
///\arg severity_level
///\arg string method
void structuredLogger::general_logging(std::string &msg, severity_level l ,std::string &method)
{
general_logging(msg.c_str(),l,method.c_str());
}
void structuredLogger::general_logging(const char* msg,severity_level l,const char* method)
{
src::severity_logger_mt< severity_level > slg;
//attrs::Severity.set(l);
structuredLogger::m_tokenattr.set(method);
//BOOST_LOG_SEV(slg,severityL) < msg;
logging::record rec = slg.open_record(keywords::severity = l); //keywords::severity = normal,keywords::channel);
if (rec)
{
logging::record_ostream strm(rec);
strm << msg;
strm.flush();
slg.push_record(boost::move(rec));
}
}
///\fn stack_trace
///\arg const char * item
///\arg const char * method name of the caller
///< force a stack dump and log it to debug and error
void structuredLogger::stack_trace(const char* item,const char * method)
{
src::severity_logger_mt< severity_level > slg;
structuredLogger::m_tokenattr.set(method);
BOOST_LOG_SEV(slg, debug) << stackTrace().str() ;
}
#if 0
void structuredLogger::formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
{
structuredLogger sl = structuredLogger::getLogger(nullptr);
strm << line_id << " "
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
<<" <" << severity << "> "
<< expr::if_(expr::has_attr(token_attr) ) // get method for call
[
expr::stream << "[" << token_attr << "] "
]
/*
<< expr::if_(expr::has_attr(scope1))
[
expr::stream << "(" << scope1 << ") "
]
*/
<< expr::if_(expr::has_attr(thread_id) ) // get thread id
[
expr::stream << "[" << thread_id << "] "
]
/*
<< expr::if_(expr::has_attr(timeline))
[
expr::stream << "[" << timeline << "] "
]
*/
<< rec[expr::smessage] ;
}
#endif
//
// needs to be in try block of boost
//void structuredLogger::stackDumpL(const char * msg){
//system call backtrace
//
// BOOST_LOG_SEV(*m_slg,critical) << logging::current_scope();
//
//}
//]
/// The operator puts a human-friendly representation of the severity level to the stream
///\fn std::ostream& structuredLogger::operator<<
///\arg (std::ostream& strm,
///\arg severity_level level)
/// <protected call that converts severity to a string
std::ostream& operator<< (std::ostream& strm, severity_level level)
{
static const char* sevStrings[] =
{
"normal",
"notification",
"warning",
"error",
"critical",
"debug",
"stacktrace"
};
if (static_cast< std::size_t >(level ) < sizeof(sevStrings) / sizeof(*sevStrings))
strm << sevStrings[level];
else
strm << static_cast< int >(level );
return strm;
}
std::string FormatTime()
{
using namespace boost::posix_time;
ptime now = second_clock::universal_time();
static std::locale loc(std::cout.getloc(),
new time_facet("%Y.%m.%d_%H.%M.%S"));
std::stringstream wss;
wss.imbue(loc);
wss << now;
return wss.str();
}