1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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"
29 #include <boost/foreach.hpp>
32 using namespace icinga;
34 REGISTER_TYPE(Logger);
35 INITIALIZE_ONCE(&Logger::StaticInitialize);
37 std::set<Logger::Ptr> Logger::m_Loggers;
38 boost::mutex Logger::m_Mutex;
39 bool Logger::m_ConsoleLogEnabled = true;
40 bool Logger::m_TimestampEnabled = true;
41 LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
43 void Logger::StaticInitialize(void)
45 ScriptGlobal::Set("LogDebug", LogDebug);
46 ScriptGlobal::Set("LogNotice", LogNotice);
47 ScriptGlobal::Set("LogInformation", LogInformation);
48 ScriptGlobal::Set("LogWarning", LogWarning);
49 ScriptGlobal::Set("LogCritical", LogCritical);
53 * Constructor for the Logger class.
55 void Logger::Start(bool runtimeCreated)
57 ObjectImpl<Logger>::Start(runtimeCreated);
59 boost::mutex::scoped_lock lock(m_Mutex);
60 m_Loggers.insert(this);
63 void Logger::Stop(bool runtimeRemoved)
66 boost::mutex::scoped_lock lock(m_Mutex);
67 m_Loggers.erase(this);
70 ObjectImpl<Logger>::Stop(runtimeRemoved);
73 std::set<Logger::Ptr> Logger::GetLoggers(void)
75 boost::mutex::scoped_lock lock(m_Mutex);
80 * Writes a message to the application's log.
82 * @param severity The message severity.
83 * @param facility The log facility.
84 * @param message The message.
86 void icinga::IcingaLog(LogSeverity severity, const String& facility,
87 const String& message)
90 entry.Timestamp = Utility::GetTime();
91 entry.Severity = severity;
92 entry.Facility = facility;
93 entry.Message = message;
95 if (severity >= LogWarning) {
98 if (context.GetLength() > 0) {
99 std::ostringstream trace;
101 entry.Message += "\nContext:" + trace.str();
105 BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) {
106 ObjectLock llock(logger);
108 if (!logger->IsActive())
111 if (entry.Severity >= logger->GetMinSeverity())
112 logger->ProcessLogEntry(entry);
115 if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity())
116 StreamLogger::ProcessLogEntry(std::cout, entry);
120 * Retrieves the minimum severity for this logger.
122 * @returns The minimum severity.
124 LogSeverity Logger::GetMinSeverity(void) const
126 String severity = GetSeverity();
127 if (severity.IsEmpty())
128 return LogInformation;
130 LogSeverity ls = LogInformation;
133 ls = Logger::StringToSeverity(severity);
134 } catch (const std::exception&) { /* use the default level */ }
141 * Converts a severity enum value to a string.
143 * @param severity The severity value.
145 String Logger::SeverityToString(LogSeverity severity)
153 return "information";
159 Log(LogCritical, "Logger", "Invalid severity.");
160 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity."));
165 * Converts a string to a severity enum value.
167 * @param severity The severity.
169 LogSeverity Logger::StringToSeverity(const String& severity)
171 if (severity == "debug")
173 else if (severity == "notice")
175 else if (severity == "information")
176 return LogInformation;
177 else if (severity == "warning")
179 else if (severity == "critical")
182 Log(LogCritical, "Logger")
183 << "Invalid severity: '" << severity << "'.";
184 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
188 void Logger::DisableConsoleLog(void)
190 m_ConsoleLogEnabled = false;
193 void Logger::EnableConsoleLog(void)
195 m_ConsoleLogEnabled = true;
198 bool Logger::IsConsoleLogEnabled(void)
200 return m_ConsoleLogEnabled;
203 void Logger::SetConsoleLogSeverity(LogSeverity logSeverity)
205 m_ConsoleLogSeverity = logSeverity;
208 LogSeverity Logger::GetConsoleLogSeverity(void)
210 return m_ConsoleLogSeverity;
213 void Logger::DisableTimestamp(bool disable)
215 m_TimestampEnabled = !disable;
218 bool Logger::IsTimestampEnabled(void)
220 return m_TimestampEnabled;