Error messages: Fix Logger exceptions.

Refs #6070
This commit is contained in:
Michael Friedrich 2014-06-05 15:35:30 +02:00
parent 2989c00680
commit 17668af2c2
3 changed files with 30 additions and 6 deletions

View File

@ -429,7 +429,16 @@ int Main(void)
#endif /* _WIN32 */
if (g_AppParams.count("log-level")) {
LogSeverity logLevel = Logger::StringToSeverity(g_AppParams["log-level"].as<std::string>());
String severity = g_AppParams["log-level"].as<std::string>();
LogSeverity logLevel = LogInformation;
try {
logLevel = Logger::StringToSeverity(severity);
} catch (std::exception&) {
/* use the default */
Log(LogWarning, "icinga", "Invalid log level set. Using default 'information'.");
}
Logger::SetConsoleLogSeverity(logLevel);
}

View File

@ -123,8 +123,15 @@ LogSeverity Logger::GetMinSeverity(void) const
String severity = GetSeverity();
if (severity.IsEmpty())
return LogInformation;
else
return Logger::StringToSeverity(severity);
else {
LogSeverity ls = LogInformation;
try {
ls = Logger::StringToSeverity(severity);
} catch (std::exception&) { /* use the default level */ }
return ls;
}
}
/**
@ -146,6 +153,7 @@ String Logger::SeverityToString(LogSeverity severity)
case LogCritical:
return "critical";
default:
Log(LogCritical, "Logger", "Invalid severity.");
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity."));
}
}
@ -167,8 +175,10 @@ LogSeverity Logger::StringToSeverity(const String& severity)
return LogWarning;
else if (severity == "critical")
return LogCritical;
else
else {
Log(LogCritical, "Logger", "Invalid severity: '" + severity + "'.");
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
}
}
void Logger::DisableConsoleLog(void)
@ -189,4 +199,4 @@ void Logger::SetConsoleLogSeverity(LogSeverity logSeverity)
LogSeverity Logger::GetConsoleLogSeverity(void)
{
return m_ConsoleLogSeverity;
}
}

View File

@ -114,7 +114,12 @@ void StreamLogger::ProcessLogEntry(std::ostream& stream, bool tty, const LogEntr
}
}
stream << Logger::SeverityToString(entry.Severity);
try {
stream << Logger::SeverityToString(entry.Severity);
} catch (std::exception&) {
/* bail early */
return;
}
if (tty)
stream << "\x1b[0m"; // clear colors