source: pacpusframework/branches/0.1.x/src/PacpusLib/Log.cpp@ 308

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

Fixed: Qt4 compilation on Linux

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