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

Last change on this file since 310 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
RevLine 
[89]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
[239]9#if defined(PACPUS_USE_LOG)
[89]10
[239]11#if defined(PACPUS_LOG_COLORED_OUTPUT)
12# include "ColorSeverityFormatter.hpp"
13#endif
14
[143]15#include <boost/log/detail/date_time_format_parser.hpp>
16#include <boost/log/expressions.hpp>
[236]17#include <boost/log/expressions/formatters/if.hpp>
[143]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>
[236]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
[141]30#include <ostream>
[89]31#include <QString>
32
[244]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
[239]40// specialization for char
41template <>
42PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s)
[89]43{
[141]44 strm << s.toStdString();
45 return strm;
[89]46}
47
[239]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}
[89]55
[231]56namespace pacpus
57{
[236]58
59BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel)
60
[142]61void init_log_factories()
62{
[143]63 boost::log::register_simple_formatter_factory< QString, char >("QString");
[142]64}
[89]65
[143]66static int niftyCounter;
[89]67
68LogConfigurator::LogConfigurator()
69{
[143]70 if (0 == niftyCounter++) {
[146]71 LOG_INFO("LogConfigurator constructor");
[143]72 init_log_factories();
73 }
[89]74}
75
[143]76LogConfigurator::~LogConfigurator()
[89]77{
[143]78 if (0 == --niftyCounter) {
79 // clean up
[146]80 LOG_INFO("LogConfigurator destructor");
[143]81 }
[89]82}
83
[228]84void LogConfigurator::configureLoggerWithFile(const char* logFileName)
[143]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();
[176]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());
[236]102 //logging::core::get()->add_global_attribute(
103 // "Scope",
104 // attrs::named_scope());
[176]105
[143]106 logging::core::get()->set_filter
107 (
[146]108#ifdef NDEBUG
109 // release
[236]110 severity >= debug
[146]111#else
112 // debug
[236]113 severity >= trace
[146]114#endif
[143]115 );
116
[236]117 ////////////////////////////////////////////////////////////////////////////////
118 // FILE
[143]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")
[176]129 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
[239]130 << " [" << boost::log::expressions::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
[236]131 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
132 << " <" << severity << ">"
[176]133 << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
134 << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
135 << " " << expr::smessage
[143]136 )
137 );
[236]138
139 ////////////////////////////////////////////////////////////////////////////////
140 // CONSOLE
[239]141 // Create a backend and attach a couple of streams to it
142#if ! (BOOST_VERSION >= 105500)
[236]143 using logging::empty_deleter;
144#endif
145 boost::shared_ptr< sinks::text_ostream_backend > backend =
146 make_shared< sinks::text_ostream_backend >();
[143]147 backend->add_stream(
[225]148 shared_ptr< std::ostream >(&std::clog, empty_deleter())
[143]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 (
[244]160#ifdef NDEBUG
161 // release
[236]162 severity >= info
[244]163#else
164 // debug
165 severity >= debug
166#endif
[143]167 );
[146]168
[143]169 sink->set_formatter
170 (
171 expr::stream
[239]172#if defined(PACPUS_LOG_COLORED_OUTPUT)
173 << formatSeverityWithColors< SeverityLevel >("Severity")
[236]174#endif
[143]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") << "] "
[236]178 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
179 << "<" << severity << ">"
[143]180 << " "
181 << expr::smessage
[239]182#if defined(PACPUS_LOG_COLORED_OUTPUT)
183 << formatSeverityWithColors< SeverityLevel >("Severity", /*restoreDefault=*/ true) // Resets the terminal to default.
[236]184#endif
[143]185 );
186
187 logging::core::get()->add_sink(sink);
188
[144]189 LOG_INFO("logger initialised");
[143]190}
191
[89]192} // namespace pacpus
[143]193
194#else // PACPUS_USE_LOG
195
196namespace pacpus
[231]197{
[143]198
199 LogConfigurator::LogConfigurator()
200 {}
[231]201
202 LogConfigurator::~LogConfigurator()
[143]203 {}
204
[231]205 void LogConfigurator::configureLoggerWithFile(const char* /*configFilename*/)
206 {}
207
[143]208} // namespace pacpus
209
210#endif // PACPUS_USE_LOG
[231]211
Note: See TracBrowser for help on using the repository browser.