-/******************************************************************************
- * Icinga 2 *
- * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
- * *
- * 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. *
- ******************************************************************************/
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/logger.hpp"
-#include "base/logger.tcpp"
+#include "base/logger-ti.cpp"
#include "base/application.hpp"
#include "base/streamlogger.hpp"
#include "base/configtype.hpp"
using namespace icinga;
+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&);
+
REGISTER_TYPE(Logger);
std::set<Logger::Ptr> Logger::m_Loggers;
LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
INITIALIZE_ONCE([]() {
- ScriptGlobal::Set("LogDebug", LogDebug);
- ScriptGlobal::Set("LogNotice", LogNotice);
- ScriptGlobal::Set("LogInformation", LogInformation);
- ScriptGlobal::Set("LogWarning", LogWarning);
- ScriptGlobal::Set("LogCritical", LogCritical);
+ 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);
});
/**
ObjectImpl<Logger>::Stop(runtimeRemoved);
}
-std::set<Logger::Ptr> Logger::GetLoggers(void)
+std::set<Logger::Ptr> Logger::GetLoggers()
{
boost::mutex::scoped_lock lock(m_Mutex);
return m_Loggers;
}
-/**
- * Writes a message to the application's log.
- *
- * @param severity The message severity.
- * @param facility The log facility.
- * @param message The message.
- */
-void icinga::IcingaLog(LogSeverity severity, const String& facility,
- const String& message)
-{
- LogEntry entry;
- entry.Timestamp = Utility::GetTime();
- entry.Severity = severity;
- entry.Facility = facility;
- entry.Message = message;
-
- if (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);
- }
-
- if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity())
- StreamLogger::ProcessLogEntry(std::cout, entry);
-}
-
/**
* Retrieves the minimum severity for this logger.
*
* @returns The minimum severity.
*/
-LogSeverity Logger::GetMinSeverity(void) const
+LogSeverity Logger::GetMinSeverity() const
{
String severity = GetSeverity();
if (severity.IsEmpty())
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid severity: " + severity));
}
-void Logger::DisableConsoleLog(void)
+void Logger::DisableConsoleLog()
{
m_ConsoleLogEnabled = false;
}
-void Logger::EnableConsoleLog(void)
+void Logger::EnableConsoleLog()
{
m_ConsoleLogEnabled = true;
}
-bool Logger::IsConsoleLogEnabled(void)
+bool Logger::IsConsoleLogEnabled()
{
return m_ConsoleLogEnabled;
}
m_ConsoleLogSeverity = logSeverity;
}
-LogSeverity Logger::GetConsoleLogSeverity(void)
+LogSeverity Logger::GetConsoleLogSeverity()
{
return m_ConsoleLogSeverity;
}
-void Logger::DisableTimestamp(bool disable)
+void Logger::DisableTimestamp()
{
- m_TimestampEnabled = !disable;
+ m_TimestampEnabled = false;
}
-bool Logger::IsTimestampEnabled(void)
+void Logger::EnableTimestamp()
+{
+ m_TimestampEnabled = true;
+}
+
+bool Logger::IsTimestampEnabled()
{
return m_TimestampEnabled;
}
-void Logger::ValidateSeverity(const String& value, const ValidationUtils& utils)
+void Logger::ValidateSeverity(const Lazy<String>& lvalue, const ValidationUtils& utils)
{
- ObjectImpl<Logger>::ValidateSeverity(value, utils);
+ ObjectImpl<Logger>::ValidateSeverity(lvalue, utils);
try {
- StringToSeverity(value);
+ StringToSeverity(lvalue());
} catch (...) {
- BOOST_THROW_EXCEPTION(ValidationError(this, { "severity" }, "Invalid severity specified: " + value));
+ BOOST_THROW_EXCEPTION(ValidationError(this, { "severity" }, "Invalid severity specified: " + lvalue()));
}
}
+
+Log::Log(LogSeverity severity, String facility, const String& message)
+ : m_Severity(severity), m_Facility(std::move(facility))
+{
+ m_Buffer << message;
+}
+
+Log::Log(LogSeverity severity, String facility)
+ : m_Severity(severity), m_Facility(std::move(facility))
+{ }
+
+/**
+ * Writes the message to the application's log.
+ */
+Log::~Log()
+{
+ 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);
+
+#ifdef I2_DEBUG /* I2_DEBUG */
+ /* Always flush, don't depend on the timer. Enable this for development sprints on Linux/macOS only. Windows crashes. */
+ //logger->Flush();
+#endif /* I2_DEBUG */
+ }
+
+ if (Logger::IsConsoleLogEnabled() && entry.Severity >= Logger::GetConsoleLogSeverity()) {
+ StreamLogger::ProcessLogEntry(std::cout, entry);
+
+ /* "Console" might be a pipe/socket (systemd, daemontools, docker, ...),
+ * then cout will not flush lines automatically. */
+ std::cout << std::flush;
+ }
+}
+
+Log& Log::operator<<(const char *val)
+{
+ m_Buffer << val;
+ return *this;
+}