source: pacpusframework/trunk/src/PacpusLib/Log.cpp@ 239

Last change on this file since 239 was 239, checked in by Marek Kurdej, 11 years ago

Added: possibility to use color log output (needs more testing).

  • Property svn:executable set to *
File size: 6.1 KB
Line 
1// %pacpus:license{
2// This file is part of the PACPUS framework distributed under the
3// CECILL-C License, Version 1.0.
4// %pacpus:license}
5/// @version $Id: Log.cpp 76 2013-01-10 17:05:10Z kurdejma $
6
7#include <Pacpus/kernel/Log.h>
8
9#if defined(PACPUS_USE_LOG)
10
11#if defined(PACPUS_LOG_COLORED_OUTPUT)
12# include "ColorSeverityFormatter.hpp"
13#endif
14
15#include <boost/log/detail/date_time_format_parser.hpp>
16#include <boost/log/expressions.hpp>
17#include <boost/log/expressions/formatters/if.hpp>
18#include <boost/log/sinks/text_file_backend.hpp>
19#include <boost/log/sinks/text_ostream_backend.hpp>
20#include <boost/log/sources/severity_logger.hpp>
21#include <boost/log/support/date_time.hpp>
22#include <boost/log/utility/setup/common_attributes.hpp>
23#include <boost/log/utility/setup/file.hpp>
24#include <boost/log/utility/setup/formatter_parser.hpp>
25#if BOOST_VERSION >= 105500 // header exists from 1.55
26# include <boost/utility/empty_deleter.hpp>
27#else
28# include <boost/log/utility/empty_deleter.hpp>
29#endif
30#include <ostream>
31#include <QString>
32
33// specialization for char
34template <>
35PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s)
36{
37 strm << s.toStdString();
38 return strm;
39}
40
41// specialization for wchar_t
42template <>
43PACPUSLIB_API std::basic_ostream<wchar_t>& operator<< (std::basic_ostream<wchar_t>& strm, QString const& s)
44{
45 strm << s.toStdWString();
46 return strm;
47}
48
49namespace pacpus
50{
51
52BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel)
53
54void init_log_factories()
55{
56 boost::log::register_simple_formatter_factory< QString, char >("QString");
57}
58
59static int niftyCounter;
60
61LogConfigurator::LogConfigurator()
62{
63 if (0 == niftyCounter++) {
64 LOG_INFO("LogConfigurator constructor");
65 init_log_factories();
66 }
67}
68
69LogConfigurator::~LogConfigurator()
70{
71 if (0 == --niftyCounter) {
72 // clean up
73 LOG_INFO("LogConfigurator destructor");
74 }
75}
76
77void LogConfigurator::configureLoggerWithFile(const char* logFileName)
78{
79 using namespace boost;
80
81 namespace logging = boost::log;
82 namespace sinks = boost::log::sinks;
83 namespace src = boost::log::sources;
84 namespace expr = boost::log::expressions;
85 namespace attrs = boost::log::attributes;
86 namespace keywords = boost::log::keywords;
87
88 logging::add_common_attributes();
89 logging::core::get()->add_global_attribute(
90 "ProcessID",
91 attrs::current_process_id());
92 logging::core::get()->add_global_attribute(
93 "ThreadID",
94 attrs::current_thread_id());
95 //logging::core::get()->add_global_attribute(
96 // "Scope",
97 // attrs::named_scope());
98
99 logging::core::get()->set_filter
100 (
101#ifdef NDEBUG
102 // release
103 severity >= debug
104#else
105 // debug
106 severity >= trace
107#endif
108 );
109
110 ////////////////////////////////////////////////////////////////////////////////
111 // FILE
112 logging::add_file_log
113 (
114 keywords::file_name = logFileName,
115 keywords::rotation_size = 10 * 1024 * 1024,
116 keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
117 //keywords::format = "%LineID% [%TimeStamp%]: %Message%"
118 keywords::format =
119 (
120 expr::stream
121 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
122 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
123 << " [" << boost::log::expressions::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
124 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
125 << " <" << severity << ">"
126 << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
127 << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
128 << " " << expr::smessage
129 )
130 );
131
132 ////////////////////////////////////////////////////////////////////////////////
133 // CONSOLE
134 // Create a backend and attach a couple of streams to it
135#if ! (BOOST_VERSION >= 105500)
136 using logging::empty_deleter;
137#endif
138 boost::shared_ptr< sinks::text_ostream_backend > backend =
139 make_shared< sinks::text_ostream_backend >();
140 backend->add_stream(
141 shared_ptr< std::ostream >(&std::clog, empty_deleter())
142 );
143
144 // Enable auto-flushing after each log record written
145 backend->auto_flush(true);
146
147 // Wrap it into the frontend and register in the core.
148 // The backend requires synchronization in the frontend.
149 typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
150 shared_ptr< sink_t > sink(new sink_t(backend));
151 sink->set_filter
152 (
153 severity >= info
154 );
155
156 sink->set_formatter
157 (
158 expr::stream
159#if defined(PACPUS_LOG_COLORED_OUTPUT)
160 << formatSeverityWithColors< SeverityLevel >("Severity")
161#endif
162 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
163 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] "
164 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "] "
165 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
166 << "<" << severity << ">"
167 << " "
168 << expr::smessage
169#if defined(PACPUS_LOG_COLORED_OUTPUT)
170 << formatSeverityWithColors< SeverityLevel >("Severity", /*restoreDefault=*/ true) // Resets the terminal to default.
171#endif
172 );
173
174 logging::core::get()->add_sink(sink);
175
176 LOG_INFO("logger initialised");
177}
178
179} // namespace pacpus
180
181#else // PACPUS_USE_LOG
182
183namespace pacpus
184{
185
186 LogConfigurator::LogConfigurator()
187 {}
188
189 LogConfigurator::~LogConfigurator()
190 {}
191
192 void LogConfigurator::configureLoggerWithFile(const char* /*configFilename*/)
193 {}
194
195} // namespace pacpus
196
197#endif // PACPUS_USE_LOG
198
Note: See TracBrowser for help on using the repository browser.