From 85a82c846180f62b09fb9b7ecf48954913a8f900 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Mon, 3 Mar 2025 13:38:21 +0100 Subject: [PATCH] Change console logging to follow LogMask --- Logging/Logging.cpp | 34 ++++++++++++++++++---------------- include/Logging.h | 2 ++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Logging/Logging.cpp b/Logging/Logging.cpp index 2ce0860..2570533 100644 --- a/Logging/Logging.cpp +++ b/Logging/Logging.cpp @@ -8,6 +8,8 @@ namespace Logging { +Severity::type loggingSeverity; + void OpenLog() { openlog(NULL, LOG_NDELAY | LOG_PID, LOG_USER); @@ -20,55 +22,55 @@ void CloseLog() void SetLogMask(Severity::type severity) { + loggingSeverity = severity; int level = static_cast(severity); setlogmask(LOG_UPTO(level)); switch (level) { case LOG_EMERG: - Log(Severity::Emergency, "LogMask set to Severity::Emergency\n"); + Log(Severity::Emergency, "LogMask set to Severity::Emergency"); break; case LOG_ALERT: - Log(Severity::Alert, "LogMask set to Severity::Alert\n"); + Log(Severity::Alert, "LogMask set to Severity::Alert"); break; case LOG_CRIT: - Log(Severity::Critical, "LogMask set to Severity::Critical\n"); + Log(Severity::Critical, "LogMask set to Severity::Critical"); break; case LOG_ERR: - Log(Severity::Error, "LogMask set to Severity::Error\n"); + Log(Severity::Error, "LogMask set to Severity::Error"); break; case LOG_WARNING: - Log(Severity::Warning, "LogMask set to Severity::Warning\n"); + Log(Severity::Warning, "LogMask set to Severity::Warning"); break; case LOG_NOTICE: - Log(Severity::Notice, "LogMask set to Severity::Notice\n"); + Log(Severity::Notice, "LogMask set to Severity::Notice"); break; case LOG_INFO: - Log(Severity::Notice, "LogMask set to Severity::Info\n"); + Log(Severity::Notice, "LogMask set to Severity::Info"); break; case LOG_DEBUG: - Log(Severity::Notice, "LogMask set to Severity::Debug\n"); + Log(Severity::Notice, "LogMask set to Severity::Debug"); break; } } void Log(Severity::type severity, const std::string& message) { - bool error = (severity < Severity::Warning); - bool debug = (severity == Severity::Debug); - std::ostream& output = error? std::cerr: std::cout; + syslog(static_cast(severity), "%s", message.c_str()); - bool newline = StringAlgorithm::ends_with(message, "\n"); + if (severity > loggingSeverity) + return; - if (debug) + std::ostream& output = (severity < Severity::Warning)? std::cerr: std::cout; + + if (severity == Severity::Debug) output << " "; output << message; - if (!newline) + if (!StringAlgorithm::ends_with(message, "\n")) output << std::endl; - - syslog(static_cast(severity), "%s", message.c_str()); } } // namespace Logging diff --git a/include/Logging.h b/include/Logging.h index 9ea3256..21d2dec 100644 --- a/include/Logging.h +++ b/include/Logging.h @@ -31,6 +31,8 @@ public: }; }; +extern Severity::type loggingSeverity; + void OpenLog(); void CloseLog(); void SetLogMask(Severity::type severity);