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

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

Minor: added (commented) manifest SxS.

  • Property svn:executable set to *
File size: 5.0 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#ifdef PACPUS_USE_LOG
10
11#include <boost/log/detail/date_time_format_parser.hpp>
12#include <boost/log/expressions.hpp>
13#include <boost/log/sinks/text_file_backend.hpp>
14#include <boost/log/sinks/text_ostream_backend.hpp>
15#include <boost/log/sources/severity_logger.hpp>
16#include <boost/log/sources/record_ostream.hpp>
17#include <boost/log/support/date_time.hpp>
18#include <boost/log/utility/setup/common_attributes.hpp>
19#include <boost/log/utility/setup/file.hpp>
20#include <boost/log/utility/setup/formatter_parser.hpp>
21#include <boost/utility/empty_deleter.hpp>
22#include <ostream>
23#include <QString>
24
25template< typename CharT, typename TraitsT >
26std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, QString const& s)
27{
28 strm << s.toStdString();
29 return strm;
30}
31
32// explicit instantiation
33template
34PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s);
35
36namespace pacpus {
37
38void init_log_factories()
39{
40 boost::log::register_simple_formatter_factory< QString, char >("QString");
41}
42
43static int niftyCounter;
44
45LogConfigurator::LogConfigurator()
46{
47 if (0 == niftyCounter++) {
48 LOG_INFO("LogConfigurator constructor");
49 init_log_factories();
50 }
51}
52
53LogConfigurator::~LogConfigurator()
54{
55 if (0 == --niftyCounter) {
56 // clean up
57 LOG_INFO("LogConfigurator destructor");
58 }
59}
60
61void LogConfigurator::configureLoggerWithFile(const char* logFileName)
62{
63 using namespace boost;
64
65 namespace logging = boost::log;
66 namespace sinks = boost::log::sinks;
67 namespace src = boost::log::sources;
68 namespace expr = boost::log::expressions;
69 namespace attrs = boost::log::attributes;
70 namespace keywords = boost::log::keywords;
71
72 logging::add_common_attributes();
73 logging::core::get()->add_global_attribute(
74 "ProcessID",
75 attrs::current_process_id());
76 logging::core::get()->add_global_attribute(
77 "ThreadID",
78 attrs::current_thread_id());
79
80 logging::core::get()->set_filter
81 (
82#ifdef NDEBUG
83 // release
84 logging::trivial::severity >= logging::trivial::debug
85#else
86 // debug
87 logging::trivial::severity >= logging::trivial::trace
88#endif
89 );
90
91 // Add a file log
92 logging::add_file_log
93 (
94 keywords::file_name = logFileName,
95 keywords::rotation_size = 10 * 1024 * 1024,
96 keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
97 //keywords::format = "%LineID% [%TimeStamp%]: %Message%"
98 keywords::format =
99 (
100 expr::stream
101 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
102 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
103 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
104 << " <" << logging::trivial::severity << ">"
105 << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
106 << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
107 << " " << expr::smessage
108 )
109 );
110
111 // Create a backend and attach a couple of streams to it
112 shared_ptr< sinks::text_ostream_backend > backend = make_shared< sinks::text_ostream_backend >();
113 backend->add_stream(
114 shared_ptr< std::ostream >(&std::clog, empty_deleter())
115 );
116
117 // Enable auto-flushing after each log record written
118 backend->auto_flush(true);
119
120 // Wrap it into the frontend and register in the core.
121 // The backend requires synchronization in the frontend.
122 typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
123 shared_ptr< sink_t > sink(new sink_t(backend));
124 sink->set_filter
125 (
126 logging::trivial::severity >= logging::trivial::info
127 );
128
129 sink->set_formatter
130 (
131 expr::stream
132 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
133 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] "
134 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "] "
135 << "<" << logging::trivial::severity << ">"
136 << " "
137 << expr::smessage
138 );
139
140 logging::core::get()->add_sink(sink);
141
142 LOG_INFO("logger initialised");
143}
144
145} // namespace pacpus
146
147#else // PACPUS_USE_LOG
148
149namespace pacpus
150
151 LogConfigurator::LogConfigurator()
152 {}
153 void LogConfigurator::configureLoggerWithFile(const char * /*configFilename*/)
154 {}
155
156} // namespace pacpus
157
158#endif // PACPUS_USE_LOG
Note: See TracBrowser for help on using the repository browser.