Implemented severity level display on log file output

This commit is contained in:
2016-07-01 18:37:55 +01:00
parent 8f10655106
commit d1d9c9d8f7
2 changed files with 10 additions and 30 deletions
+2 -9
View File
@@ -3,6 +3,7 @@
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/log/trivial.hpp>
class Logger {
public:
@@ -16,16 +17,8 @@ class Logger {
private:
static void formatter(boost::log::record_view const& rec, boost::log::formatting_ostream& strm);
const char* severity_levels[5] =
{
"normal",
"notification",
"warning",
"error",
"critical"
};
typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> text_sink;
boost::log::sources::severity_logger<int> lg;
boost::log::sources::severity_logger< boost::log::trivial::severity_level > lg;
boost::shared_ptr<text_sink> sink;
};
+8 -21
View File
@@ -21,12 +21,13 @@ namespace keywords = boost::log::keywords;
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int)
Logger::Logger() {
boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
Logger::sink = boost::make_shared<text_sink>();
boost::shared_ptr<std::ostream> stdout_stream{&std::clog, boost::null_deleter{}};
Logger::sink->locked_backend()->add_stream(stdout_stream);
/*
add_file_log
(
keywords::file_name = "concatenator_%N.log",
@@ -36,12 +37,11 @@ Logger::Logger() {
"[" <<
expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") <<
"] [" <<
Logger::severity_levels[severity] <<
"] --- "
logging::trivial::severity <<
"] --- " <<
expr::smessage
)
);
*/
Logger::sink->set_formatter(&Logger::formatter);
logging::add_common_attributes();
Logger::sink->set_filter(severity > 0);
@@ -50,31 +50,18 @@ Logger::Logger() {
};
void Logger::formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
{
// Get the LineID attribute value and put it into the stream
strm << logging::extract< unsigned int >("LineID", rec) << ": ";
// The same for the severity level.
// The simplified syntax is possible if attribute keywords are used.
strm << "<" << rec[logging::trivial::severity] << "> ";
// Finally, put the record message to the stream
strm << rec[expr::smessage];
}
void Logger::error(std::string str) {
BOOST_LOG_SEV(lg, 2) << str;
BOOST_LOG_SEV(lg, logging::trivial::error) << str;
Logger::sink->flush();
}
void Logger::info(std::string str) {
BOOST_LOG_SEV(lg, 0) << str;
BOOST_LOG_SEV(lg, logging::trivial::error) << str;
Logger::sink->flush();
}
void Logger::warning(std::string str) {
BOOST_LOG_SEV(lg, 1) << str;
BOOST_LOG_SEV(lg, logging::trivial::error) << str;
Logger::sink->flush();
}