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

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

Minor: debug/info level on console.

  • Property svn:executable set to *
File size: 6.4 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//template <typename CharT>
34//PACPUSLIB_API std::basic_ostream<CharT>& operator<< (std::basic_ostream<CharT>& strm, QString const& s)
35//{
36// strm << s.toLocal8Bit().data();
37// return strm;
38//}
39
40// specialization for char
41template <>
42PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s)
43{
44 strm << s.toStdString();
45 return strm;
46}
47
48// specialization for wchar_t
49template <>
50PACPUSLIB_API std::basic_ostream<wchar_t>& operator<< (std::basic_ostream<wchar_t>& strm, QString const& s)
51{
52 strm << s.toStdWString();
53 return strm;
54}
55
56namespace pacpus
57{
58
59BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel)
60
61void init_log_factories()
62{
63 boost::log::register_simple_formatter_factory< QString, char >("QString");
64}
65
66static int niftyCounter;
67
68LogConfigurator::LogConfigurator()
69{
70 if (0 == niftyCounter++) {
71 LOG_INFO("LogConfigurator constructor");
72 init_log_factories();
73 }
74}
75
76LogConfigurator::~LogConfigurator()
77{
78 if (0 == --niftyCounter) {
79 // clean up
80 LOG_INFO("LogConfigurator destructor");
81 }
82}
83
84void LogConfigurator::configureLoggerWithFile(const char* logFileName)
85{
86 using namespace boost;
87
88 namespace logging = boost::log;
89 namespace sinks = boost::log::sinks;
90 namespace src = boost::log::sources;
91 namespace expr = boost::log::expressions;
92 namespace attrs = boost::log::attributes;
93 namespace keywords = boost::log::keywords;
94
95 logging::add_common_attributes();
96 logging::core::get()->add_global_attribute(
97 "ProcessID",
98 attrs::current_process_id());
99 logging::core::get()->add_global_attribute(
100 "ThreadID",
101 attrs::current_thread_id());
102 //logging::core::get()->add_global_attribute(
103 // "Scope",
104 // attrs::named_scope());
105
106 logging::core::get()->set_filter
107 (
108#ifdef NDEBUG
109 // release
110 severity >= debug
111#else
112 // debug
113 severity >= trace
114#endif
115 );
116
117 ////////////////////////////////////////////////////////////////////////////////
118 // FILE
119 logging::add_file_log
120 (
121 keywords::file_name = logFileName,
122 keywords::rotation_size = 10 * 1024 * 1024,
123 keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
124 //keywords::format = "%LineID% [%TimeStamp%]: %Message%"
125 keywords::format =
126 (
127 expr::stream
128 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
129 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
130 << " [" << boost::log::expressions::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
131 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
132 << " <" << severity << ">"
133 << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
134 << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
135 << " " << expr::smessage
136 )
137 );
138
139 ////////////////////////////////////////////////////////////////////////////////
140 // CONSOLE
141 // Create a backend and attach a couple of streams to it
142#if ! (BOOST_VERSION >= 105500)
143 using logging::empty_deleter;
144#endif
145 boost::shared_ptr< sinks::text_ostream_backend > backend =
146 make_shared< sinks::text_ostream_backend >();
147 backend->add_stream(
148 shared_ptr< std::ostream >(&std::clog, empty_deleter())
149 );
150
151 // Enable auto-flushing after each log record written
152 backend->auto_flush(true);
153
154 // Wrap it into the frontend and register in the core.
155 // The backend requires synchronization in the frontend.
156 typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
157 shared_ptr< sink_t > sink(new sink_t(backend));
158 sink->set_filter
159 (
160#ifdef NDEBUG
161 // release
162 severity >= info
163#else
164 // debug
165 severity >= debug
166#endif
167 );
168
169 sink->set_formatter
170 (
171 expr::stream
172#if defined(PACPUS_LOG_COLORED_OUTPUT)
173 << formatSeverityWithColors< SeverityLevel >("Severity")
174#endif
175 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
176 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] "
177 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "] "
178 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
179 << "<" << severity << ">"
180 << " "
181 << expr::smessage
182#if defined(PACPUS_LOG_COLORED_OUTPUT)
183 << formatSeverityWithColors< SeverityLevel >("Severity", /*restoreDefault=*/ true) // Resets the terminal to default.
184#endif
185 );
186
187 logging::core::get()->add_sink(sink);
188
189 LOG_INFO("logger initialised");
190}
191
192} // namespace pacpus
193
194#else // PACPUS_USE_LOG
195
196namespace pacpus
197{
198
199 LogConfigurator::LogConfigurator()
200 {}
201
202 LogConfigurator::~LogConfigurator()
203 {}
204
205 void LogConfigurator::configureLoggerWithFile(const char* /*configFilename*/)
206 {}
207
208} // namespace pacpus
209
210#endif // PACPUS_USE_LOG
211
Note: See TracBrowser for help on using the repository browser.