#include "log.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace logging = boost::log; namespace src = boost::log::sources; namespace expr = boost::log::expressions; namespace sinks = boost::log::sinks; namespace attrs = boost::log::attributes; BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime) BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level); BOOST_LOG_GLOBAL_LOGGER_INIT(logger, src::severity_logger_mt) { src::severity_logger_mt logger; // add attribute: each log line gets a timestamp logger.add_attribute("TimeStamp", attrs::local_clock()); return logger; } void init_logging(const std::string &path) { // Construct the sink typedef sinks::synchronous_sink text_sink; boost::shared_ptr sink = boost::make_shared(); // add stream for writing to file sink->locked_backend()->add_stream(boost::make_shared(path)); // add stream for writing to console sink->locked_backend()->add_stream( boost::shared_ptr(&std::clog, boost::null_deleter())); // specify format of log message logging::formatter formatter = expr::stream << expr::format_date_time( timestamp, "%Y-%m-%d %H:%M:%S.%f") << " " << "[" << severity << "]" << " " << expr::smessage; sink->set_formatter(formatter); // only messages with severity >= SEVERITY_THRESHOLD are written sink->set_filter(severity <= verbosity); // Register the sink in the logging core logging::core::get()->add_sink(sink); }