[icinga-checkins] icinga.org: icinga2/next: Error messages: Fix Logger exceptions.

git at icinga.org git at icinga.org
Thu Jun 5 18:29:54 CEST 2014


Module: icinga2
Branch: next
Commit: 17668af2c290595ba731e366d03fc7a0d863ff2d
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=17668af2c290595ba731e366d03fc7a0d863ff2d

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Thu Jun  5 15:35:30 2014 +0200

Error messages: Fix Logger exceptions.

Refs #6070

---

 icinga-app/icinga.cpp     |   11 ++++++++++-
 lib/base/logger.cpp       |   18 ++++++++++++++----
 lib/base/streamlogger.cpp |    7 ++++++-
 3 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp
index d557ce4..1d5a0da 100644
--- a/icinga-app/icinga.cpp
+++ b/icinga-app/icinga.cpp
@@ -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);
 	}
 
diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp
index 507f690..ca1f923 100644
--- a/lib/base/logger.cpp
+++ b/lib/base/logger.cpp
@@ -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;
-}
\ No newline at end of file
+}
diff --git a/lib/base/streamlogger.cpp b/lib/base/streamlogger.cpp
index 163fada..872d94e 100644
--- a/lib/base/streamlogger.cpp
+++ b/lib/base/streamlogger.cpp
@@ -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



More information about the icinga-checkins mailing list