Add output operator for severities, verbosities

This commit is contained in:
Alexey Rybalchenko 2020-07-13 12:26:49 +02:00
parent 77ed410257
commit 9bdb068105
7 changed files with 33 additions and 31 deletions

View File

@ -303,7 +303,7 @@ void Logger::SetConsoleSeverity(const Severity severity)
{ {
#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 (" << Severity::FAIR_MIN_SEVERITY << "), ignoring" << endl;
return; return;
} }
#endif #endif
@ -330,7 +330,7 @@ void Logger::SetFileSeverity(const Severity severity)
{ {
#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 (" << Severity::FAIR_MIN_SEVERITY << "), ignoring" << endl;
return; return;
} }
#endif #endif
@ -353,11 +353,11 @@ void Logger::SetCustomSeverity(const string& key, const Severity severity)
try { 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 (" << Severity::FAIR_MIN_SEVERITY << "), ignoring" << endl;
return; return;
} }
#endif #endif
fCustomSinks.at(key).first = severity; // TODO: range checks fCustomSinks.at(key).first = severity;
UpdateMinSeverity(); UpdateMinSeverity();
} catch (const out_of_range& oor) { } catch (const out_of_range& oor) {
LOG(error) << "No custom sink with id '" << key << "' found"; LOG(error) << "No custom sink with id '" << key << "' found";
@ -554,7 +554,7 @@ string Logger::InitFileSink(const Severity severity, const string& filename, boo
if (fFileStream.is_open()) { if (fFileStream.is_open()) {
#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 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; cout << "Requested file sink severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << Severity::FAIR_MIN_SEVERITY << "), setting to " << Severity::FAIR_MIN_SEVERITY << endl;
fFileSeverity = Severity::FAIR_MIN_SEVERITY; fFileSeverity = Severity::FAIR_MIN_SEVERITY;
} else { } else {
fFileSeverity = severity; fFileSeverity = severity;
@ -622,7 +622,7 @@ void Logger::AddCustomSink(const string& key, Severity severity, function<void(c
if (fCustomSinks.count(key) == 0) { if (fCustomSinks.count(key) == 0) {
#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 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; cout << "Requested custom sink severity is higher than the enabled compile-time FAIR_MIN_SEVERITY (" << Severity::FAIR_MIN_SEVERITY << "), setting to " << Severity::FAIR_MIN_SEVERITY << endl;
fCustomSinks.insert(make_pair(key, make_pair(Severity::FAIR_MIN_SEVERITY, func))); fCustomSinks.insert(make_pair(key, make_pair(Severity::FAIR_MIN_SEVERITY, func)));
} else { } else {
fCustomSinks.insert(make_pair(key, make_pair(severity, func))); fCustomSinks.insert(make_pair(key, make_pair(severity, func)));

View File

@ -363,6 +363,9 @@ class Logger
static std::map<Verbosity, VerbositySpec> fVerbosities; static std::map<Verbosity, VerbositySpec> fVerbosities;
}; };
inline std::ostream& operator<<(std::ostream& os, const Severity& s) { return os << Logger::SeverityName(s); }
inline std::ostream& operator<<(std::ostream& os, const Verbosity& v) { return os << Logger::VerbosityName(v); }
} // namespace fair } // namespace fair
#define IMP_CONVERTTOSTRING(s) # s #define IMP_CONVERTTOSTRING(s) # s

View File

@ -5,6 +5,7 @@
* GNU Lesser General Public Licence (LGPL) version 3, * * GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
#ifndef FAIR_LOGGER_TEST_COMMON_H #ifndef FAIR_LOGGER_TEST_COMMON_H
#define FAIR_LOGGER_TEST_COMMON_H #define FAIR_LOGGER_TEST_COMMON_H

View File

@ -36,7 +36,7 @@ void printAllVerbositiesWithSeverity(Severity sev)
Logger::SetConsoleSeverity(sev); Logger::SetConsoleSeverity(sev);
for (uint32_t i = 0; i < Logger::fVerbosityNames.size(); ++i) { for (uint32_t i = 0; i < Logger::fVerbosityNames.size(); ++i) {
cout << "##### testing severity '" << Logger::fSeverityNames.at(static_cast<int>(sev)) << "' with verbosity '" << Logger::fVerbosityNames.at(i) << "'" << endl; cout << "##### testing severity '" << sev << "' with verbosity '" << Logger::fVerbosityNames.at(i) << "'" << endl;
Logger::SetVerbosity(static_cast<Verbosity>(i)); Logger::SetVerbosity(static_cast<Verbosity>(i));
printEverySeverity(); printEverySeverity();
} }

View File

@ -7,7 +7,6 @@
********************************************************************************/ ********************************************************************************/
#include "Common.h" #include "Common.h"
#include <Logger.h> #include <Logger.h>
#include <cstdint> #include <cstdint>
@ -48,21 +47,21 @@ void CheckSeverity(Severity severity)
if (sev == Severity::nolog) { if (sev == Severity::nolog) {
if (i == 11) { if (i == 11) {
if (!Logger::Logging(static_cast<Severity>(i))) { if (!Logger::Logging(static_cast<Severity>(i))) {
throw runtime_error(ToStr("expecting to be logging ", Logger::fSeverityNames.at(i), " during ", Logger::fSeverityNames.at(static_cast<int>(sev)), ", but it is not.")); throw runtime_error(ToStr("expecting to be logging ", Logger::fSeverityNames.at(i), " during ", sev, ", but it is not."));
} }
} else { } else {
if (Logger::Logging(static_cast<Severity>(i))) { if (Logger::Logging(static_cast<Severity>(i))) {
throw runtime_error(ToStr("expecting to NOT be logging ", Logger::fSeverityNames.at(i), " during ", Logger::fSeverityNames.at(static_cast<int>(sev)), ", but it is.")); throw runtime_error(ToStr("expecting to NOT be logging ", Logger::fSeverityNames.at(i), " during ", sev, ", but it is."));
} }
} }
} else { } else {
if (i >= static_cast<unsigned int>(sev)) { if (i >= static_cast<unsigned int>(sev)) {
if (!Logger::Logging(static_cast<Severity>(i))) { if (!Logger::Logging(static_cast<Severity>(i))) {
throw runtime_error(ToStr("expecting to be logging ", Logger::fSeverityNames.at(i), " during ", Logger::fSeverityNames.at(static_cast<int>(sev)), ", but it is not.")); throw runtime_error(ToStr("expecting to be logging ", Logger::fSeverityNames.at(i), " during ", sev, ", but it is not."));
} }
} else { } else {
if (Logger::Logging(static_cast<Severity>(i))) { if (Logger::Logging(static_cast<Severity>(i))) {
throw runtime_error(ToStr("expecting to NOT be logging ", Logger::fSeverityNames.at(i), " during ", Logger::fSeverityNames.at(static_cast<int>(sev)), ", but it is.")); throw runtime_error(ToStr("expecting to NOT be logging ", Logger::fSeverityNames.at(i), " during ", sev, ", but it is."));
} }
} }
} }
@ -83,9 +82,9 @@ void CheckSeverity(Severity severity)
int main() int main()
{ {
Logger::SetConsoleColor(true);
try { try {
Logger::SetConsoleColor(true);
cout << "##### testing " << Logger::fSeverityNames.size() << " severities..." << endl; cout << "##### testing " << Logger::fSeverityNames.size() << " severities..." << endl;
for (uint32_t i = 0; i < Logger::fSeverityNames.size(); ++i) { for (uint32_t i = 0; i < Logger::fSeverityNames.size(); ++i) {
CheckSeverity(static_cast<Severity>(i)); CheckSeverity(static_cast<Severity>(i));

View File

@ -20,18 +20,18 @@ using namespace fair::logger::test;
int main() int main()
{ {
Logger::SetConsoleColor(false); #ifdef FAIR_MIN_SEVERITY
Logger::SetVerbosity(Verbosity::low); if (static_cast<int>(Severity::FAIR_MIN_SEVERITY) > static_cast<int>(Severity::warn)) {
cout << "test requires at least FAIR_MIN_SEVERITY == warn to run, skipping" << endl;
Logger::SetConsoleSeverity(Severity::nolog); return 0;
}
#endif
try { try {
#ifdef FAIR_MIN_SEVERITY Logger::SetConsoleColor(false);
if (static_cast<int>(Severity::FAIR_MIN_SEVERITY) > static_cast<int>(Severity::warn)) { Logger::SetConsoleSeverity(Severity::nolog);
cout << "test requires at least FAIR_MIN_SEVERITY == warn to run, skipping" << endl; Logger::SetVerbosity(Verbosity::low);
return 0;
}
#endif
if (Logger::Logging(Severity::warn)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; } if (Logger::Logging(Severity::warn)) { cout << "Logger expected to NOT log warn, but it reports to do so" << endl; return 1; }
if (Logger::Logging(Severity::error)) { cout << "Logger expected to NOT log error, but it reports to do so" << endl; return 1; } if (Logger::Logging(Severity::error)) { cout << "Logger expected to NOT log error, but it reports to do so" << endl; return 1; }
if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; } if (!Logger::Logging(Severity::fatal)) { cout << "Logger expected to log fatal, but it reports not to" << endl; return 1; }
@ -43,7 +43,7 @@ int main()
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) { 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)), ")")); throw runtime_error(ToStr("File sink severity (", Logger::GetFileSeverity(), ") does not match the expected one (", Severity::warn, ")"));
} }
CheckOutput("^\\[FATAL] fatal\n$", [](){ CheckOutput("^\\[FATAL] fatal\n$", [](){
@ -81,7 +81,7 @@ int main()
cout << "CustomSink " << content << endl; cout << "CustomSink " << content << endl;
if (metadata.severity != Severity::warn && metadata.severity != Severity::error && metadata.severity != Severity::fatal) { if (metadata.severity != Severity::warn && metadata.severity != Severity::error && metadata.severity != Severity::fatal) {
throw runtime_error(ToStr("unexpected severity message arrived at custom sink that accepts only warn,error,fatal: ", Logger::fSeverityNames.at(static_cast<int>(metadata.severity)))); throw runtime_error(ToStr("unexpected severity message arrived at custom sink that accepts only warn,error,fatal: ", metadata.severity));
} }
if (metadata.severity_name != "WARN" && metadata.severity_name != "ERROR" && metadata.severity_name != "FATAL") { if (metadata.severity_name != "WARN" && metadata.severity_name != "ERROR" && metadata.severity_name != "FATAL") {
@ -90,7 +90,7 @@ int main()
}); });
if (Logger::GetCustomSeverity("CustomSink") != Severity::warn) { 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)), ")")); throw runtime_error(ToStr("File sink severity (", Logger::GetCustomSeverity("CustomSink"), ") does not match the expected one (", Severity::warn, ")"));
} }
bool oorThrown = false; bool oorThrown = false;

View File

@ -7,7 +7,6 @@
********************************************************************************/ ********************************************************************************/
#include "Common.h" #include "Common.h"
#include <Logger.h> #include <Logger.h>
#include <iostream> #include <iostream>
@ -19,10 +18,10 @@ using namespace fair::logger::test;
int main() int main()
{ {
Logger::SetConsoleColor(false);
Logger::SetConsoleSeverity(Severity::fatal);
try { try {
Logger::SetConsoleColor(false);
Logger::SetConsoleSeverity(Severity::fatal);
auto spec1 = VerbositySpec::Make(VerbositySpec::Info::file_line_function, VerbositySpec::Info::process_name); auto spec1 = VerbositySpec::Make(VerbositySpec::Info::file_line_function, VerbositySpec::Info::process_name);
auto spec2 = VerbositySpec::Make(VerbositySpec::Info::process_name, VerbositySpec::Info::file_line_function); auto spec2 = VerbositySpec::Make(VerbositySpec::Info::process_name, VerbositySpec::Info::file_line_function);