From 1cb941021cf6d092899292164fd4167be5c128cb Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Mon, 13 Jul 2020 11:24:11 +0200 Subject: [PATCH] Add getters for file & custom sink severity --- logger/Logger.cxx | 27 +++++++++++++++++++++------ logger/Logger.h | 2 ++ test/sinks.cxx | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/logger/Logger.cxx b/logger/Logger.cxx index b1255ed..5d0efc2 100644 --- a/logger/Logger.cxx +++ b/logger/Logger.cxx @@ -350,14 +350,19 @@ void Logger::SetFileSeverity(const string& severityStr) void Logger::SetCustomSeverity(const string& key, const Severity severity) { + try { #ifdef FAIR_MIN_SEVERITY - if (severity < Severity::FAIR_MIN_SEVERITY && severity != Severity::nolog) { - cout << "Requested severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << fSeverityNames.at(static_cast(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl; - return; - } + if (severity < Severity::FAIR_MIN_SEVERITY && severity != Severity::nolog) { + cout << "Requested severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << fSeverityNames.at(static_cast(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl; + return; + } #endif - fCustomSinks.at(key).first = severity; // TODO: range checks - UpdateMinSeverity(); + fCustomSinks.at(key).first = severity; // TODO: range checks + UpdateMinSeverity(); + } catch (const out_of_range& oor) { + LOG(error) << "No custom sink with id '" << key << "' found"; + throw; + } } void Logger::SetCustomSeverity(const string& key, const string& severityStr) @@ -370,6 +375,16 @@ void Logger::SetCustomSeverity(const string& key, const string& severityStr) } } +Severity Logger::GetCustomSeverity(const std::string& key) +{ + try { + return fCustomSinks.at(key).first; + } catch (const out_of_range& oor) { + LOG(error) << "No custom sink with id '" << key << "' found"; + throw; + } +} + void Logger::CycleConsoleSeverityUp() { int current = static_cast(fConsoleSeverity); diff --git a/logger/Logger.h b/logger/Logger.h index 24bee56..8856c90 100644 --- a/logger/Logger.h +++ b/logger/Logger.h @@ -244,9 +244,11 @@ class Logger static void SetFileSeverity(const Severity severity); static void SetFileSeverity(const std::string& severityStr); + static Severity GetFileSeverity() { return fFileSeverity; } static void SetCustomSeverity(const std::string& key, const Severity severity); static void SetCustomSeverity(const std::string& key, const std::string& severityStr); + static Severity GetCustomSeverity(const std::string& key); static void CycleConsoleSeverityUp(); static void CycleConsoleSeverityDown(); diff --git a/test/sinks.cxx b/test/sinks.cxx index 0282a9f..2c76a5c 100644 --- a/test/sinks.cxx +++ b/test/sinks.cxx @@ -42,6 +42,10 @@ int main() uniform_int_distribution<> distrib(1, 65536); string name = Logger::InitFileSink(Severity::warn, string("test_log_" + to_string(distrib(gen))), true); + if (Logger::GetFileSeverity() != Severity::warn) { + throw runtime_error(ToStr("File sink severity (", Logger::fSeverityNames.at(static_cast(Logger::GetFileSeverity())), ") does not match the expected one (", Logger::fSeverityNames.at(static_cast(Severity::warn)), ")")); + } + CheckOutput("^\\[FATAL] fatal\n$", [](){ LOG(state) << "state"; LOG(warn) << "warning"; @@ -85,6 +89,20 @@ int main() } }); + if (Logger::GetCustomSeverity("CustomSink") != Severity::warn) { + throw runtime_error(ToStr("File sink severity (", Logger::fSeverityNames.at(static_cast(Logger::GetCustomSeverity("CustomSink"))), ") does not match the expected one (", Logger::fSeverityNames.at(static_cast(Severity::warn)), ")")); + } + + bool oorThrown = false; + try { + Logger::GetCustomSeverity("NonExistentSink"); + } catch (const out_of_range& oor) { + oorThrown = true; + } + if (!oorThrown) { + throw runtime_error("Did not detect a severity request from a non-existent sink"); + } + CheckOutput("^CustomSink warning\nCustomSink error\nCustomSink fatal\n\\[FATAL] fatal\n$", [](){ LOG(state) << "state"; LOG(warn) << "warning";