]> granicus.if.org Git - icinga2/blob - lib/base/logger.hpp
Update copyright headers for 2016
[icinga2] / lib / base / logger.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef LOGGER_H
21 #define LOGGER_H
22
23 #include "base/i2-base.hpp"
24 #include "base/logger.thpp"
25 #include <set>
26 #include <sstream>
27
28 namespace icinga
29 {
30
31 /**
32  * Log severity.
33  *
34  * @ingroup base
35  */
36 enum LogSeverity
37 {
38         LogDebug,
39         LogNotice,
40         LogInformation,
41         LogWarning,
42         LogCritical
43 };
44
45 /**
46  * A log entry.
47  *
48  * @ingroup base
49  */
50 struct LogEntry {
51         double Timestamp; /**< The timestamp when this log entry was created. */
52         LogSeverity Severity; /**< The severity of this log entry. */
53         String Facility; /**< The facility this log entry belongs to. */
54         String Message; /**< The log entry's message. */
55 };
56
57 /**
58  * A log provider.
59  *
60  * @ingroup base
61  */
62 class I2_BASE_API Logger : public ObjectImpl<Logger>
63 {
64 public:
65         DECLARE_OBJECT(Logger);
66
67         static String SeverityToString(LogSeverity severity);
68         static LogSeverity StringToSeverity(const String& severity);
69
70         LogSeverity GetMinSeverity(void) const;
71
72         /**
73          * Processes the log entry and writes it to the log that is
74          * represented by this ILogger object.
75          *
76          * @param entry The log entry that is to be processed.
77          */
78         virtual void ProcessLogEntry(const LogEntry& entry) = 0;
79
80         virtual void Flush(void) = 0;
81
82         static std::set<Logger::Ptr> GetLoggers(void);
83
84         static void DisableConsoleLog(void);
85         static void EnableConsoleLog(void);
86         static bool IsConsoleLogEnabled(void);
87         static void DisableTimestamp(bool);
88         static bool IsTimestampEnabled(void);
89
90         static void SetConsoleLogSeverity(LogSeverity logSeverity);
91         static LogSeverity GetConsoleLogSeverity(void);
92
93         static void StaticInitialize(void);
94
95 protected:
96         virtual void Start(bool runtimeCreated) override;
97         virtual void Stop(bool runtimeRemoved) override;
98
99 private:
100         static boost::mutex m_Mutex;
101         static std::set<Logger::Ptr> m_Loggers;
102         static bool m_ConsoleLogEnabled;
103         static bool m_TimestampEnabled;
104         static LogSeverity m_ConsoleLogSeverity;
105 };
106
107 I2_BASE_API void IcingaLog(LogSeverity severity, const String& facility, const String& message);
108
109 class Log
110 {
111 public:
112         inline Log(LogSeverity severity, const String& facility, const String& message)
113                 : m_Severity(severity), m_Facility(facility)
114         {
115                 m_Buffer << message;
116         }
117
118         inline Log(LogSeverity severity, const String& facility)
119                 : m_Severity(severity), m_Facility(facility)
120         { }
121
122         inline ~Log(void)
123         {
124                 IcingaLog(m_Severity, m_Facility, m_Buffer.str());
125         }
126
127         template<typename T>
128         Log& operator<<(const T& val)
129         {
130                 m_Buffer << val;
131                 return *this;
132         }
133
134 private:
135         LogSeverity m_Severity;
136         String m_Facility;
137         std::ostringstream m_Buffer;
138
139         Log(void);
140         Log(const Log& other);
141         Log& operator=(const Log& rhs);
142 };
143
144 }
145
146 #endif /* LOGGER_H */