Repository navigation
Expand file tree
/
Copy pathlogging.cpp
More file actions
68 lines (55 loc) · 2.4 KB
/
Copy pathlogging.cpp
File metadata and controls
68 lines (55 loc) · 2.4 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
62
63
64
65
66
67
68
#include "pch.h"
#include "logging.h"
#include <fstream>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <boost/core/null_deleter.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/expressions.hpp>
#include <boost/ref.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/thread/thread.hpp>
namespace attrs = boost::log::attributes;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
namespace fms
{
BOOST_LOG_GLOBAL_LOGGER_DEFAULT(lg, src::logger_mt)
void logging::init_logging(const std::string &path)
{
// The pattern has to carry the directory: file_name places the file being
// written, while the collector target below only governs rotated files.
boost::filesystem::path pattern(path);
pattern /= "%Y%m%d_%H%M%S_%5N.log";
backend_ptr const bp = boost::make_shared<backend_t>(keywords::file_name = pattern,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0));
bp->auto_flush(true);
m_sink = boost::make_shared<sink_t>(bp,
// We'll apply record ordering to ensure that records from different threads go sequentially in the file
keywords::order = boost::log::make_attr_ordering<unsigned int>("RecordID", std::less<unsigned int>()));
m_sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = path, // where to store rotated files
keywords::min_free_space = 100 * 1024 * 1024, // minimum free space on the drive, in bytes
keywords::max_size = 100 * 1024 * 1024 // oldest log files will be removed if the total size reaches 100 Mb
));
m_sink->locked_backend()->scan_for_files();
m_sink->set_formatter(
expr::format("%1%: [%2%] [%3%] - %4%")
% expr::attr<unsigned int>("RecordID")
% expr::attr<boost::posix_time::ptime>("TimeStamp")
% expr::attr<boost::thread::id>("ThreadID")
% expr::smessage
);
// Add it to the core
boost::log::core::get()->add_sink(m_sink);
// Add some attributes too
boost::log::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
boost::log::core::get()->add_global_attribute("RecordID", attrs::counter<unsigned int>());
boost::log::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
m_created = true;
}
}