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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/application.h"
|
|
|
|
#include "base/streamlogger.h"
|
|
|
|
#include "base/sysloglogger.h"
|
|
|
|
#include "base/dynamictype.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-03-17 22:14:40 +01:00
|
|
|
#include <iostream>
|
2012-07-10 12:21:19 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
std::set<Logger::Ptr> Logger::m_Loggers;
|
|
|
|
boost::mutex Logger::m_Mutex;
|
2012-07-10 12:21:19 +02:00
|
|
|
|
2012-07-10 12:51:53 +02:00
|
|
|
/**
|
2012-09-17 13:35:55 +02:00
|
|
|
* Constructor for the Logger class.
|
2012-07-10 12:51:53 +02:00
|
|
|
*/
|
2013-03-01 12:07:52 +01:00
|
|
|
void Logger::Start(void)
|
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
boost::mutex::scoped_lock(m_Mutex);
|
|
|
|
m_Loggers.insert(GetSelf());
|
|
|
|
}
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
void Logger::Stop(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock(m_Mutex);
|
|
|
|
m_Loggers.erase(GetSelf());
|
|
|
|
}
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
std::set<Logger::Ptr> Logger::GetLoggers(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock(m_Mutex);
|
|
|
|
return m_Loggers;
|
2012-07-27 16:05:02 +02: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.
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
void icinga::Log(LogSeverity severity, const String& facility,
|
2012-08-02 09:38:08 +02:00
|
|
|
const String& message)
|
2012-07-10 12:21:19 +02:00
|
|
|
{
|
|
|
|
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-08-14 12:51:51 +02:00
|
|
|
bool processed = false;
|
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) {
|
2013-02-18 23:44:24 +01:00
|
|
|
{
|
|
|
|
ObjectLock llock(logger);
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
if (entry.Severity >= logger->GetMinSeverity())
|
2013-06-06 11:26:30 +02:00
|
|
|
logger->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
|
|
|
}
|
2012-08-14 12:51:51 +02:00
|
|
|
|
2013-02-02 23:22:27 +01: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-07-13 09:03:22 +02:00
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the minimum severity for this logger.
|
|
|
|
*
|
|
|
|
* @returns The minimum severity.
|
|
|
|
*/
|
|
|
|
LogSeverity Logger::GetMinSeverity(void) const
|
|
|
|
{
|
|
|
|
String severity = m_Severity;
|
|
|
|
if (severity.IsEmpty())
|
|
|
|
return LogInformation;
|
|
|
|
else
|
|
|
|
return Logger::StringToSeverity(severity);
|
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
|
|
|
* Converts a severity enum value to a string.
|
|
|
|
*
|
|
|
|
* @param severity The severity value.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String Logger::SeverityToString(LogSeverity severity)
|
2012-07-13 09:03:22 +02:00
|
|
|
{
|
|
|
|
switch (severity) {
|
|
|
|
case LogDebug:
|
|
|
|
return "debug";
|
|
|
|
case LogInformation:
|
|
|
|
return "information";
|
|
|
|
case LogWarning:
|
|
|
|
return "warning";
|
|
|
|
case LogCritical:
|
|
|
|
return "critical";
|
|
|
|
default:
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity."));
|
2012-07-13 09:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
|
|
|
* Converts a string to a severity enum value.
|
|
|
|
*
|
|
|
|
* @param severity The severity.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
LogSeverity Logger::StringToSeverity(const String& severity)
|
2012-07-13 09:03:22 +02:00
|
|
|
{
|
|
|
|
if (severity == "debug")
|
|
|
|
return LogDebug;
|
|
|
|
else if (severity == "information")
|
|
|
|
return LogInformation;
|
|
|
|
else if (severity == "warning")
|
|
|
|
return LogWarning;
|
|
|
|
else if (severity == "critical")
|
|
|
|
return LogCritical;
|
|
|
|
else
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
|
2012-07-13 09:03:22 +02:00
|
|
|
}
|
2012-09-17 14:47:43 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Logger::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
|
|
|
|
{
|
|
|
|
DynamicObject::InternalSerialize(bag, attributeTypes);
|
|
|
|
|
2013-08-29 10:40:43 +02:00
|
|
|
if (attributeTypes & Attribute_Config)
|
|
|
|
bag->Set("severity", m_Severity);
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
|
|
|
|
{
|
|
|
|
DynamicObject::InternalDeserialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config)
|
|
|
|
m_Severity = bag->Get("severity");
|
|
|
|
}
|