[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 |
|
---|
[239] | 33 | // specialization for char
|
---|
| 34 | template <>
|
---|
| 35 | PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s)
|
---|
[89] | 36 | {
|
---|
[141] | 37 | strm << s.toStdString();
|
---|
| 38 | return strm;
|
---|
[89] | 39 | }
|
---|
| 40 |
|
---|
[239] | 41 | // specialization for wchar_t
|
---|
| 42 | template <>
|
---|
| 43 | PACPUSLIB_API std::basic_ostream<wchar_t>& operator<< (std::basic_ostream<wchar_t>& strm, QString const& s)
|
---|
| 44 | {
|
---|
| 45 | strm << s.toStdWString();
|
---|
| 46 | return strm;
|
---|
| 47 | }
|
---|
[89] | 48 |
|
---|
[231] | 49 | namespace pacpus
|
---|
| 50 | {
|
---|
[236] | 51 |
|
---|
| 52 | BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel)
|
---|
| 53 |
|
---|
[142] | 54 | void init_log_factories()
|
---|
| 55 | {
|
---|
[143] | 56 | boost::log::register_simple_formatter_factory< QString, char >("QString");
|
---|
[142] | 57 | }
|
---|
[89] | 58 |
|
---|
[143] | 59 | static int niftyCounter;
|
---|
[89] | 60 |
|
---|
| 61 | LogConfigurator::LogConfigurator()
|
---|
| 62 | {
|
---|
[143] | 63 | if (0 == niftyCounter++) {
|
---|
[146] | 64 | LOG_INFO("LogConfigurator constructor");
|
---|
[143] | 65 | init_log_factories();
|
---|
| 66 | }
|
---|
[89] | 67 | }
|
---|
| 68 |
|
---|
[143] | 69 | LogConfigurator::~LogConfigurator()
|
---|
[89] | 70 | {
|
---|
[143] | 71 | if (0 == --niftyCounter) {
|
---|
| 72 | // clean up
|
---|
[146] | 73 | LOG_INFO("LogConfigurator destructor");
|
---|
[143] | 74 | }
|
---|
[89] | 75 | }
|
---|
| 76 |
|
---|
[228] | 77 | void LogConfigurator::configureLoggerWithFile(const char* logFileName)
|
---|
[143] | 78 | {
|
---|
| 79 | using namespace boost;
|
---|
| 80 |
|
---|
| 81 | namespace logging = boost::log;
|
---|
| 82 | namespace sinks = boost::log::sinks;
|
---|
| 83 | namespace src = boost::log::sources;
|
---|
| 84 | namespace expr = boost::log::expressions;
|
---|
| 85 | namespace attrs = boost::log::attributes;
|
---|
| 86 | namespace keywords = boost::log::keywords;
|
---|
| 87 |
|
---|
| 88 | logging::add_common_attributes();
|
---|
[176] | 89 | logging::core::get()->add_global_attribute(
|
---|
| 90 | "ProcessID",
|
---|
| 91 | attrs::current_process_id());
|
---|
| 92 | logging::core::get()->add_global_attribute(
|
---|
| 93 | "ThreadID",
|
---|
| 94 | attrs::current_thread_id());
|
---|
[236] | 95 | //logging::core::get()->add_global_attribute(
|
---|
| 96 | // "Scope",
|
---|
| 97 | // attrs::named_scope());
|
---|
[176] | 98 |
|
---|
[143] | 99 | logging::core::get()->set_filter
|
---|
| 100 | (
|
---|
[146] | 101 | #ifdef NDEBUG
|
---|
| 102 | // release
|
---|
[236] | 103 | severity >= debug
|
---|
[146] | 104 | #else
|
---|
| 105 | // debug
|
---|
[236] | 106 | severity >= trace
|
---|
[146] | 107 | #endif
|
---|
[143] | 108 | );
|
---|
| 109 |
|
---|
[236] | 110 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 111 | // FILE
|
---|
[143] | 112 | logging::add_file_log
|
---|
| 113 | (
|
---|
| 114 | keywords::file_name = logFileName,
|
---|
| 115 | keywords::rotation_size = 10 * 1024 * 1024,
|
---|
| 116 | keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
|
---|
| 117 | //keywords::format = "%LineID% [%TimeStamp%]: %Message%"
|
---|
| 118 | keywords::format =
|
---|
| 119 | (
|
---|
| 120 | expr::stream
|
---|
| 121 | << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
|
---|
[176] | 122 | //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
|
---|
[239] | 123 | << " [" << boost::log::expressions::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
|
---|
[236] | 124 | //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
|
---|
| 125 | << " <" << severity << ">"
|
---|
[176] | 126 | << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
|
---|
| 127 | << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
|
---|
| 128 | << " " << expr::smessage
|
---|
[143] | 129 | )
|
---|
| 130 | );
|
---|
[236] | 131 |
|
---|
| 132 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 133 | // CONSOLE
|
---|
[239] | 134 | // Create a backend and attach a couple of streams to it
|
---|
| 135 | #if ! (BOOST_VERSION >= 105500)
|
---|
[236] | 136 | using logging::empty_deleter;
|
---|
| 137 | #endif
|
---|
| 138 | boost::shared_ptr< sinks::text_ostream_backend > backend =
|
---|
| 139 | make_shared< sinks::text_ostream_backend >();
|
---|
[143] | 140 | backend->add_stream(
|
---|
[225] | 141 | shared_ptr< std::ostream >(&std::clog, empty_deleter())
|
---|
[143] | 142 | );
|
---|
| 143 |
|
---|
| 144 | // Enable auto-flushing after each log record written
|
---|
| 145 | backend->auto_flush(true);
|
---|
| 146 |
|
---|
| 147 | // Wrap it into the frontend and register in the core.
|
---|
| 148 | // The backend requires synchronization in the frontend.
|
---|
| 149 | typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
|
---|
| 150 | shared_ptr< sink_t > sink(new sink_t(backend));
|
---|
| 151 | sink->set_filter
|
---|
| 152 | (
|
---|
[236] | 153 | severity >= info
|
---|
[143] | 154 | );
|
---|
[146] | 155 |
|
---|
[143] | 156 | sink->set_formatter
|
---|
| 157 | (
|
---|
| 158 | expr::stream
|
---|
[239] | 159 | #if defined(PACPUS_LOG_COLORED_OUTPUT)
|
---|
| 160 | << formatSeverityWithColors< SeverityLevel >("Severity")
|
---|
[236] | 161 | #endif
|
---|
[143] | 162 | << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
|
---|
| 163 | //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] "
|
---|
| 164 | << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "] "
|
---|
[236] | 165 | //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
|
---|
| 166 | << "<" << severity << ">"
|
---|
[143] | 167 | << " "
|
---|
| 168 | << expr::smessage
|
---|
[239] | 169 | #if defined(PACPUS_LOG_COLORED_OUTPUT)
|
---|
| 170 | << formatSeverityWithColors< SeverityLevel >("Severity", /*restoreDefault=*/ true) // Resets the terminal to default.
|
---|
[236] | 171 | #endif
|
---|
[143] | 172 | );
|
---|
| 173 |
|
---|
| 174 | logging::core::get()->add_sink(sink);
|
---|
| 175 |
|
---|
[144] | 176 | LOG_INFO("logger initialised");
|
---|
[143] | 177 | }
|
---|
| 178 |
|
---|
[89] | 179 | } // namespace pacpus
|
---|
[143] | 180 |
|
---|
| 181 | #else // PACPUS_USE_LOG
|
---|
| 182 |
|
---|
| 183 | namespace pacpus
|
---|
[231] | 184 | {
|
---|
[143] | 185 |
|
---|
| 186 | LogConfigurator::LogConfigurator()
|
---|
| 187 | {}
|
---|
[231] | 188 |
|
---|
| 189 | LogConfigurator::~LogConfigurator()
|
---|
[143] | 190 | {}
|
---|
| 191 |
|
---|
[231] | 192 | void LogConfigurator::configureLoggerWithFile(const char* /*configFilename*/)
|
---|
| 193 | {}
|
---|
| 194 |
|
---|
[143] | 195 | } // namespace pacpus
|
---|
| 196 |
|
---|
| 197 | #endif // PACPUS_USE_LOG
|
---|
[231] | 198 |
|
---|