icinga2/lib/base/logger.cpp

194 lines
5.3 KiB
C++
Raw Normal View History

2012-07-10 12:21:19 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "i2-base.h"
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Logger);
2012-07-10 12:21:19 +02:00
/**
2012-09-17 13:35:55 +02:00
* Constructor for the Logger class.
*
2012-09-17 13:35:55 +02:00
* @param properties A serialized dictionary containing attributes.
*/
Logger::Logger(const Dictionary::Ptr& properties)
2012-07-30 10:17:29 +02:00
: DynamicObject(properties)
{
2013-02-26 10:13:54 +01:00
RegisterAttribute("type", Attribute_Config, &m_Type);
RegisterAttribute("path", Attribute_Config, &m_Path);
RegisterAttribute("severity", Attribute_Config, &m_Severity);
if (!IsLocal())
BOOST_THROW_EXCEPTION(runtime_error("Logger objects must be local."));
2013-03-01 12:07:52 +01:00
}
2013-03-01 12:07:52 +01:00
void Logger::Start(void)
{
2013-02-26 10:13:54 +01:00
String type = m_Type;
2012-08-03 13:19:55 +02:00
if (type.IsEmpty())
BOOST_THROW_EXCEPTION(runtime_error("Logger objects must have a 'type' property."));
ILogger::Ptr impl;
if (type == "syslog") {
#ifndef _WIN32
impl = boost::make_shared<SyslogLogger>();
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(invalid_argument("Syslog is not supported on Windows."));
#endif /* _WIN32 */
} else if (type == "file") {
2013-02-26 10:13:54 +01:00
String path = m_Path;
2012-08-03 13:19:55 +02:00
if (path.IsEmpty())
BOOST_THROW_EXCEPTION(invalid_argument("'log' object of type 'file' must have a 'path' property"));
StreamLogger::Ptr slogger = boost::make_shared<StreamLogger>();
slogger->OpenFile(path);
impl = slogger;
} else if (type == "console") {
impl = boost::make_shared<StreamLogger>(&std::cout);
} else {
BOOST_THROW_EXCEPTION(runtime_error("Unknown log type: " + type));
}
2013-03-01 12:07:52 +01:00
impl->m_Config = GetSelf();
m_Impl = impl;
2013-03-01 12:07:52 +01:00
}
2012-07-10 12:21:19 +02:00
/**
* Writes a message to the application's log.
*
* @param severity The message severity.
* @param facility The log facility.
* @param message The message.
*/
void Logger::Write(LogSeverity severity, const String& facility,
const String& message)
2012-07-10 12:21:19 +02:00
{
LogEntry entry;
entry.Timestamp = Utility::GetTime();
2012-07-10 12:21:19 +02:00
entry.Severity = severity;
entry.Facility = facility;
entry.Message = message;
2013-02-18 14:40:24 +01:00
ForwardLogEntry(entry);
2012-07-10 12:21:19 +02:00
}
/**
* Retrieves the minimum severity for this logger.
*
* @returns The minimum severity.
*/
2012-07-10 12:21:19 +02:00
LogSeverity Logger::GetMinSeverity(void) const
{
2013-02-26 10:13:54 +01:00
String severity = m_Severity;
2012-08-03 13:19:55 +02:00
if (severity.IsEmpty())
return LogInformation;
else
return Logger::StringToSeverity(severity);
2012-07-10 12:21:19 +02:00
}
/**
* Forwards a log entry to the registered loggers.
*
* @param entry The log entry.
*/
2012-07-10 12:21:19 +02:00
void Logger::ForwardLogEntry(const LogEntry& entry)
{
bool processed = false;
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Logger")) {
Logger::Ptr logger = dynamic_pointer_cast<Logger>(object);
2013-02-18 23:44:24 +01:00
{
ObjectLock llock(logger);
2013-02-18 23:44:24 +01:00
if (entry.Severity >= logger->GetMinSeverity())
logger->m_Impl->ProcessLogEntry(entry);
2013-02-18 14:40:24 +01:00
}
2013-02-18 23:44:24 +01:00
processed = true;
2012-07-10 12:21:19 +02:00
}
LogSeverity defaultLogLevel;
if (Application::IsDebugging())
defaultLogLevel = LogDebug;
else
defaultLogLevel = LogInformation;
if (!processed && entry.Severity >= defaultLogLevel) {
2013-02-01 14:12:24 +01:00
static bool tty = StreamLogger::IsTty(std::cout);
StreamLogger::ProcessLogEntry(std::cout, tty, entry);
}
2012-07-10 12:21:19 +02:00
}
2012-09-14 14:41:17 +02:00
/**
* Converts a severity enum value to a string.
*
* @param severity The severity value.
*/
String Logger::SeverityToString(LogSeverity severity)
{
switch (severity) {
case LogDebug:
return "debug";
case LogInformation:
return "information";
case LogWarning:
return "warning";
case LogCritical:
return "critical";
default:
BOOST_THROW_EXCEPTION(invalid_argument("Invalid severity."));
}
}
2012-09-14 14:41:17 +02:00
/**
* Converts a string to a severity enum value.
*
* @param severity The severity.
*/
LogSeverity Logger::StringToSeverity(const String& severity)
{
if (severity == "debug")
return LogDebug;
else if (severity == "information")
return LogInformation;
else if (severity == "warning")
return LogWarning;
else if (severity == "critical")
return LogCritical;
else
BOOST_THROW_EXCEPTION(invalid_argument("Invalid severity: " + severity));
}
2012-09-17 14:47:43 +02:00
/**
* Retrieves the configuration object that belongs to this logger.
*
* @returns The configuration object.
*/
DynamicObject::Ptr ILogger::GetConfig(void) const
{
2013-03-01 12:07:52 +01:00
return m_Config.lock();
2012-09-17 14:47:43 +02:00
}