Changeset 143 in pacpusframework for branches/2.0-beta1/src/PacpusLib/Log.cpp
- Timestamp:
- Jul 30, 2013, 6:16:04 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.0-beta1/src/PacpusLib/Log.cpp
r142 r143 9 9 #ifdef PACPUS_USE_LOG 10 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> 11 22 #include <ostream> 12 23 #include <QString> … … 23 34 PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s); 24 35 25 #endif // PACPUS_USE_LOG26 27 36 namespace pacpus { 28 37 29 #if defined(PACPUS_USE_LOG)30 31 //#include <boost/log/utility/setup/formatter_parser.hpp>32 33 38 void init_log_factories() 34 39 { 35 //boost::log::register_simple_formatter_factory< QString, char >("QString");40 boost::log::register_simple_formatter_factory< QString, char >("QString"); 36 41 } 37 42 38 #endif // PACPUS_USE_LOG 43 static int niftyCounter; 39 44 40 45 LogConfigurator::LogConfigurator() 41 46 { 42 #if defined(PACPUS_USE_LOG) 43 init_log_factories();44 #endif // PACPUS_USE_LOG 47 if (0 == niftyCounter++) { 48 init_log_factories(); 49 } 45 50 } 46 51 47 void LogConfigurator::configureLoggerWithFile(const char * /*configFilename*/)52 LogConfigurator::~LogConfigurator() 48 53 { 54 if (0 == --niftyCounter) { 55 // clean up 56 } 57 } 58 59 void 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 BOOST_LOG_TRIVIAL(info) << "logger initialised"; 49 129 } 50 130 51 131 } // namespace pacpus 132 133 #else // PACPUS_USE_LOG 134 135 namespace 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 TracChangeset
for help on using the changeset viewer.