Consider FAIR_MIN_SEVERITY when setting severities

This commit is contained in:
Alexey Rybalchenko 2020-07-10 13:55:23 +02:00
parent 1253bbbac8
commit 35ebc10204

View File

@ -26,9 +26,14 @@ using VSpec = VerbositySpec;
bool Logger::fColored = false; bool Logger::fColored = false;
fstream Logger::fFileStream; fstream Logger::fFileStream;
Verbosity Logger::fVerbosity = Verbosity::low; Verbosity Logger::fVerbosity = Verbosity::low;
#ifdef FAIR_MIN_SEVERITY
Severity Logger::fConsoleSeverity = Severity::FAIR_MIN_SEVERITY > Severity::info ? Severity::FAIR_MIN_SEVERITY : Severity::info;
Severity Logger::fMinSeverity = Severity::FAIR_MIN_SEVERITY > Severity::info ? Severity::FAIR_MIN_SEVERITY : Severity::info;
#else
Severity Logger::fConsoleSeverity = Severity::info; Severity Logger::fConsoleSeverity = Severity::info;
Severity Logger::fFileSeverity = Severity::nolog;
Severity Logger::fMinSeverity = Severity::info; Severity Logger::fMinSeverity = Severity::info;
#endif
Severity Logger::fFileSeverity = Severity::nolog;
function<void()> Logger::fFatalCallback; function<void()> Logger::fFatalCallback;
unordered_map<string, pair<Severity, function<void(const string& content, const LogMetaData& metadata)>>> Logger::fCustomSinks; unordered_map<string, pair<Severity, function<void(const string& content, const LogMetaData& metadata)>>> Logger::fCustomSinks;
mutex Logger::fMtx; mutex Logger::fMtx;
@ -296,6 +301,12 @@ string Logger::GetColoredSeverityString(Severity severity)
void Logger::SetConsoleSeverity(const Severity severity) void Logger::SetConsoleSeverity(const Severity severity)
{ {
#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<int>(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl;
return;
}
#endif
fConsoleSeverity = severity; fConsoleSeverity = severity;
UpdateMinSeverity(); UpdateMinSeverity();
} }
@ -317,6 +328,12 @@ Severity Logger::GetConsoleSeverity()
void Logger::SetFileSeverity(const Severity severity) void Logger::SetFileSeverity(const Severity severity)
{ {
#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<int>(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl;
return;
}
#endif
fFileSeverity = severity; fFileSeverity = severity;
UpdateMinSeverity(); UpdateMinSeverity();
} }
@ -333,6 +350,12 @@ void Logger::SetFileSeverity(const string& severityStr)
void Logger::SetCustomSeverity(const string& key, const Severity severity) void Logger::SetCustomSeverity(const string& key, const Severity severity)
{ {
#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<int>(Severity::FAIR_MIN_SEVERITY)) << "), ignoring" << endl;
return;
}
#endif
fCustomSinks.at(key).first = severity; // TODO: range checks fCustomSinks.at(key).first = severity; // TODO: range checks
UpdateMinSeverity(); UpdateMinSeverity();
} }
@ -514,7 +537,16 @@ string Logger::InitFileSink(const Severity severity, const string& filename, boo
fFileStream.open(fullName, fstream::out | fstream::app); fFileStream.open(fullName, fstream::out | fstream::app);
if (fFileStream.is_open()) { if (fFileStream.is_open()) {
#ifdef FAIR_MIN_SEVERITY
if (severity < Severity::FAIR_MIN_SEVERITY && severity != Severity::nolog) {
cout << "Requested file sink severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << fSeverityNames.at(static_cast<int>(Severity::FAIR_MIN_SEVERITY)) << "), setting to " << fSeverityNames.at(static_cast<int>(Severity::FAIR_MIN_SEVERITY)) << endl;
fFileSeverity = Severity::FAIR_MIN_SEVERITY;
} else {
fFileSeverity = severity;
}
#else
fFileSeverity = severity; fFileSeverity = severity;
#endif
UpdateMinSeverity(); UpdateMinSeverity();
} else { } else {
cout << "Error opening file: " << fullName; cout << "Error opening file: " << fullName;
@ -573,7 +605,16 @@ void Logger::AddCustomSink(const string& key, Severity severity, function<void(c
{ {
lock_guard<mutex> lock(fMtx); lock_guard<mutex> lock(fMtx);
if (fCustomSinks.count(key) == 0) { if (fCustomSinks.count(key) == 0) {
#ifdef FAIR_MIN_SEVERITY
if (severity < Severity::FAIR_MIN_SEVERITY && severity != Severity::nolog) {
cout << "Requested custom sink severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << fSeverityNames.at(static_cast<int>(Severity::FAIR_MIN_SEVERITY)) << "), setting to " << fSeverityNames.at(static_cast<int>(Severity::FAIR_MIN_SEVERITY)) << endl;
fCustomSinks.insert(make_pair(key, make_pair(Severity::FAIR_MIN_SEVERITY, func)));
} else {
fCustomSinks.insert(make_pair(key, make_pair(severity, func)));
}
#else
fCustomSinks.insert(make_pair(key, make_pair(severity, func))); fCustomSinks.insert(make_pair(key, make_pair(severity, func)));
#endif
UpdateMinSeverity(); UpdateMinSeverity();
} else { } else {
cout << "Logger::AddCustomSink: sink '" << key << "' already exists, will not add again. Remove first with Logger::RemoveCustomSink(const string& key)" << endl; cout << "Logger::AddCustomSink: sink '" << key << "' already exists, will not add again. Remove first with Logger::RemoveCustomSink(const string& key)" << endl;