-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.cpp
executable file
·104 lines (93 loc) · 3.87 KB
/
trace.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "trace.h"
#include "Isis.h"
#include "Config.h"
#include <cstddef>
#include <iostream>
#include <string>
#include <cstdarg>
#include <sstream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/attributes/value_extraction.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/utility/empty_deleter.hpp>
#include <boost/filesystem.hpp>
//------------------------------------------------------------------------------
using namespace isis;
namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > file_sink;
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > ostream_sink;
typedef boost::log::trivial::severity_level priority;
typedef boost::log::sources::severity_channel_logger_mt<boost::log::trivial::severity_level, std::string> Logger;
trace::trace() {
addStdSink();
addFileSink();
logging::add_common_attributes();
}
trace::~trace() {
logging::core::get()->remove_all_sinks();
}
/**
* Helper to format all streams appropriately
*/
template <class T>
void trace::setSinkFormat(T* SinkToFormat){
(*SinkToFormat)->set_formatter(expr::stream
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<< " [" << expr::attr< std::string >("Channel")
<< "] " << logging::trivial::severity
<< ": " << expr::smessage
);
}
/**
* Helper to add an std stream based backend sink
*/
void trace::addStdSink()
{
boost::shared_ptr< sinks::text_ostream_backend > StdBackend =
boost::make_shared< sinks::text_ostream_backend >();
boost::shared_ptr< std::ostream > StdStream(&std::clog, boost::empty_deleter()); //create cout sink
boost::shared_ptr< ostream_sink > StdSink (new ostream_sink(StdBackend));
StdSink->locked_backend()->add_stream(StdStream);
StdBackend->auto_flush(true);
setSinkFormat(&StdSink);
StdSink->set_filter(boost::log::trivial::severity >= Isis::config->verbose);
logging::core::get()->add_sink(StdSink);
}
/**
* Helper to add a file based backend sink
*/
void trace::addFileSink()
{
boost::shared_ptr<sinks::text_file_backend> file_backend =
boost::make_shared<sinks::text_file_backend>(
keywords::file_name = Isis::config->logPath + "/" + LOG_PREFIX + "-%Y-%m-%d_%H-%M-%S.%N.log",
keywords::rotation_size = 5 * 1024 * 1024
);
boost::shared_ptr<file_sink> FileSink(new file_sink(file_backend));
FileSink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = Isis::config->logPath, /*< the target directory >*/
keywords::max_size = 20 * 1024 * 1024, /*< maximum total size of the stored files, in bytes >*/
keywords::min_free_space = 100 * 1024 * 1024 /*< minimum free space on the drive, in bytes >*/
));
FileSink->locked_backend()->scan_for_files();// sinks::file::scan_all for all in folder
setSinkFormat(&FileSink);
FileSink->set_filter(boost::log::trivial::severity >= Isis::config->verbose);
file_backend->auto_flush(true);
logging::core::get()->add_sink(FileSink);
}
//------------------------------------------------------------------------------