]> granicus.if.org Git - icinga2/blob - lib/base/logger.cpp
Update copyright headers for 2016
[icinga2] / lib / base / logger.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
30 #include <iostream>
31
32 using namespace icinga;
33
34 REGISTER_TYPE(Logger);
35 INITIALIZE_ONCE(&Logger::StaticInitialize);
36
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;
42
43 void Logger::StaticInitialize(void)
44 {
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);
50 }
51
52 /**
53  * Constructor for the Logger class.
54  */
55 void Logger::Start(bool runtimeCreated)
56 {
57         ObjectImpl<Logger>::Start(runtimeCreated);
58
59         boost::mutex::scoped_lock lock(m_Mutex);
60         m_Loggers.insert(this);
61 }
62
63 void Logger::Stop(bool runtimeRemoved)
64 {
65         {
66                 boost::mutex::scoped_lock lock(m_Mutex);
67                 m_Loggers.erase(this);
68         }
69
70         ObjectImpl<Logger>::Stop(runtimeRemoved);
71 }
72
73 std::set<Logger::Ptr> Logger::GetLoggers(void)
74 {
75         boost::mutex::scoped_lock lock(m_Mutex);
76         return m_Loggers;
77 }
78
79 /**
80  * Writes a message to the application's log.
81  *
82  * @param severity The message severity.
83  * @param facility The log facility.
84  * @param message The message.
85  */
86 void icinga::IcingaLog(LogSeverity severity, const String& facility,
87     const String& message)
88 {
89         LogEntry entry;
90         entry.Timestamp = Utility::GetTime();
91         entry.Severity = severity;
92         entry.Facility = facility;
93         entry.Message = message;
94
95         if (severity >= LogWarning) {
96                 ContextTrace context;
97
98                 if (context.GetLength() > 0) {
99                         std::ostringstream trace;
100                         trace << context;
101                         entry.Message += "\nContext:" + trace.str();
102                 }
103         }
104
105         BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) {
106                 ObjectLock llock(logger);
107
108                 if (!logger->IsActive())
109                         continue;
110
111                 if (entry.Severity >= logger->GetMinSeverity())
112                         logger->ProcessLogEntry(entry);
113         }
114
115         if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity())
116                 StreamLogger::ProcessLogEntry(std::cout, entry);
117 }
118
119 /**
120  * Retrieves the minimum severity for this logger.
121  *
122  * @returns The minimum severity.
123  */
124 LogSeverity Logger::GetMinSeverity(void) const
125 {
126         String severity = GetSeverity();
127         if (severity.IsEmpty())
128                 return LogInformation;
129         else {
130                 LogSeverity ls = LogInformation;
131
132                 try {
133                         ls = Logger::StringToSeverity(severity);
134                 } catch (const std::exception&) { /* use the default level */ }
135
136                 return ls;
137         }
138 }
139
140 /**
141  * Converts a severity enum value to a string.
142  *
143  * @param severity The severity value.
144  */
145 String Logger::SeverityToString(LogSeverity severity)
146 {
147         switch (severity) {
148                 case LogDebug:
149                         return "debug";
150                 case LogNotice:
151                         return "notice";
152                 case LogInformation:
153                         return "information";
154                 case LogWarning:
155                         return "warning";
156                 case LogCritical:
157                         return "critical";
158                 default:
159                         Log(LogCritical, "Logger", "Invalid severity.");
160                         BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity."));
161         }
162 }
163
164 /**
165  * Converts a string to a severity enum value.
166  *
167  * @param severity The severity.
168  */
169 LogSeverity Logger::StringToSeverity(const String& severity)
170 {
171         if (severity == "debug")
172                 return LogDebug;
173         else if (severity == "notice")
174                 return LogNotice;
175         else if (severity == "information")
176                 return LogInformation;
177         else if (severity == "warning")
178                 return LogWarning;
179         else if (severity == "critical")
180                 return LogCritical;
181         else {
182                 Log(LogCritical, "Logger")
183                     << "Invalid severity: '" << severity << "'.";
184                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
185         }
186 }
187
188 void Logger::DisableConsoleLog(void)
189 {
190         m_ConsoleLogEnabled = false;
191 }
192
193 void Logger::EnableConsoleLog(void)
194 {
195         m_ConsoleLogEnabled = true;
196 }
197
198 bool Logger::IsConsoleLogEnabled(void)
199 {
200         return m_ConsoleLogEnabled;
201 }
202
203 void Logger::SetConsoleLogSeverity(LogSeverity logSeverity)
204 {
205         m_ConsoleLogSeverity = logSeverity;
206 }
207
208 LogSeverity Logger::GetConsoleLogSeverity(void)
209 {
210         return m_ConsoleLogSeverity;
211 }
212
213 void Logger::DisableTimestamp(bool disable)
214 {
215         m_TimestampEnabled = !disable;
216 }
217
218 bool Logger::IsTimestampEnabled(void)
219 {
220         return m_TimestampEnabled;
221 }