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

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

Minor update: Log using LOG_... macro as well.

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