Change console logging to follow LogMask

This commit is contained in:
2025-03-03 13:38:21 +01:00
parent 2490dabc82
commit 85a82c8461
2 changed files with 20 additions and 16 deletions
+18 -16
View File
@@ -8,6 +8,8 @@
namespace Logging { namespace Logging {
Severity::type loggingSeverity;
void OpenLog() void OpenLog()
{ {
openlog(NULL, LOG_NDELAY | LOG_PID, LOG_USER); openlog(NULL, LOG_NDELAY | LOG_PID, LOG_USER);
@@ -20,55 +22,55 @@ void CloseLog()
void SetLogMask(Severity::type severity) void SetLogMask(Severity::type severity)
{ {
loggingSeverity = severity;
int level = static_cast<int>(severity); int level = static_cast<int>(severity);
setlogmask(LOG_UPTO(level)); setlogmask(LOG_UPTO(level));
switch (level) switch (level)
{ {
case LOG_EMERG: case LOG_EMERG:
Log(Severity::Emergency, "LogMask set to Severity::Emergency\n"); Log(Severity::Emergency, "LogMask set to Severity::Emergency");
break; break;
case LOG_ALERT: case LOG_ALERT:
Log(Severity::Alert, "LogMask set to Severity::Alert\n"); Log(Severity::Alert, "LogMask set to Severity::Alert");
break; break;
case LOG_CRIT: case LOG_CRIT:
Log(Severity::Critical, "LogMask set to Severity::Critical\n"); Log(Severity::Critical, "LogMask set to Severity::Critical");
break; break;
case LOG_ERR: case LOG_ERR:
Log(Severity::Error, "LogMask set to Severity::Error\n"); Log(Severity::Error, "LogMask set to Severity::Error");
break; break;
case LOG_WARNING: case LOG_WARNING:
Log(Severity::Warning, "LogMask set to Severity::Warning\n"); Log(Severity::Warning, "LogMask set to Severity::Warning");
break; break;
case LOG_NOTICE: case LOG_NOTICE:
Log(Severity::Notice, "LogMask set to Severity::Notice\n"); Log(Severity::Notice, "LogMask set to Severity::Notice");
break; break;
case LOG_INFO: case LOG_INFO:
Log(Severity::Notice, "LogMask set to Severity::Info\n"); Log(Severity::Notice, "LogMask set to Severity::Info");
break; break;
case LOG_DEBUG: case LOG_DEBUG:
Log(Severity::Notice, "LogMask set to Severity::Debug\n"); Log(Severity::Notice, "LogMask set to Severity::Debug");
break; break;
} }
} }
void Log(Severity::type severity, const std::string& message) void Log(Severity::type severity, const std::string& message)
{ {
bool error = (severity < Severity::Warning); syslog(static_cast<int>(severity), "%s", message.c_str());
bool debug = (severity == Severity::Debug);
std::ostream& output = error? std::cerr: std::cout;
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 << " ";
output << message; output << message;
if (!newline) if (!StringAlgorithm::ends_with(message, "\n"))
output << std::endl; output << std::endl;
syslog(static_cast<int>(severity), "%s", message.c_str());
} }
} // namespace Logging } // namespace Logging
+2
View File
@@ -31,6 +31,8 @@ public:
}; };
}; };
extern Severity::type loggingSeverity;
void OpenLog(); void OpenLog();
void CloseLog(); void CloseLog();
void SetLogMask(Severity::type severity); void SetLogMask(Severity::type severity);