13 #include <boost/log/core.hpp> 14 #include <boost/log/trivial.hpp> 15 #include <boost/log/expressions.hpp> 16 #include <boost/log/sinks/text_file_backend.hpp> 17 #include <boost/log/utility/setup/file.hpp> 18 #include <boost/log/utility/setup/common_attributes.hpp> 19 #include <boost/log/sources/severity_logger.hpp> 20 #include <boost/log/sources/record_ostream.hpp> 21 #include <boost/log/attributes/attribute.hpp> 22 #include <boost/log/attributes/attribute.hpp> 23 #include <boost/log/attributes/attribute_cast.hpp> 24 #include <boost/log/attributes/attribute_value.hpp> 25 #include <boost/lambda/lambda.hpp> 26 #include <boost/date_time/posix_time/posix_time.hpp> 27 #include <boost/log/common.hpp> 28 #include <boost/log/expressions.hpp> 29 #include <boost/date_time/posix_time/posix_time_types.hpp> 30 #include <boost/log/support/date_time.hpp> 34 namespace logging = boost::log;
35 namespace src = boost::log::sources;
36 namespace sinks = boost::log::sinks;
37 namespace keywords = boost::log::keywords;
38 namespace attrs = boost::log::attributes;
39 namespace expr = boost::log::expressions;
45 boost::shared_ptr< logging::core > core = logging::core::get();
46 if (sev_level ==
"debug")
50 logging::trivial::severity >= logging::trivial::debug
53 if (sev_level ==
"info")
57 logging::trivial::severity >= logging::trivial::info
60 if (sev_level ==
"warning")
64 logging::trivial::severity >= logging::trivial::warning
67 if (sev_level ==
"error")
71 logging::trivial::severity >= logging::trivial::error
74 if (sev_level ==
"fatal")
78 logging::trivial::severity >= logging::trivial::fatal
83 boost::shared_ptr< sinks::text_file_backend > backend =
84 boost::make_shared< sinks::text_file_backend >(
85 keywords::file_name =
"sample_%5N.log",
86 keywords::rotation_size = 5 * 1024 * 1024,
87 keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0)
91 backend->auto_flush(
true);
93 typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
94 boost::shared_ptr<sink_t> sink(
new sink_t(backend));
97 logging::formatter formatter = expr::stream
98 <<expr::format_date_time<boost::posix_time::ptime >(
"TimeStamp",
"%-%M:%S.%f")
99 <<
" ["<<logging::expressions::attr<logging::attributes::current_thread_id::value_type>(
"ThreadID")<<
"]:" 101 sink->set_formatter(formatter);
102 core->add_sink(sink);
103 logging::add_common_attributes();
107 using namespace logging::trivial;
108 src::severity_logger< severity_level >
lg;
122 std::map<std::thread::id, std::string>
spacers;
126 std::string spacer();
139 std::thread::id t_id = std::this_thread::get_id();
140 if (spacers.find(t_id) == spacers.end())
142 spacers[t_id] +=
" ";
155 std::thread::id t_id = std::this_thread::get_id();
156 if (spacers.find(t_id) == spacers.end())
158 if (spacers[t_id].empty())
160 spacers[t_id].erase(0,2);
168 std::thread::id t_id = std::this_thread::get_id();
169 if (spacers.find(t_id) == spacers.end())
171 return spacers[t_id];
181 std::lock_guard<std::mutex> lg1(lgSpacer.
lgmutex);
std::string spacer()
returns white-space string corresponding to invoking thread
void init_logging(std::string sev_level)
sets up logging using Boost.Log library
void delSpace()
deletes one white-space from white-space string corresponding to invoking thread
void addSpace()
adds one white-space to white-space string corresponding to invoking thread
std::map< std::thread::id, std::string > spacers
src::severity_logger< severity_level > lg
std::ostream & operator<<(std::ostream &ofs, logSpacer &lgSpacer)
pushes white-space string corresponding to invoking thread to std::ostream
enables indentation of logs for easy viewing