icinga2/lib/base/logger.hpp

157 lines
3.7 KiB
C++
Raw Permalink Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
2012-07-10 06:21:19 -04:00
#ifndef LOGGER_H
#define LOGGER_H
#include "base/atomic.hpp"
2014-05-25 10:23:35 -04:00
#include "base/i2-base.hpp"
2018-01-18 07:50:38 -05:00
#include "base/logger-ti.hpp"
#include <optional>
#include <set>
2021-02-02 04:44:02 -05:00
#include <sstream>
2012-07-10 06:21:19 -04:00
2013-03-16 16:18:53 -04:00
namespace icinga
2012-07-10 06:21:19 -04:00
{
2014-10-19 08:21:12 -04:00
/**
* Log severity.
*
* @ingroup base
*/
enum LogSeverity
{
LogDebug,
LogNotice,
LogInformation,
LogWarning,
LogCritical,
// Just for internal comparision
LogNothing,
2014-10-19 08:21:12 -04:00
};
/**
2013-11-19 01:14:04 -05:00
* A log entry.
*
* @ingroup base
*/
2012-07-10 06:21:19 -04:00
struct LogEntry {
2012-09-17 08:47:43 -04:00
double Timestamp; /**< The timestamp when this log entry was created. */
LogSeverity Severity; /**< The severity of this log entry. */
String Facility; /**< The facility this log entry belongs to. */
String Message; /**< The log entry's message. */
2012-07-10 06:21:19 -04:00
};
/**
* A log provider.
2012-09-17 07:35:55 -04:00
*
* @ingroup base
*/
2017-12-31 01:22:16 -05:00
class Logger : public ObjectImpl<Logger>
2012-07-10 06:21:19 -04:00
{
public:
2014-11-02 18:44:04 -05:00
DECLARE_OBJECT(Logger);
2012-07-10 06:21:19 -04:00
static String SeverityToString(LogSeverity severity);
static LogSeverity StringToSeverity(const String& severity);
LogSeverity GetMinSeverity() const;
2012-07-10 06:21:19 -04:00
/**
* Processes the log entry and writes it to the log that is
* represented by this ILogger object.
*
* @param entry The log entry that is to be processed.
*/
virtual void ProcessLogEntry(const LogEntry& entry) = 0;
virtual void Flush() = 0;
static std::set<Logger::Ptr> GetLoggers();
static void DisableConsoleLog();
static void EnableConsoleLog();
static bool IsConsoleLogEnabled();
static void DisableEarlyLogging();
static bool IsEarlyLoggingEnabled();
static void DisableTimestamp();
static void EnableTimestamp();
static bool IsTimestampEnabled();
static void SetConsoleLogSeverity(LogSeverity logSeverity);
static LogSeverity GetConsoleLogSeverity();
static inline
LogSeverity GetMinLogSeverity()
{
return m_MinLogSeverity.load();
}
void SetSeverity(const String& value, bool suppress_events = false, const Value& cookie = Empty) override;
2025-10-17 08:08:06 -04:00
void ValidateSeverity(const Lazy<String>& lvalue, const ValidationUtils& utils) override final;
2013-03-01 06:07:52 -05:00
protected:
void Start(bool runtimeCreated) override;
void Stop(bool runtimeRemoved) override;
2013-03-01 06:07:52 -05:00
2012-07-10 06:21:19 -04:00
private:
static void UpdateMinLogSeverity();
2021-02-02 04:16:04 -05:00
static std::mutex m_Mutex;
static std::set<Logger::Ptr> m_Loggers;
static bool m_ConsoleLogEnabled;
static std::atomic<bool> m_EarlyLoggingEnabled;
static bool m_TimestampEnabled;
static LogSeverity m_ConsoleLogSeverity;
static std::mutex m_UpdateMinLogSeverityMutex;
static Atomic<LogSeverity> m_MinLogSeverity;
2012-07-10 06:21:19 -04:00
};
2014-10-19 11:52:17 -04:00
class Log
{
public:
Log() = delete;
Log(const Log& other) = delete;
Log& operator=(const Log& rhs) = delete;
2014-10-19 11:52:17 -04:00
Log(LogSeverity severity, String facility, const String& message);
Log(LogSeverity severity, String facility);
2014-10-19 11:52:17 -04:00
~Log();
2014-10-19 11:52:17 -04:00
template<typename T>
Log& operator<<(T&& val)
2014-10-19 11:52:17 -04:00
{
if (m_Buffer) {
*m_Buffer << std::forward<T>(val);
}
2014-10-19 11:52:17 -04:00
return *this;
}
private:
LogSeverity m_Severity;
String m_Facility;
/**
* Stream for incrementally generating the log message. If the message will be discarded as it's level currently
* isn't logged, it will be empty as the stream doesn't need to be initialized in this case.
*/
std::optional<std::ostringstream> m_Buffer;
2014-10-19 11:52:17 -04:00
};
extern template Log& Log::operator<<(const Value&);
extern template Log& Log::operator<<(const String&);
extern template Log& Log::operator<<(const std::string&);
extern template Log& Log::operator<<(const bool&);
extern template Log& Log::operator<<(const unsigned int&);
extern template Log& Log::operator<<(const int&);
extern template Log& Log::operator<<(const unsigned long&);
extern template Log& Log::operator<<(const long&);
extern template Log& Log::operator<<(const double&);
extern template Log& Log::operator<<(const char*&);
2012-07-10 06:21:19 -04:00
}
#endif /* LOGGER_H */