]> granicus.if.org Git - icinga2/blob - lib/base/logger.hpp
Merge pull request #7002 from Icinga/bugfix/check_network-percent-6155
[icinga2] / lib / base / logger.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef LOGGER_H
4 #define LOGGER_H
5
6 #include "base/i2-base.hpp"
7 #include "base/logger-ti.hpp"
8 #include <set>
9 #include <iosfwd>
10
11 namespace icinga
12 {
13
14 /**
15  * Log severity.
16  *
17  * @ingroup base
18  */
19 enum LogSeverity
20 {
21         LogDebug,
22         LogNotice,
23         LogInformation,
24         LogWarning,
25         LogCritical
26 };
27
28 /**
29  * A log entry.
30  *
31  * @ingroup base
32  */
33 struct LogEntry {
34         double Timestamp; /**< The timestamp when this log entry was created. */
35         LogSeverity Severity; /**< The severity of this log entry. */
36         String Facility; /**< The facility this log entry belongs to. */
37         String Message; /**< The log entry's message. */
38 };
39
40 /**
41  * A log provider.
42  *
43  * @ingroup base
44  */
45 class Logger : public ObjectImpl<Logger>
46 {
47 public:
48         DECLARE_OBJECT(Logger);
49
50         static String SeverityToString(LogSeverity severity);
51         static LogSeverity StringToSeverity(const String& severity);
52
53         LogSeverity GetMinSeverity() const;
54
55         /**
56          * Processes the log entry and writes it to the log that is
57          * represented by this ILogger object.
58          *
59          * @param entry The log entry that is to be processed.
60          */
61         virtual void ProcessLogEntry(const LogEntry& entry) = 0;
62
63         virtual void Flush() = 0;
64
65         static std::set<Logger::Ptr> GetLoggers();
66
67         static void DisableConsoleLog();
68         static void EnableConsoleLog();
69         static bool IsConsoleLogEnabled();
70         static void DisableTimestamp();
71         static void EnableTimestamp();
72         static bool IsTimestampEnabled();
73
74         static void SetConsoleLogSeverity(LogSeverity logSeverity);
75         static LogSeverity GetConsoleLogSeverity();
76
77         void ValidateSeverity(const Lazy<String>& lvalue, const ValidationUtils& utils) final;
78
79 protected:
80         void Start(bool runtimeCreated) override;
81         void Stop(bool runtimeRemoved) override;
82
83 private:
84         static boost::mutex m_Mutex;
85         static std::set<Logger::Ptr> m_Loggers;
86         static bool m_ConsoleLogEnabled;
87         static bool m_TimestampEnabled;
88         static LogSeverity m_ConsoleLogSeverity;
89 };
90
91 class Log
92 {
93 public:
94         Log() = delete;
95         Log(const Log& other) = delete;
96         Log& operator=(const Log& rhs) = delete;
97
98         Log(LogSeverity severity, String facility, const String& message);
99         Log(LogSeverity severity, String facility);
100
101         ~Log();
102
103         template<typename T>
104         Log& operator<<(const T& val)
105         {
106                 m_Buffer << val;
107                 return *this;
108         }
109
110         Log& operator<<(const char *val);
111
112 private:
113         LogSeverity m_Severity;
114         String m_Facility;
115         std::ostringstream m_Buffer;
116 };
117
118 extern template Log& Log::operator<<(const Value&);
119 extern template Log& Log::operator<<(const String&);
120 extern template Log& Log::operator<<(const std::string&);
121 extern template Log& Log::operator<<(const bool&);
122 extern template Log& Log::operator<<(const unsigned int&);
123 extern template Log& Log::operator<<(const int&);
124 extern template Log& Log::operator<<(const unsigned long&);
125 extern template Log& Log::operator<<(const long&);
126 extern template Log& Log::operator<<(const double&);
127
128 }
129
130 #endif /* LOGGER_H */