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"
|
2021-04-08 13:49:53 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "base/windowseventloglogger.hpp"
|
|
|
|
#endif /* _WIN32 */
|
2022-09-08 12:09:25 +02:00
|
|
|
#include <algorithm>
|
2013-03-17 22:14:40 +01:00
|
|
|
#include <iostream>
|
2020-02-19 11:27:58 +01:00
|
|
|
#include <utility>
|
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;
|
2021-02-02 10:16:04 +01:00
|
|
|
std::mutex Logger::m_Mutex;
|
2013-12-16 17:05:23 +01:00
|
|
|
bool Logger::m_ConsoleLogEnabled = true;
|
2021-04-08 13:49:53 +02:00
|
|
|
std::atomic<bool> Logger::m_EarlyLoggingEnabled (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;
|
2022-09-08 12:09:25 +02:00
|
|
|
std::mutex Logger::m_UpdateMinLogSeverityMutex;
|
|
|
|
Atomic<LogSeverity> Logger::m_MinLogSeverity (LogDebug);
|
2012-07-10 12:21:19 +02:00
|
|
|
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE([]() {
|
2023-01-09 17:09:46 +01:00
|
|
|
ScriptGlobal::Set("System.LogDebug", LogDebug);
|
|
|
|
ScriptGlobal::Set("System.LogNotice", LogNotice);
|
|
|
|
ScriptGlobal::Set("System.LogInformation", LogInformation);
|
|
|
|
ScriptGlobal::Set("System.LogWarning", LogWarning);
|
|
|
|
ScriptGlobal::Set("System.LogCritical", LogCritical);
|
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
|
|
|
|
2022-09-08 12:09:25 +02:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_Mutex);
|
|
|
|
m_Loggers.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateMinLogSeverity();
|
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
|
|
|
{
|
2021-02-02 10:16:04 +01:00
|
|
|
std::unique_lock<std::mutex> lock(m_Mutex);
|
2014-11-13 09:02:23 +01:00
|
|
|
m_Loggers.erase(this);
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:09:25 +02:00
|
|
|
UpdateMinLogSeverity();
|
|
|
|
|
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
|
|
|
{
|
2021-02-02 10:16:04 +01:00
|
|
|
std::unique_lock<std::mutex> 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;
|
2022-09-08 12:09:25 +02:00
|
|
|
|
|
|
|
UpdateMinLogSeverity();
|
2013-12-16 17:05:23 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2022-09-08 12:09:25 +02:00
|
|
|
|
|
|
|
UpdateMinLogSeverity();
|
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
|
|
|
|
2021-04-08 13:49:53 +02:00
|
|
|
void Logger::DisableEarlyLogging() {
|
|
|
|
m_EarlyLoggingEnabled = false;
|
2022-09-08 12:09:25 +02:00
|
|
|
|
|
|
|
UpdateMinLogSeverity();
|
2021-04-08 13:49:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Logger::IsEarlyLoggingEnabled() {
|
|
|
|
return m_EarlyLoggingEnabled;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-08 12:09:25 +02:00
|
|
|
void Logger::SetSeverity(const String& value, bool suppress_events, const Value& cookie)
|
|
|
|
{
|
|
|
|
ObjectImpl<Logger>::SetSeverity(value, suppress_events, cookie);
|
|
|
|
|
|
|
|
UpdateMinLogSeverity();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-08 12:09:25 +02:00
|
|
|
void Logger::UpdateMinLogSeverity()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock (m_UpdateMinLogSeverityMutex);
|
|
|
|
auto result (LogNothing);
|
|
|
|
|
|
|
|
for (auto& logger : Logger::GetLoggers()) {
|
|
|
|
ObjectLock llock (logger);
|
|
|
|
|
|
|
|
if (logger->IsActive()) {
|
|
|
|
result = std::min(result, logger->GetMinSeverity());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Logger::IsConsoleLogEnabled()) {
|
|
|
|
result = std::min(result, Logger::GetConsoleLogSeverity());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (Logger::IsEarlyLoggingEnabled()) {
|
|
|
|
result = std::min(result, LogCritical);
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
m_MinLogSeverity.store(result);
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Log::Log(LogSeverity severity, String facility, const String& message)
|
2022-09-08 12:09:25 +02:00
|
|
|
: Log(severity, std::move(facility))
|
2018-01-03 06:01:02 +01:00
|
|
|
{
|
2022-09-08 12:09:25 +02:00
|
|
|
if (!m_IsNoOp) {
|
|
|
|
m_Buffer << message;
|
|
|
|
}
|
2018-01-03 06:01:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
Log::Log(LogSeverity severity, String facility)
|
2022-09-08 12:09:25 +02:00
|
|
|
: m_Severity(severity), m_Facility(std::move(facility)), m_IsNoOp(severity < Logger::GetMinLogSeverity())
|
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
|
|
|
{
|
2022-09-08 12:09:25 +02:00
|
|
|
if (m_IsNoOp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
LogEntry entry;
|
|
|
|
entry.Timestamp = Utility::GetTime();
|
|
|
|
entry.Severity = m_Severity;
|
|
|
|
entry.Facility = m_Facility;
|
2020-02-19 11:27:58 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
auto msg (m_Buffer.str());
|
|
|
|
msg.erase(msg.find_last_not_of("\n") + 1u);
|
|
|
|
|
|
|
|
entry.Message = std::move(msg);
|
|
|
|
}
|
2018-01-03 06:01:02 +01:00
|
|
|
|
|
|
|
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 */
|
2019-09-06 09:24:34 +02:00
|
|
|
/* Always flush, don't depend on the timer. Enable this for development sprints on Linux/macOS only. Windows crashes. */
|
2018-07-19 12:51:30 +02:00
|
|
|
//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;
|
|
|
|
}
|
2021-04-08 13:49:53 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2022-07-14 14:07:56 +02:00
|
|
|
if (Logger::IsEarlyLoggingEnabled() && entry.Severity >= LogCritical) {
|
2021-04-08 13:49:53 +02:00
|
|
|
WindowsEventLogLogger::WriteToWindowsEventLog(entry);
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
2018-01-03 06:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Log& Log::operator<<(const char *val)
|
|
|
|
{
|
2022-09-08 12:09:25 +02:00
|
|
|
if (!m_IsNoOp) {
|
|
|
|
m_Buffer << val;
|
|
|
|
}
|
|
|
|
|
2018-01-03 06:01:02 +01:00
|
|
|
return *this;
|
|
|
|
}
|