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 {
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<int>(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<int>(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<int>(severity), "%s", message.c_str());
}
} // namespace Logging
+2
View File
@@ -31,6 +31,8 @@ public:
};
};
extern Severity::type loggingSeverity;
void OpenLog();
void CloseLog();
void SetLogMask(Severity::type severity);