From 6887a4a0efd7feb8fa31b46337ac170038bc0020 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Thu, 26 Jan 2017 09:19:28 +0100 Subject: [PATCH] Filter log messages by severity --- src/anbox/logger.cpp | 28 +++++++++++++--------------- src/anbox/logger.h | 3 +++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/anbox/logger.cpp b/src/anbox/logger.cpp index c2c2aed..bb92783 100644 --- a/src/anbox/logger.cpp +++ b/src/anbox/logger.cpp @@ -30,8 +30,7 @@ namespace { namespace attrs { -BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "anbox::Severity", - anbox::Logger::Severity) +BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "anbox::Severity", anbox::Logger::Severity) BOOST_LOG_ATTRIBUTE_KEYWORD(Location, "Location", anbox::Logger::Location) BOOST_LOG_ATTRIBUTE_KEYWORD(Timestamp, "Timestamp", boost::posix_time::ptime) } @@ -39,8 +38,7 @@ BOOST_LOG_ATTRIBUTE_KEYWORD(Timestamp, "Timestamp", boost::posix_time::ptime) struct BoostLogLogger : public anbox::Logger { BoostLogLogger() : initialized_(false) {} - void Init(const anbox::Logger::Severity& severity = - anbox::Logger::Severity::kWarning) override { + void Init(const anbox::Logger::Severity& severity = anbox::Logger::Severity::kWarning) override { if (initialized_) return; boost::log::formatter formatter = @@ -58,25 +56,24 @@ struct BoostLogLogger : public anbox::Logger { auto logger = boost::log::add_console_log(std::cout); logger->set_formatter(formatter); - // FIXME need to enable this once we found how we wrap this - // properly into our service architecture. For now left as - // it is. - boost::ignore_unused_variable_warning(severity); - // logger->set_filter(attrs::Severity < severity); - + severity_ = severity; initialized_ = true; } - void Log(Severity severity, const std::string& message, - const boost::optional& loc) { + void Log(Severity severity, const std::string& message, const boost::optional& loc) override { if (!initialized_) Init(); + // FIXME somehow set_filter doesn't work with the trivial logger. If + // we set a filter based on the severity attribute open_record will + // not return a new record. Because of that we do a poor man filtering + // here until we have a proper way to do this via boost. + if (severity < severity_) + return; + if (auto rec = boost::log::trivial::logger::get().open_record()) { boost::log::record_ostream out{rec}; out << boost::log::add_value(attrs::Severity, severity) - << boost::log::add_value( - attrs::Timestamp, - boost::posix_time::microsec_clock::universal_time()) + << boost::log::add_value(attrs::Timestamp, boost::posix_time::microsec_clock::universal_time()) << message; if (loc) { @@ -91,6 +88,7 @@ struct BoostLogLogger : public anbox::Logger { } private: + Severity severity_; bool initialized_; }; diff --git a/src/anbox/logger.h b/src/anbox/logger.h index 8f72064..7a177eb 100644 --- a/src/anbox/logger.h +++ b/src/anbox/logger.h @@ -49,6 +49,9 @@ class Logger : public DoNotCopyOrMove { virtual void Init(const Severity& severity = Severity::kWarning) = 0; + void SetSeverity(const std::string &severity); + virtual void SetSeverity(const Severity& severity) = 0; + virtual void Log(Severity severity, const std::string& message, const boost::optional& location) = 0;