1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/logger.hpp"
21 #include "base/logger.tcpp"
22 #include "base/application.hpp"
23 #include "base/streamlogger.hpp"
24 #include "base/configtype.hpp"
25 #include "base/utility.hpp"
26 #include "base/objectlock.hpp"
27 #include "base/context.hpp"
28 #include "base/scriptglobal.hpp"
31 using namespace icinga;
33 REGISTER_TYPE(Logger);
35 std::set<Logger::Ptr> Logger::m_Loggers;
36 boost::mutex Logger::m_Mutex;
37 bool Logger::m_ConsoleLogEnabled = true;
38 bool Logger::m_TimestampEnabled = true;
39 LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
41 INITIALIZE_ONCE([]() {
42 ScriptGlobal::Set("LogDebug", LogDebug);
43 ScriptGlobal::Set("LogNotice", LogNotice);
44 ScriptGlobal::Set("LogInformation", LogInformation);
45 ScriptGlobal::Set("LogWarning", LogWarning);
46 ScriptGlobal::Set("LogCritical", LogCritical);
50 * Constructor for the Logger class.
52 void Logger::Start(bool runtimeCreated)
54 ObjectImpl<Logger>::Start(runtimeCreated);
56 boost::mutex::scoped_lock lock(m_Mutex);
57 m_Loggers.insert(this);
60 void Logger::Stop(bool runtimeRemoved)
63 boost::mutex::scoped_lock lock(m_Mutex);
64 m_Loggers.erase(this);
67 ObjectImpl<Logger>::Stop(runtimeRemoved);
70 std::set<Logger::Ptr> Logger::GetLoggers(void)
72 boost::mutex::scoped_lock lock(m_Mutex);
77 * Writes a message to the application's log.
79 * @param severity The message severity.
80 * @param facility The log facility.
81 * @param message The message.
83 void icinga::IcingaLog(LogSeverity severity, const String& facility,
84 const String& message)
87 entry.Timestamp = Utility::GetTime();
88 entry.Severity = severity;
89 entry.Facility = facility;
90 entry.Message = message;
92 if (severity >= LogWarning) {
95 if (context.GetLength() > 0) {
96 std::ostringstream trace;
98 entry.Message += "\nContext:" + trace.str();
102 for (const Logger::Ptr& logger : Logger::GetLoggers()) {
103 ObjectLock llock(logger);
105 if (!logger->IsActive())
108 if (entry.Severity >= logger->GetMinSeverity())
109 logger->ProcessLogEntry(entry);
112 if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity())
113 StreamLogger::ProcessLogEntry(std::cout, entry);
117 * Retrieves the minimum severity for this logger.
119 * @returns The minimum severity.
121 LogSeverity Logger::GetMinSeverity(void) const
123 String severity = GetSeverity();
124 if (severity.IsEmpty())
125 return LogInformation;
127 LogSeverity ls = LogInformation;
130 ls = Logger::StringToSeverity(severity);
131 } catch (const std::exception&) { /* use the default level */ }
138 * Converts a severity enum value to a string.
140 * @param severity The severity value.
142 String Logger::SeverityToString(LogSeverity severity)
150 return "information";
156 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity."));
161 * Converts a string to a severity enum value.
163 * @param severity The severity.
165 LogSeverity Logger::StringToSeverity(const String& severity)
167 if (severity == "debug")
169 else if (severity == "notice")
171 else if (severity == "information")
172 return LogInformation;
173 else if (severity == "warning")
175 else if (severity == "critical")
178 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
181 void Logger::DisableConsoleLog(void)
183 m_ConsoleLogEnabled = false;
186 void Logger::EnableConsoleLog(void)
188 m_ConsoleLogEnabled = true;
191 bool Logger::IsConsoleLogEnabled(void)
193 return m_ConsoleLogEnabled;
196 void Logger::SetConsoleLogSeverity(LogSeverity logSeverity)
198 m_ConsoleLogSeverity = logSeverity;
201 LogSeverity Logger::GetConsoleLogSeverity(void)
203 return m_ConsoleLogSeverity;
206 void Logger::DisableTimestamp(bool disable)
208 m_TimestampEnabled = !disable;
211 bool Logger::IsTimestampEnabled(void)
213 return m_TimestampEnabled;
216 void Logger::ValidateSeverity(const String& value, const ValidationUtils& utils)
218 ObjectImpl<Logger>::ValidateSeverity(value, utils);
221 StringToSeverity(value);
223 BOOST_THROW_EXCEPTION(ValidationError(this, { "severity" }, "Invalid severity specified: " + value));