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-10 15:14:45 +02:00
|
|
|
set<Logger::Ptr> Logger::m_Loggers;
|
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-10 12:21:19 +02:00
|
|
|
Logger::Logger(LogSeverity minSeverity)
|
|
|
|
: m_MinSeverity(minSeverity)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
time(&entry.Timestamp);
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Registers a new logger.
|
|
|
|
*
|
|
|
|
* @param logger The logger.
|
|
|
|
*/
|
2012-07-10 12:21:19 +02:00
|
|
|
void Logger::RegisterLogger(const Logger::Ptr& logger)
|
|
|
|
{
|
2012-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-10 15:14:45 +02:00
|
|
|
m_Loggers.insert(logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters a logger.
|
|
|
|
*
|
|
|
|
* @param logger The logger.
|
|
|
|
*/
|
|
|
|
void Logger::UnregisterLogger(const Logger::Ptr& logger)
|
|
|
|
{
|
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
|
|
|
m_Loggers.erase(logger);
|
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
|
|
|
|
{
|
|
|
|
return m_MinSeverity;
|
|
|
|
}
|
|
|
|
|
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-10 15:14:45 +02:00
|
|
|
set<Logger::Ptr>::iterator it;
|
2012-07-10 12:21:19 +02:00
|
|
|
for (it = m_Loggers.begin(); it != m_Loggers.end(); it++) {
|
|
|
|
Logger::Ptr logger = *it;
|
|
|
|
|
|
|
|
if (entry.Severity >= logger->GetMinSeverity())
|
|
|
|
logger->ProcessLogEntry(entry);
|
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
throw invalid_argument("Invalid 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
|
|
|
|
throw invalid_argument("Invalid severity: " + severity);
|
|
|
|
}
|