]> granicus.if.org Git - icinga2/blob - lib/base/logger.cpp
Use BOOST_THROW_EXCEPTION instead of boost::throw_exception()
[icinga2] / lib / base / logger.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://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 "i2-base.h"
21
22 using namespace icinga;
23
24 REGISTER_TYPE(Logger, NULL);
25
26 /**
27  * Constructor for the Logger class.
28  *
29  * @param properties A serialized dictionary containing attributes.
30  */
31 Logger::Logger(const Dictionary::Ptr& properties)
32         : DynamicObject(properties)
33 {
34         if (!IsLocal())
35                 BOOST_THROW_EXCEPTION(runtime_error("Logger objects must be local."));
36
37         String type = Get("type");
38         if (type.IsEmpty())
39                 BOOST_THROW_EXCEPTION(runtime_error("Logger objects must have a 'type' property."));
40
41         ILogger::Ptr impl;
42
43         if (type == "syslog") {
44 #ifndef _WIN32
45                 impl = boost::make_shared<SyslogLogger>();
46 #else /* _WIN32 */
47                 BOOST_THROW_EXCEPTION(invalid_argument("Syslog is not supported on Windows."));
48 #endif /* _WIN32 */
49         } else if (type == "file") {
50                 String path = Get("path");
51                 if (path.IsEmpty())
52                         BOOST_THROW_EXCEPTION(invalid_argument("'log' object of type 'file' must have a 'path' property"));
53
54                 StreamLogger::Ptr slogger = boost::make_shared<StreamLogger>();
55                 slogger->OpenFile(path);
56
57                 impl = slogger;
58         } else if (type == "console") {
59                 impl = boost::make_shared<StreamLogger>(&std::cout);
60         } else {
61                 BOOST_THROW_EXCEPTION(runtime_error("Unknown log type: " + type));
62         }
63
64         impl->m_Config = this;
65         m_Impl = impl;
66 }
67
68 /**
69  * Writes a message to the application's log.
70  *
71  * @param severity The message severity.
72  * @param facility The log facility.
73  * @param message The message.
74  */
75 void Logger::Write(LogSeverity severity, const String& facility,
76     const String& message)
77 {
78         LogEntry entry;
79         entry.Timestamp = Utility::GetTime();
80         entry.Severity = severity;
81         entry.Facility = facility;
82         entry.Message = message;
83
84         Event::Post(boost::bind(&Logger::ForwardLogEntry, entry));
85 }
86
87 /**
88  * Retrieves the minimum severity for this logger.
89  *
90  * @returns The minimum severity.
91  */
92 LogSeverity Logger::GetMinSeverity(void) const
93 {
94         String severity = Get("severity");
95         if (severity.IsEmpty())
96                 return LogInformation;
97         else
98                 return Logger::StringToSeverity(severity);
99 }
100
101 /**
102  * Forwards a log entry to the registered loggers.
103  *
104  * @param entry The log entry.
105  */
106 void Logger::ForwardLogEntry(const LogEntry& entry)
107 {
108         bool processed = false;
109
110         DynamicType::Ptr dt = DynamicType::GetByName("Logger");
111
112         DynamicObject::Ptr object;
113         BOOST_FOREACH(tie(tuples::ignore, object), dt->GetObjects()) {
114                 Logger::Ptr logger = dynamic_pointer_cast<Logger>(object);
115
116                 if (entry.Severity >= logger->GetMinSeverity())
117                         logger->m_Impl->ProcessLogEntry(entry);
118
119                 processed = true;
120         }
121
122         LogSeverity defaultLogLevel;
123
124         if (Application::IsDebugging())
125                 defaultLogLevel = LogDebug;
126         else
127                 defaultLogLevel = LogInformation;
128
129         if (!processed && entry.Severity >= defaultLogLevel) {
130                 static bool tty = StreamLogger::IsTty(std::cout);
131
132                 StreamLogger::ProcessLogEntry(std::cout, tty, entry);
133         }
134 }
135
136 /**
137  * Converts a severity enum value to a string.
138  *
139  * @param severity The severity value.
140  */
141 String Logger::SeverityToString(LogSeverity severity)
142 {
143         switch (severity) {
144                 case LogDebug:
145                         return "debug";
146                 case LogInformation:
147                         return "information";
148                 case LogWarning:
149                         return "warning";
150                 case LogCritical:
151                         return "critical";
152                 default:
153                         BOOST_THROW_EXCEPTION(invalid_argument("Invalid severity."));
154         }
155 }
156
157 /**
158  * Converts a string to a severity enum value.
159  *
160  * @param severity The severity.
161  */
162 LogSeverity Logger::StringToSeverity(const String& severity)
163 {
164         if (severity == "debug")
165                 return LogDebug;
166         else if (severity == "information")
167                 return LogInformation;
168         else if (severity == "warning")
169                 return LogWarning;
170         else if (severity == "critical")
171                 return LogCritical;
172         else
173                 BOOST_THROW_EXCEPTION(invalid_argument("Invalid severity: " + severity));
174 }
175
176 /**
177  * Retrieves the configuration object that belongs to this logger.
178  *
179  * @returns The configuration object.
180  */
181 DynamicObject::Ptr ILogger::GetConfig(void) const
182 {
183         return m_Config->GetSelf();
184 }
185