Filter log messages by severity

This commit is contained in:
Simon Fels 2017-01-26 09:19:28 +01:00
commit 6887a4a0ef
2 changed files with 16 additions and 15 deletions

View file

@ -30,8 +30,7 @@
namespace { namespace {
namespace attrs { namespace attrs {
BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "anbox::Severity", BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "anbox::Severity", anbox::Logger::Severity)
anbox::Logger::Severity)
BOOST_LOG_ATTRIBUTE_KEYWORD(Location, "Location", anbox::Logger::Location) BOOST_LOG_ATTRIBUTE_KEYWORD(Location, "Location", anbox::Logger::Location)
BOOST_LOG_ATTRIBUTE_KEYWORD(Timestamp, "Timestamp", boost::posix_time::ptime) 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 { struct BoostLogLogger : public anbox::Logger {
BoostLogLogger() : initialized_(false) {} BoostLogLogger() : initialized_(false) {}
void Init(const anbox::Logger::Severity& severity = void Init(const anbox::Logger::Severity& severity = anbox::Logger::Severity::kWarning) override {
anbox::Logger::Severity::kWarning) override {
if (initialized_) return; if (initialized_) return;
boost::log::formatter formatter = boost::log::formatter formatter =
@ -58,25 +56,24 @@ struct BoostLogLogger : public anbox::Logger {
auto logger = boost::log::add_console_log(std::cout); auto logger = boost::log::add_console_log(std::cout);
logger->set_formatter(formatter); logger->set_formatter(formatter);
// FIXME need to enable this once we found how we wrap this severity_ = severity;
// properly into our service architecture. For now left as
// it is.
boost::ignore_unused_variable_warning(severity);
// logger->set_filter(attrs::Severity < severity);
initialized_ = true; initialized_ = true;
} }
void Log(Severity severity, const std::string& message, void Log(Severity severity, const std::string& message, const boost::optional<Location>& loc) override {
const boost::optional<Location>& loc) {
if (!initialized_) Init(); 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()) { if (auto rec = boost::log::trivial::logger::get().open_record()) {
boost::log::record_ostream out{rec}; boost::log::record_ostream out{rec};
out << boost::log::add_value(attrs::Severity, severity) out << boost::log::add_value(attrs::Severity, severity)
<< boost::log::add_value( << boost::log::add_value(attrs::Timestamp, boost::posix_time::microsec_clock::universal_time())
attrs::Timestamp,
boost::posix_time::microsec_clock::universal_time())
<< message; << message;
if (loc) { if (loc) {
@ -91,6 +88,7 @@ struct BoostLogLogger : public anbox::Logger {
} }
private: private:
Severity severity_;
bool initialized_; bool initialized_;
}; };

View file

@ -49,6 +49,9 @@ class Logger : public DoNotCopyOrMove {
virtual void Init(const Severity& severity = Severity::kWarning) = 0; 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, virtual void Log(Severity severity, const std::string& message,
const boost::optional<Location>& location) = 0; const boost::optional<Location>& location) = 0;