Add getters for file & custom sink severity

This commit is contained in:
Alexey Rybalchenko 2020-07-13 11:24:11 +02:00
parent de1014dabb
commit 1cb941021c
3 changed files with 41 additions and 6 deletions

View File

@ -350,14 +350,19 @@ void Logger::SetFileSeverity(const string& severityStr)
void Logger::SetCustomSeverity(const string& key, const Severity severity) void Logger::SetCustomSeverity(const string& key, const Severity severity)
{ {
try {
#ifdef FAIR_MIN_SEVERITY #ifdef FAIR_MIN_SEVERITY
if (severity < Severity::FAIR_MIN_SEVERITY && severity != Severity::nolog) { 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<int>(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl; cout << "Requested severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << fSeverityNames.at(static_cast<int>(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl;
return; return;
} }
#endif #endif
fCustomSinks.at(key).first = severity; // TODO: range checks fCustomSinks.at(key).first = severity; // TODO: range checks
UpdateMinSeverity(); 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) 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() void Logger::CycleConsoleSeverityUp()
{ {
int current = static_cast<int>(fConsoleSeverity); int current = static_cast<int>(fConsoleSeverity);

View File

@ -244,9 +244,11 @@ class Logger
static void SetFileSeverity(const Severity severity); static void SetFileSeverity(const Severity severity);
static void SetFileSeverity(const std::string& severityStr); 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 Severity severity);
static void SetCustomSeverity(const std::string& key, const std::string& severityStr); static void SetCustomSeverity(const std::string& key, const std::string& severityStr);
static Severity GetCustomSeverity(const std::string& key);
static void CycleConsoleSeverityUp(); static void CycleConsoleSeverityUp();
static void CycleConsoleSeverityDown(); static void CycleConsoleSeverityDown();

View File

@ -42,6 +42,10 @@ int main()
uniform_int_distribution<> distrib(1, 65536); uniform_int_distribution<> distrib(1, 65536);
string name = Logger::InitFileSink(Severity::warn, string("test_log_" + to_string(distrib(gen))), true); 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<int>(Logger::GetFileSeverity())), ") does not match the expected one (", Logger::fSeverityNames.at(static_cast<int>(Severity::warn)), ")"));
}
CheckOutput("^\\[FATAL] fatal\n$", [](){ CheckOutput("^\\[FATAL] fatal\n$", [](){
LOG(state) << "state"; LOG(state) << "state";
LOG(warn) << "warning"; 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<int>(Logger::GetCustomSeverity("CustomSink"))), ") does not match the expected one (", Logger::fSeverityNames.at(static_cast<int>(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$", [](){ CheckOutput("^CustomSink warning\nCustomSink error\nCustomSink fatal\n\\[FATAL] fatal\n$", [](){
LOG(state) << "state"; LOG(state) << "state";
LOG(warn) << "warning"; LOG(warn) << "warning";