Skip to content

Commit

Permalink
Warn log level by default and userinfo to change it (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Nov 3, 2021
1 parent 34aca7b commit 7b38395
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/2-TaggedMetrics.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
23 changes: 14 additions & 9 deletions src/MonLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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<int>(severity) < static_cast<int>(mLoggerSeverity)) ? true : false;
switch(severity) {
case Severity::Silent: mMute = true; break;
case Severity::Warn: mStream << &wToken; break;
default: mStream << static_cast<InfoLogger::Severity>(severity); break;
}
Expand All @@ -106,7 +111,7 @@ class MonLogger
std::ostream& mStream = std::cout;
void logHeader(Severity severity)
{
if (severity == Severity::Silent) { mMute = true; }
mMute = (static_cast<std::underlying_type<Severity>::type>(severity) > static_cast<std::underlying_type<Severity>::type>(mLoggerSeverity)) ? true : false;
*this << "\033[0;" << static_cast<int>(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";
Expand Down
3 changes: 3 additions & 0 deletions src/MonitoringFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ std::unique_ptr<Backend> 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);
Expand Down

0 comments on commit 7b38395

Please sign in to comment.