-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.cpp
More file actions
61 lines (50 loc) · 2.19 KB
/
log.cpp
File metadata and controls
61 lines (50 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "log.hpp"
#include <boost/log/core.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <fstream>
#include <ostream>
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<severity_level> 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<sinks::text_ostream_backend> text_sink;
boost::shared_ptr<text_sink> sink = boost::make_shared<text_sink>();
// add stream for writing to file
sink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(path));
// add stream for writing to console
sink->locked_backend()->add_stream(
boost::shared_ptr<std::ostream>(&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);
}