In the process of adding a log file options to the logger module - end of day commit

This commit is contained in:
2016-06-30 22:29:07 +01:00
parent 81cb1a5f60
commit 8f10655106
2 changed files with 47 additions and 6 deletions
+12
View File
@@ -1,6 +1,8 @@
#include <boost/shared_ptr.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
class Logger {
public:
@@ -13,6 +15,16 @@ class Logger {
void critical(std::string str);
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::shared_ptr<text_sink> sink;
+35 -6
View File
@@ -1,15 +1,22 @@
#include <boost/log/common.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/log/trivial.hpp>
#include <iostream>
#include "Logger.h"
using namespace boost::log;
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int)
@@ -19,21 +26,43 @@ Logger::Logger() {
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 = "sample_%N.log",
keywords::file_name = "concatenator_%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::format = "[%(asctime)s] [%(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s) %Y-%m-%d %H:%M:%S"
keywords::format = (
expr::stream <<
"[" <<
expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") <<
"] [" <<
Logger::severity_levels[severity] <<
"] --- "
)
);
*/
Logger::sink->set_formatter(&Logger::formatter);
logging::add_common_attributes();
Logger::sink->set_filter(severity > 0);
Logger::sink->set_formatter(expressions::stream << severity << ": " <<
expressions::smessage);
core::get()->add_sink(Logger::sink);
logging::core::get()->add_sink(Logger::sink);
};
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;
Logger::sink->flush();