Changeset 143 in pacpusframework for branches/2.0-beta1/src/PacpusLib


Ignore:
Timestamp:
Jul 30, 2013, 6:16:04 PM (11 years ago)
Author:
Marek Kurdej
Message:

Update: Logger initialization invoked in DbitePlayer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0-beta1/src/PacpusLib/Log.cpp

    r142 r143  
    99#ifdef PACPUS_USE_LOG
    1010
     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>
    1122#include <ostream>
    1223#include <QString>
     
    2334PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s);
    2435
    25 #endif // PACPUS_USE_LOG
    26 
    2736namespace pacpus {
    2837   
    29 #if defined(PACPUS_USE_LOG)
    30 
    31 //#include <boost/log/utility/setup/formatter_parser.hpp>
    32 
    3338void init_log_factories()
    3439{
    35     //boost::log::register_simple_formatter_factory< QString, char >("QString");
     40    boost::log::register_simple_formatter_factory< QString, char >("QString");
    3641}
    3742
    38 #endif // PACPUS_USE_LOG
     43static int niftyCounter;
    3944
    4045LogConfigurator::LogConfigurator()
    4146{
    42 #if defined(PACPUS_USE_LOG)
    43     init_log_factories();
    44 #endif // PACPUS_USE_LOG
     47    if (0 == niftyCounter++) {
     48        init_log_factories();
     49    }
    4550}
    4651
    47 void LogConfigurator::configureLoggerWithFile(const char * /*configFilename*/)
     52LogConfigurator::~LogConfigurator()
    4853{
     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    BOOST_LOG_TRIVIAL(info) << "logger initialised";
    49129}
    50130
    51131} // 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 TracChangeset for help on using the changeset viewer.