From 7b383953ade2dd47386b2b44d6b179a0a6038370 Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Wed, 3 Nov 2021 17:25:09 +0100 Subject: [PATCH] Warn log level by default and userinfo to change it (#272) --- examples/2-TaggedMetrics.cxx | 2 +- src/MonLogger.h | 23 ++++++++++++++--------- src/MonitoringFactory.cxx | 3 +++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/examples/2-TaggedMetrics.cxx b/examples/2-TaggedMetrics.cxx index 4338b561..a25b3617 100644 --- a/examples/2-TaggedMetrics.cxx +++ b/examples/2-TaggedMetrics.cxx @@ -12,7 +12,7 @@ int main() // Configure monitoring // Pass string with list of URLs as parameter - auto monitoring = MonitoringFactory::Get("influxdbv2://localhost:8086/?bucket=adam&org=adam&token=v0SbCG4TRvHbHk82lckOT-T6iYY5VbGlXqOUnQlyaJNlT43eRnK_U8MllQT2kctwPFNwIqTO3HK4mnmGDCXk9g=="); + auto monitoring = MonitoringFactory::Get("stdout://debug:@/"); /// Add global tags monitoring->addGlobalTag("name", "test"); diff --git a/src/MonLogger.h b/src/MonLogger.h index d2b925ee..375f1846 100644 --- a/src/MonLogger.h +++ b/src/MonLogger.h @@ -34,16 +34,16 @@ namespace monitoring /// List of possible log severities #ifdef O2_MONITORING_WITH_INFOLOGGER -enum class Severity { Error = InfoLogger::Severity::Error, - Warn = InfoLogger::Severity::Warning, - Info = InfoLogger::Severity::Info, - Silent = InfoLogger::Severity::Debug}; +enum class Severity { Error = InfoLogger::Severity::Error, // 69 + Warn = InfoLogger::Severity::Warning, // 87 + Info = InfoLogger::Severity::Info, // 73 + Debug = InfoLogger::Severity::Debug}; // 68 #else enum class Severity { Error = 31, Warn = 33, Info = 49, - Silent = 50}; + Debug = 50}; #endif @@ -67,13 +67,16 @@ class MonLogger /// Returns Logger instance with current date and given severity /// \param severity - severity level /// \return - logger instance - static MonLogger& Get(Severity severity = Severity::Silent) + static MonLogger& Get(Severity severity = Severity::Debug) { static MonLogger loggerInstance; loggerInstance.logHeader(severity); return loggerInstance; } + /// Logger severity to be set by the user + inline static Severity mLoggerSeverity = Severity::Warn; + /// Terminates log line /// return - string with color termination and new line #ifdef O2_MONITORING_WITH_INFOLOGGER @@ -83,8 +86,9 @@ class MonLogger #endif private: - /// Makes sure Silent messages are muted + /// Makes sure Debug messages are muted bool mMute = false; + #ifdef O2_MONITORING_WITH_INFOLOGGER /// InfoLogger log output stream InfoLogger mStream; @@ -95,8 +99,9 @@ class MonLogger context.setField(InfoLoggerContext::FieldName::Facility, "Library"); mStream.setContext(context); static InfoLogger::AutoMuteToken wToken({InfoLogger::Severity::Warning, InfoLogger::Level::Support, -1, nullptr, -1}, 2, 30); + + mMute = (static_cast(severity) < static_cast(mLoggerSeverity)) ? true : false; switch(severity) { - case Severity::Silent: mMute = true; break; case Severity::Warn: mStream << &wToken; break; default: mStream << static_cast(severity); break; } @@ -106,7 +111,7 @@ class MonLogger std::ostream& mStream = std::cout; void logHeader(Severity severity) { - if (severity == Severity::Silent) { mMute = true; } + mMute = (static_cast::type>(severity) > static_cast::type>(mLoggerSeverity)) ? true : false; *this << "\033[0;" << static_cast(severity) << "m"; auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); *this << std::put_time(std::localtime(&now), "%Y-%m-%d %X") << "\t" << "Monitoring" << "\t"; diff --git a/src/MonitoringFactory.cxx b/src/MonitoringFactory.cxx index 6d27838c..9e4d98e2 100644 --- a/src/MonitoringFactory.cxx +++ b/src/MonitoringFactory.cxx @@ -180,6 +180,9 @@ std::unique_ptr MonitoringFactory::GetBackend(std::string& url) throw MonitoringException("Factory", "Unrecognized backend " + parsedUrl.protocol); } try { + if (parsedUrl.user == "debug") { + MonLogger::mLoggerSeverity = Severity::Debug; + } auto backend = iterator->second(parsedUrl); if (!parsedUrl.path.empty() && parsedUrl.path != "/") { SetVerbosity(parsedUrl.path.substr(parsedUrl.path.rfind("/")), backend);