2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-07-10 12:21:19 +02:00
|
|
|
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "base/logger.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "base/logger-ti.cpp"
|
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-17 22:14:40 +01:00
|
|
|
#include <iostream>
|
2012-07-10 12:21:19 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
template Log& Log::operator<<(const Value&);
|
|
|
|
template Log& Log::operator<<(const String&);
|
|
|
|
template Log& Log::operator<<(const std::string&);
|
|
|
|
template Log& Log::operator<<(const bool&);
|
|
|
|
template Log& Log::operator<<(const unsigned int&);
|
|
|
|
template Log& Log::operator<<(const int&);
|
|
|
|
template Log& Log::operator<<(const unsigned long&);
|
|
|
|
template Log& Log::operator<<(const long&);
|
|
|
|
template Log& Log::operator<<(const double&);
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(Logger);
|
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
|
|
|
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE([]() {
|
2018-08-07 13:55:41 +02:00
|
|
|
ScriptGlobal::Set("System.LogDebug", LogDebug, true);
|
|
|
|
ScriptGlobal::Set("System.LogNotice", LogNotice, true);
|
|
|
|
ScriptGlobal::Set("System.LogInformation", LogInformation, true);
|
|
|
|
ScriptGlobal::Set("System.LogWarning", LogWarning, true);
|
|
|
|
ScriptGlobal::Set("System.LogCritical", LogCritical, true);
|
2016-08-27 09:35:08 +02:00
|
|
|
});
|
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
|
|
|
*/
|
2015-08-20 17:18:48 +02:00
|
|
|
void Logger::Start(bool runtimeCreated)
|
2013-03-01 12:07:52 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<Logger>::Start(runtimeCreated);
|
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
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void Logger::Stop(bool runtimeRemoved)
|
2013-06-06 11:26:30 +02:00
|
|
|
{
|
2014-11-13 09:02:23 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_Loggers.erase(this);
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<Logger>::Stop(runtimeRemoved);
|
2013-06-06 11:26:30 +02:00
|
|
|
}
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
std::set<Logger::Ptr> Logger::GetLoggers()
|
2013-06-06 11:26:30 +02:00
|
|
|
{
|
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
|
|
|
|
2013-06-06 11:26:30 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the minimum severity for this logger.
|
|
|
|
*
|
|
|
|
* @returns The minimum severity.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
LogSeverity Logger::GetMinSeverity() const
|
2013-06-06 11:26:30 +02:00
|
|
|
{
|
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:
|
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;
|
2016-04-21 13:50:47 +02:00
|
|
|
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
|
|
|
}
|
2013-12-16 17:05:23 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Logger::DisableConsoleLog()
|
2013-12-16 17:05:23 +01:00
|
|
|
{
|
|
|
|
m_ConsoleLogEnabled = false;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Logger::EnableConsoleLog()
|
2015-02-19 17:12:32 +01:00
|
|
|
{
|
2015-03-02 12:44:52 +01:00
|
|
|
m_ConsoleLogEnabled = true;
|
2015-02-19 17:12:32 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool Logger::IsConsoleLogEnabled()
|
2013-12-16 17:05:23 +01:00
|
|
|
{
|
|
|
|
return m_ConsoleLogEnabled;
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:05:04 +02:00
|
|
|
void Logger::SetConsoleLogSeverity(LogSeverity logSeverity)
|
|
|
|
{
|
|
|
|
m_ConsoleLogSeverity = logSeverity;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
LogSeverity Logger::GetConsoleLogSeverity()
|
2014-05-23 18:05:04 +02:00
|
|
|
{
|
|
|
|
return m_ConsoleLogSeverity;
|
2014-06-05 15:35:30 +02:00
|
|
|
}
|
2014-10-31 22:01:36 +01:00
|
|
|
|
2018-05-03 11:35:29 +02:00
|
|
|
void Logger::DisableTimestamp()
|
2014-10-31 22:01:36 +01:00
|
|
|
{
|
2018-05-03 11:35:29 +02:00
|
|
|
m_TimestampEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::EnableTimestamp()
|
|
|
|
{
|
|
|
|
m_TimestampEnabled = true;
|
2014-10-31 22:01:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool Logger::IsTimestampEnabled()
|
2014-10-31 22:01:36 +01:00
|
|
|
{
|
|
|
|
return m_TimestampEnabled;
|
|
|
|
}
|
2016-04-21 13:50:47 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void Logger::ValidateSeverity(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2016-04-21 13:50:47 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<Logger>::ValidateSeverity(lvalue, utils);
|
2016-04-21 13:50:47 +02:00
|
|
|
|
|
|
|
try {
|
2018-01-11 07:08:09 +01:00
|
|
|
StringToSeverity(lvalue());
|
2016-04-21 13:50:47 +02:00
|
|
|
} catch (...) {
|
2018-01-11 07:08:09 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "severity" }, "Invalid severity specified: " + lvalue()));
|
2016-04-21 13:50:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-03 06:01:02 +01:00
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Log::Log(LogSeverity severity, String facility, const String& message)
|
|
|
|
: m_Severity(severity), m_Facility(std::move(facility))
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
|
|
|
m_Buffer << message;
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Log::Log(LogSeverity severity, String facility)
|
|
|
|
: m_Severity(severity), m_Facility(std::move(facility))
|
2018-01-03 06:01:02 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the message to the application's log.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
Log::~Log()
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
|
|
|
LogEntry entry;
|
|
|
|
entry.Timestamp = Utility::GetTime();
|
|
|
|
entry.Severity = m_Severity;
|
|
|
|
entry.Facility = m_Facility;
|
|
|
|
entry.Message = m_Buffer.str();
|
|
|
|
|
|
|
|
if (m_Severity >= LogWarning) {
|
|
|
|
ContextTrace context;
|
|
|
|
|
|
|
|
if (context.GetLength() > 0) {
|
|
|
|
std::ostringstream trace;
|
|
|
|
trace << context;
|
|
|
|
entry.Message += "\nContext:" + trace.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Logger::Ptr& logger : Logger::GetLoggers()) {
|
|
|
|
ObjectLock llock(logger);
|
|
|
|
|
|
|
|
if (!logger->IsActive())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (entry.Severity >= logger->GetMinSeverity())
|
|
|
|
logger->ProcessLogEntry(entry);
|
2018-07-19 12:51:30 +02:00
|
|
|
|
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
/* Always flush, don't depend on the timer. Enable this for development sprints. */
|
|
|
|
//logger->Flush();
|
|
|
|
#endif /* I2_DEBUG */
|
2018-01-03 06:01:02 +01:00
|
|
|
}
|
|
|
|
|
2018-05-28 22:08:57 +02:00
|
|
|
if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity()) {
|
2018-01-03 06:01:02 +01:00
|
|
|
StreamLogger::ProcessLogEntry(std::cout, entry);
|
2018-05-28 22:08:57 +02:00
|
|
|
|
|
|
|
/* "Console" might be a pipe/socket (systemd, daemontools, docker, ...),
|
|
|
|
* then cout will not flush lines automatically. */
|
|
|
|
std::cout << std::flush;
|
|
|
|
}
|
2018-01-03 06:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Log& Log::operator<<(const char *val)
|
|
|
|
{
|
|
|
|
m_Buffer << val;
|
|
|
|
return *this;
|
|
|
|
}
|