1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://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 ******************************************************************************/
22 using namespace icinga;
24 REGISTER_TYPE(Logger, NULL);
27 * Constructor for the Logger class.
29 * @param properties A serialized dictionary containing attributes.
31 Logger::Logger(const Dictionary::Ptr& properties)
32 : DynamicObject(properties)
35 BOOST_THROW_EXCEPTION(runtime_error("Logger objects must be local."));
37 String type = Get("type");
39 BOOST_THROW_EXCEPTION(runtime_error("Logger objects must have a 'type' property."));
43 if (type == "syslog") {
45 impl = boost::make_shared<SyslogLogger>();
47 BOOST_THROW_EXCEPTION(invalid_argument("Syslog is not supported on Windows."));
49 } else if (type == "file") {
50 String path = Get("path");
52 BOOST_THROW_EXCEPTION(invalid_argument("'log' object of type 'file' must have a 'path' property"));
54 StreamLogger::Ptr slogger = boost::make_shared<StreamLogger>();
55 slogger->OpenFile(path);
58 } else if (type == "console") {
59 impl = boost::make_shared<StreamLogger>(&std::cout);
61 BOOST_THROW_EXCEPTION(runtime_error("Unknown log type: " + type));
64 impl->m_Config = this;
69 * Writes a message to the application's log.
71 * @param severity The message severity.
72 * @param facility The log facility.
73 * @param message The message.
75 void Logger::Write(LogSeverity severity, const String& facility,
76 const String& message)
79 entry.Timestamp = Utility::GetTime();
80 entry.Severity = severity;
81 entry.Facility = facility;
82 entry.Message = message;
84 Event::Post(boost::bind(&Logger::ForwardLogEntry, entry));
88 * Retrieves the minimum severity for this logger.
90 * @returns The minimum severity.
92 LogSeverity Logger::GetMinSeverity(void) const
94 String severity = Get("severity");
95 if (severity.IsEmpty())
96 return LogInformation;
98 return Logger::StringToSeverity(severity);
102 * Forwards a log entry to the registered loggers.
104 * @param entry The log entry.
106 void Logger::ForwardLogEntry(const LogEntry& entry)
108 bool processed = false;
110 DynamicType::Ptr dt = DynamicType::GetByName("Logger");
112 DynamicObject::Ptr object;
113 BOOST_FOREACH(tie(tuples::ignore, object), dt->GetObjects()) {
114 Logger::Ptr logger = dynamic_pointer_cast<Logger>(object);
116 if (entry.Severity >= logger->GetMinSeverity())
117 logger->m_Impl->ProcessLogEntry(entry);
122 LogSeverity defaultLogLevel;
124 if (Application::IsDebugging())
125 defaultLogLevel = LogDebug;
127 defaultLogLevel = LogInformation;
129 if (!processed && entry.Severity >= defaultLogLevel) {
130 static bool tty = StreamLogger::IsTty(std::cout);
132 StreamLogger::ProcessLogEntry(std::cout, tty, entry);
137 * Converts a severity enum value to a string.
139 * @param severity The severity value.
141 String Logger::SeverityToString(LogSeverity severity)
147 return "information";
153 BOOST_THROW_EXCEPTION(invalid_argument("Invalid severity."));
158 * Converts a string to a severity enum value.
160 * @param severity The severity.
162 LogSeverity Logger::StringToSeverity(const String& severity)
164 if (severity == "debug")
166 else if (severity == "information")
167 return LogInformation;
168 else if (severity == "warning")
170 else if (severity == "critical")
173 BOOST_THROW_EXCEPTION(invalid_argument("Invalid severity: " + severity));
177 * Retrieves the configuration object that belongs to this logger.
179 * @returns The configuration object.
181 DynamicObject::Ptr ILogger::GetConfig(void) const
183 return m_Config->GetSelf();