2012-07-10 12:21:19 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-07-10 12:21:19 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/logger.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/streamlogger.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/context.hpp"
|
2014-12-14 11:33:45 +01:00
|
|
|
#include "base/scriptglobal.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#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-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(Logger);
|
2014-04-01 09:33:54 +02:00
|
|
|
INITIALIZE_ONCE(&Logger::StaticInitialize);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
std::set<Logger::Ptr> Logger::m_Loggers;
|
|
|
|
boost::mutex Logger::m_Mutex;
|
2013-12-16 17:05:23 +01:00
|
|
|
bool Logger::m_ConsoleLogEnabled = true;
|
2014-10-31 22:01:36 +01:00
|
|
|
bool Logger::m_TimestampEnabled = true;
|
2014-05-23 18:05:04 +02:00
|
|
|
LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
|
2012-07-10 12:21:19 +02:00
|
|
|
|
2014-04-01 09:33:54 +02:00
|
|
|
void Logger::StaticInitialize(void)
|
|
|
|
{
|
2014-12-14 11:33:45 +01:00
|
|
|
ScriptGlobal::Set("LogDebug", LogDebug);
|
|
|
|
ScriptGlobal::Set("LogNotice", LogNotice);
|
|
|
|
ScriptGlobal::Set("LogInformation", LogInformation);
|
|
|
|
ScriptGlobal::Set("LogWarning", LogWarning);
|
|
|
|
ScriptGlobal::Set("LogCritical", LogCritical);
|
2014-04-01 09:33:54 +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)
|
|
|
|
{
|
2015-08-25 13:53:43 +02:00
|
|
|
ObjectImpl<Logger>::Start();
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-12-16 09:53:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2014-11-08 21:17:16 +01:00
|
|
|
m_Loggers.insert(this);
|
2013-06-06 11:26:30 +02:00
|
|
|
}
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
void Logger::Stop(void)
|
|
|
|
{
|
2014-11-13 09:02:23 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_Loggers.erase(this);
|
|
|
|
}
|
|
|
|
|
2015-08-25 13:53:43 +02:00
|
|
|
ObjectImpl<Logger>::Stop();
|
2013-06-06 11:26:30 +02:00
|
|
|
}
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
std::set<Logger::Ptr> Logger::GetLoggers(void)
|
|
|
|
{
|
2013-12-16 09:53:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-06-06 11:26:30 +02:00
|
|
|
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.
|
|
|
|
*/
|
2014-10-19 17:52:17 +02:00
|
|
|
void icinga::IcingaLog(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;
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
if (severity >= LogWarning) {
|
|
|
|
ContextTrace context;
|
|
|
|
|
|
|
|
if (context.GetLength() > 0) {
|
|
|
|
std::ostringstream trace;
|
|
|
|
trace << context;
|
|
|
|
entry.Message += "\nContext:" + trace.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) {
|
2013-12-16 17:05:23 +01:00
|
|
|
ObjectLock llock(logger);
|
2013-12-15 18:15:05 +01:00
|
|
|
|
2013-12-16 17:05:23 +01:00
|
|
|
if (!logger->IsActive())
|
|
|
|
continue;
|
2013-02-18 23:44:24 +01:00
|
|
|
|
2013-12-16 17:05:23 +01:00
|
|
|
if (entry.Severity >= logger->GetMinSeverity())
|
|
|
|
logger->ProcessLogEntry(entry);
|
2012-07-10 12:21:19 +02:00
|
|
|
}
|
2012-08-14 12:51:51 +02:00
|
|
|
|
2014-10-17 09:45:46 +02:00
|
|
|
if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity())
|
|
|
|
StreamLogger::ProcessLogEntry(std::cout, 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
|
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
String severity = GetSeverity();
|
2013-06-06 11:26:30 +02:00
|
|
|
if (severity.IsEmpty())
|
|
|
|
return LogInformation;
|
2014-06-05 15:35:30 +02:00
|
|
|
else {
|
|
|
|
LogSeverity ls = LogInformation;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ls = Logger::StringToSeverity(severity);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) { /* use the default level */ }
|
2014-06-05 15:35:30 +02:00
|
|
|
|
|
|
|
return ls;
|
|
|
|
}
|
2013-06-06 11:26:30 +02:00
|
|
|
}
|
|
|
|
|
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";
|
2014-05-22 12:12:15 +02:00
|
|
|
case LogNotice:
|
|
|
|
return "notice";
|
2012-07-13 09:03:22 +02:00
|
|
|
case LogInformation:
|
|
|
|
return "information";
|
|
|
|
case LogWarning:
|
|
|
|
return "warning";
|
|
|
|
case LogCritical:
|
|
|
|
return "critical";
|
|
|
|
default:
|
2014-06-05 15:35:30 +02:00
|
|
|
Log(LogCritical, "Logger", "Invalid severity.");
|
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;
|
2014-05-22 12:12:15 +02:00
|
|
|
else if (severity == "notice")
|
|
|
|
return LogNotice;
|
2012-07-13 09:03:22 +02:00
|
|
|
else if (severity == "information")
|
|
|
|
return LogInformation;
|
|
|
|
else if (severity == "warning")
|
|
|
|
return LogWarning;
|
|
|
|
else if (severity == "critical")
|
|
|
|
return LogCritical;
|
2014-06-05 15:35:30 +02:00
|
|
|
else {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogCritical, "Logger")
|
|
|
|
<< "Invalid severity: '" << severity << "'.";
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
|
2014-06-05 15:35:30 +02:00
|
|
|
}
|
2012-07-13 09:03:22 +02:00
|
|
|
}
|
2013-12-16 17:05:23 +01:00
|
|
|
|
|
|
|
void Logger::DisableConsoleLog(void)
|
|
|
|
{
|
|
|
|
m_ConsoleLogEnabled = false;
|
|
|
|
}
|
|
|
|
|
2015-02-19 17:12:32 +01:00
|
|
|
void Logger::EnableConsoleLog(void)
|
|
|
|
{
|
2015-03-02 12:44:52 +01:00
|
|
|
m_ConsoleLogEnabled = true;
|
2015-02-19 17:12:32 +01:00
|
|
|
}
|
|
|
|
|
2013-12-16 17:05:23 +01:00
|
|
|
bool Logger::IsConsoleLogEnabled(void)
|
|
|
|
{
|
|
|
|
return m_ConsoleLogEnabled;
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:05:04 +02:00
|
|
|
void Logger::SetConsoleLogSeverity(LogSeverity logSeverity)
|
|
|
|
{
|
|
|
|
m_ConsoleLogSeverity = logSeverity;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogSeverity Logger::GetConsoleLogSeverity(void)
|
|
|
|
{
|
|
|
|
return m_ConsoleLogSeverity;
|
2014-06-05 15:35:30 +02:00
|
|
|
}
|
2014-10-31 22:01:36 +01:00
|
|
|
|
|
|
|
void Logger::DisableTimestamp(bool disable)
|
|
|
|
{
|
|
|
|
m_TimestampEnabled = !disable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Logger::IsTimestampEnabled(void)
|
|
|
|
{
|
|
|
|
return m_TimestampEnabled;
|
|
|
|
}
|