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;
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
REGISTER_CLASS(Logger);
|
2012-07-10 12:21:19 +02:00
|
|
|
|
2012-07-10 12:51:53 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the logger class.
|
|
|
|
*
|
|
|
|
* @param minSeverity The minimum severity of log messages that should be sent
|
|
|
|
* to this logger.
|
|
|
|
*/
|
2012-07-27 16:05:02 +02:00
|
|
|
Logger::Logger(const Dictionary::Ptr& properties)
|
2012-07-30 10:17:29 +02:00
|
|
|
: DynamicObject(properties)
|
2012-07-27 16:05:02 +02:00
|
|
|
{
|
|
|
|
if (!IsLocal())
|
|
|
|
throw_exception(runtime_error("Logger objects must be local."));
|
|
|
|
|
|
|
|
string type;
|
|
|
|
if (!GetProperty("type", &type))
|
|
|
|
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 */
|
|
|
|
throw_exception(invalid_argument("Syslog is not supported on Windows."));
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
} else if (type == "file") {
|
|
|
|
string path;
|
|
|
|
if (!GetProperty("path", &path))
|
|
|
|
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 {
|
|
|
|
throw_exception(runtime_error("Unknown log type: " + type));
|
|
|
|
}
|
|
|
|
|
|
|
|
impl->m_Config = this;
|
|
|
|
SetImplementation(impl);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
LogEntry entry;
|
2012-07-25 12:59:17 +02:00
|
|
|
entry.Timestamp = Utility::GetTime();
|
2012-07-10 12:21:19 +02:00
|
|
|
entry.Severity = severity;
|
|
|
|
entry.Facility = facility;
|
|
|
|
entry.Message = message;
|
|
|
|
|
2012-07-13 23:33:30 +02:00
|
|
|
Event::Post(boost::bind(&Logger::ForwardLogEntry, entry));
|
2012-07-10 12:21:19 +02:00
|
|
|
}
|
|
|
|
|
2012-07-10 12:51:53 +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
|
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
string strSeverity;
|
|
|
|
LogSeverity severity = LogInformation;
|
|
|
|
if (GetProperty("severity", &strSeverity))
|
|
|
|
severity = Logger::StringToSeverity(strSeverity);
|
|
|
|
|
|
|
|
return severity;
|
2012-07-10 12:21:19 +02:00
|
|
|
}
|
|
|
|
|
2012-07-10 12:51:53 +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)
|
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr object;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, object), DynamicObject::GetObjects("Logger")) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Logger::Ptr logger = dynamic_pointer_cast<Logger>(object);
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
if (entry.Severity >= logger->GetMinSeverity())
|
2012-07-27 16:05:02 +02:00
|
|
|
logger->GetImplementation()->ProcessLogEntry(entry);
|
2012-07-10 12:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-13 09:03:22 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
ILogger::Ptr Logger::GetImplementation(void) const
|
|
|
|
{
|
|
|
|
ILogger::Ptr impl;
|
|
|
|
GetTag("impl", &impl);
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::SetImplementation(const ILogger::Ptr& impl)
|
|
|
|
{
|
|
|
|
SetTag("impl", impl);
|
|
|
|
}
|
|
|
|
|
2012-07-13 09:03:22 +02:00
|
|
|
string Logger::SeverityToString(LogSeverity severity)
|
|
|
|
{
|
|
|
|
switch (severity) {
|
|
|
|
case LogDebug:
|
|
|
|
return "debug";
|
|
|
|
case LogInformation:
|
|
|
|
return "information";
|
|
|
|
case LogWarning:
|
|
|
|
return "warning";
|
|
|
|
case LogCritical:
|
|
|
|
return "critical";
|
|
|
|
default:
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Invalid severity."));
|
2012-07-13 09:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Invalid severity: " + severity));
|
2012-07-13 09:03:22 +02:00
|
|
|
}
|