0e63041ccdbd215d82ae6cade0b1e0e5bb04989c
[oweals/minetest.git] / log.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef LOG_HEADER
21 #define LOG_HEADER
22
23 #include <map>
24 #include <queue>
25 #include <string>
26 #include <fstream>
27 #include "threads.h"
28
29 class ILogOutput;
30
31 enum LogLevel {
32         LL_NONE, // Special level that is always printed
33         LL_ERROR,
34         LL_WARNING,
35         LL_ACTION,  // In-game actions
36         LL_INFO,
37         LL_VERBOSE,
38         LL_MAX,
39 };
40
41 class Logger {
42 public:
43         void addOutput(ILogOutput *out);
44         void addOutput(ILogOutput *out, LogLevel lev);
45         void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
46         void removeOutput(ILogOutput *out);
47         void setLevelSilenced(LogLevel lev, bool silenced);
48
49         void registerThread(const std::string &name);
50         void deregisterThread();
51
52         void log(LogLevel lev, const std::string &text);
53         // Logs without a prefix
54         void logRaw(LogLevel lev, const std::string &text);
55
56         void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
57         bool getTraceEnabled() { return m_trace_enabled; }
58
59         static LogLevel stringToLevel(const std::string &name);
60
61 private:
62         void logToSystem(LogLevel, const std::string &text);
63         void logToOutputs(LogLevel, const std::string &text);
64
65         const std::string getLevelLabel(LogLevel lev);
66         const std::string getThreadName();
67
68         std::vector<ILogOutput *> m_outputs[LL_MAX];
69
70         // Should implement atomic loads and stores (even though it's only
71         // written to when one thread has access currently).
72         // Works on all known architectures (x86, ARM, MIPS).
73         volatile bool m_silenced_levels[LL_MAX];
74         std::map<threadid_t, std::string> m_thread_names;
75         mutable Mutex m_mutex;
76         bool m_trace_enabled;
77 };
78
79 class ILogOutput {
80 public:
81         virtual void log(const std::string &line) = 0;
82 };
83
84 class StreamLogOutput : public ILogOutput {
85 public:
86         StreamLogOutput(std::ostream &stream) :
87                 stream(stream)
88         {
89         }
90
91         void log(const std::string &line)
92         {
93                 stream << line << std::endl;
94         }
95
96 private:
97         std::ostream &stream;
98 };
99
100 class FileLogOutput : public ILogOutput {
101 public:
102         void open(const std::string &filename);
103
104         void log(const std::string &line)
105         {
106                 stream << line << std::endl;
107         }
108
109 private:
110         std::ofstream stream;
111 };
112
113 class LogOutputBuffer : public ILogOutput {
114 public:
115         LogOutputBuffer(Logger &logger, LogLevel lev) :
116                 logger(logger)
117         {
118                 logger.addOutput(this, lev);
119         }
120
121         ~LogOutputBuffer()
122         {
123                 logger.removeOutput(this);
124         }
125
126         virtual void log(const std::string &line)
127         {
128                 buffer.push(line);
129         }
130
131         bool empty()
132         {
133                 return buffer.empty();
134         }
135
136         std::string get()
137         {
138                 if (empty())
139                         return "";
140                 std::string s = buffer.front();
141                 buffer.pop();
142                 return s;
143         }
144
145 private:
146         std::queue<std::string> buffer;
147         Logger &logger;
148 };
149
150
151 extern StreamLogOutput stdout_output;
152 extern StreamLogOutput stderr_output;
153 extern std::ostream null_stream;
154
155 extern std::ostream *dout_con_ptr;
156 extern std::ostream *derr_con_ptr;
157 extern std::ostream *dout_server_ptr;
158 extern std::ostream *derr_server_ptr;
159
160 #ifndef SERVER
161 extern std::ostream *dout_client_ptr;
162 extern std::ostream *derr_client_ptr;
163 #endif
164
165 extern Logger g_logger;
166
167 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
168 extern std::ostream rawstream;
169
170 extern std::ostream errorstream;
171 extern std::ostream warningstream;
172 extern std::ostream actionstream;
173 extern std::ostream infostream;
174 extern std::ostream verbosestream;
175 extern std::ostream dstream;
176
177 #define TRACEDO(x) do {               \
178         if (g_logger.getTraceEnabled()) { \
179                 x;                            \
180         }                                 \
181 } while (0)
182
183 #define TRACESTREAM(x) TRACEDO(verbosestream x)
184
185 #define dout_con (*dout_con_ptr)
186 #define derr_con (*derr_con_ptr)
187 #define dout_server (*dout_server_ptr)
188 #define derr_server (*derr_server_ptr)
189
190 #ifndef SERVER
191         #define dout_client (*dout_client_ptr)
192         #define derr_client (*derr_client_ptr)
193 #endif
194
195
196 #endif