source: pacpusframework/branches/2.0-beta1/src/PacpusLib/Log.cpp@ 169

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

Update: refactoring in XmlConfigFile.
Added: attributes 'prefix', 'postfix', 'extension' in <parameters>.
Example: <parameters prefix="" postfix="_d" extension="dll">

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