Horizon Official Technical Documentation
Logger Class Reference

#include <Logger.hpp>

Public Member Functions

void initialize ()
 
logtypeget_core_log ()
 
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > get_console_sink ()
 
void colored_formatter (boost::log::record_view const &rec, boost::log::formatting_ostream &strm)
 
void shutdown ()
 

Static Public Member Functions

static LoggergetInstance ()
 

Protected Attributes

logtype _core_log
 
std::atomic< bool > _initialized
 
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > _consoleSink
 
boost::shared_ptr< boost::log::sinks::asynchronous_sink< boost::log::sinks::text_file_backend > > _fileSink
 

Private Types

typedef boost::log::sources::severity_logger< boost::log::trivial::severity_level > logtype
 

Member Typedef Documentation

◆ logtype

typedef boost::log::sources::severity_logger<boost::log::trivial::severity_level> Logger::logtype
private

Member Function Documentation

◆ colored_formatter()

void Logger::colored_formatter ( boost::log::record_view const &  rec,
boost::log::formatting_ostream &  strm 
)
43{
44 static auto date_time_formatter = boost::log::expressions::stream << boost::log::expressions::format_date_time<boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f");
45
46 strm << "[";
47 date_time_formatter(rec, strm);
48 strm << "] [";
49
50 auto severity = rec[boost::log::trivial::severity];
51
52 switch (*severity) {
53 case boost::log::trivial::trace:
54 case boost::log::trivial::debug:
55 strm << color(90);
56 break;
57 case boost::log::trivial::info:
58 strm << color(36);
59 break;
60 case boost::log::trivial::warning:
61 strm << color(33);
62 break;
63 case boost::log::trivial::error:
64 case boost::log::trivial::fatal:
65 strm << color(31);
66 break;
67 default:
68 break;
69 }
70
71 strm << severity << color(0) << "] " << rec[boost::log::expressions::message];
72}
static std::string color(uint16_t color)
Definition: Logger.cpp:40

References color().

Referenced by initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_console_sink()

boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > Logger::get_console_sink ( )
inline
62{ return _consoleSink; }
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > > _consoleSink
Definition: Logger.hpp:72

References _consoleSink.

◆ get_core_log()

logtype & Logger::get_core_log ( )
inline
60{ return _core_log; }
logtype _core_log
Definition: Logger.hpp:68

References _core_log.

Referenced by HLogStream::~HLogStream().

+ Here is the caller graph for this function:

◆ getInstance()

static Logger * Logger::getInstance ( )
inlinestatic
49 {
50 static Logger instance;
51
52 if (!instance._initialized)
53 instance.initialize();
54
55 return &instance;
56 }
Definition: Logger.hpp:43
void initialize()
Definition: Logger.cpp:74
std::atomic< bool > _initialized
Definition: Logger.hpp:70

References _initialized, and initialize().

Referenced by HLogStream::~HLogStream().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initialize()

void Logger::initialize ( )
75{
76 boost::log::core::get()->add_global_attribute("Scope",
77 boost::log::attributes::named_scope());
78
79 boost::log::core::get()->set_filter(
80 boost::log::trivial::severity >= boost::log::trivial::trace
81 );
82
83 /* log formatter:
84 * [TimeStamp] [Severity Level] Log message
85 */
86 auto fmtTimeStamp = boost::log::expressions::
87 format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f");
88
89 auto fmtSeverity = boost::log::expressions::
90 attr<boost::log::trivial::severity_level>("Severity");
91
92 auto fmtScope = boost::log::expressions::format_named_scope("Scope",
93 boost::log::keywords::format = "%n(%f:%l)",
94 boost::log::keywords::iteration = boost::log::expressions::reverse,
95 boost::log::keywords::depth = 2);
96
97 boost::log::formatter logFmt =
98 boost::log::expressions::format("[%1%] (%2%) %3%")
99 % fmtTimeStamp % fmtSeverity
100 % boost::log::expressions::smessage;
101
102 /* console sink */
103 _consoleSink = boost::make_shared<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>>();
104 boost::log::core::get()->add_sink(_consoleSink);
105
106 boost::shared_ptr<std::ostream> stream{&std::clog, boost::null_deleter{}};
107 _consoleSink->locked_backend()->add_stream(stream);
108
109 _consoleSink->set_formatter(std::bind(&Logger::colored_formatter, this, std::placeholders::_1, std::placeholders::_2));
110
111 /* fs sink */
112 _fileSink = boost::make_shared<boost::log::sinks::asynchronous_sink<boost::log::sinks::text_file_backend>>(
113 boost::log::keywords::target = "logs",
114 boost::log::keywords::file_name = "logs/log_%Y-%m-%d_%H-%M-%S.%N.log",
115 boost::log::keywords::rotation_size = 10 * 1024 * 1024,
116 boost::log::keywords::min_free_space = 30 * 1024 * 1024,
117 boost::log::keywords::open_mode = std::ios_base::app,
118 boost::log::keywords::start_thread = false);
119
120 _fileSink->set_formatter(logFmt);
121
122 _fileSink->locked_backend()->auto_flush(true);
123
124 // Add the file sink to the core
125 boost::log::core::get()->add_sink(_fileSink);
126
127 // Add common attributes
128 boost::log::add_common_attributes();
129
130 _initialized.store(true);
131}
boost::shared_ptr< boost::log::sinks::asynchronous_sink< boost::log::sinks::text_file_backend > > _fileSink
Definition: Logger.hpp:73
void colored_formatter(boost::log::record_view const &rec, boost::log::formatting_ostream &strm)
Definition: Logger.cpp:42

References _consoleSink, _fileSink, _initialized, and colored_formatter().

Referenced by getInstance().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ shutdown()

void Logger::shutdown ( )
133 {
134 // Remove and flush the console sink
135 if (_consoleSink) {
136 _consoleSink->flush();
137 boost::log::core::get()->remove_sink(_consoleSink);
138 _consoleSink.reset();
139 }
140
141 // Remove and flush the file sink
142 if (_fileSink) {
143 _fileSink->flush();
144 boost::log::core::get()->remove_sink(_fileSink);
145 _fileSink.reset();
146 }
147}

References _consoleSink, and _fileSink.

Member Data Documentation

◆ _consoleSink

boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> > Logger::_consoleSink
protected

◆ _core_log

logtype Logger::_core_log
protected

Referenced by get_core_log().

◆ _fileSink

boost::shared_ptr<boost::log::sinks::asynchronous_sink<boost::log::sinks::text_file_backend> > Logger::_fileSink
protected

Referenced by initialize(), and shutdown().

◆ _initialized

std::atomic<bool> Logger::_initialized
protected

Referenced by getInstance(), and initialize().


The documentation for this class was generated from the following files: