]> granicus.if.org Git - icinga2/blob - lib/base/logger.hpp
Remove unused includes
[icinga2] / lib / base / logger.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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-ti.hpp"
25 #include <set>
26 #include <iosfwd>
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 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() 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() = 0;
81
82         static std::set<Logger::Ptr> GetLoggers();
83
84         static void DisableConsoleLog();
85         static void EnableConsoleLog();
86         static bool IsConsoleLogEnabled();
87         static void DisableTimestamp(bool);
88         static bool IsTimestampEnabled();
89
90         static void SetConsoleLogSeverity(LogSeverity logSeverity);
91         static LogSeverity GetConsoleLogSeverity();
92
93         void ValidateSeverity(const Lazy<String>& lvalue, const ValidationUtils& utils) final;
94
95 protected:
96         void Start(bool runtimeCreated) override;
97         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 class Log
108 {
109 public:
110         Log() = delete;
111         Log(const Log& other) = delete;
112         Log& operator=(const Log& rhs) = delete;
113
114         Log(LogSeverity severity, String facility, const String& message);
115         Log(LogSeverity severity, String facility);
116
117         ~Log();
118
119         template<typename T>
120         Log& operator<<(const T& val)
121         {
122                 m_Buffer << val;
123                 return *this;
124         }
125
126         Log& operator<<(const char *val);
127
128 private:
129         LogSeverity m_Severity;
130         String m_Facility;
131         std::ostringstream m_Buffer;
132 };
133
134 extern template Log& Log::operator<<(const Value&);
135 extern template Log& Log::operator<<(const String&);
136 extern template Log& Log::operator<<(const std::string&);
137 extern template Log& Log::operator<<(const bool&);
138 extern template Log& Log::operator<<(const unsigned int&);
139 extern template Log& Log::operator<<(const int&);
140 extern template Log& Log::operator<<(const unsigned long&);
141 extern template Log& Log::operator<<(const long&);
142 extern template Log& Log::operator<<(const double&);
143
144 }
145
146 #endif /* LOGGER_H */