3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
28 #include "irrlichttypes.h"
33 LL_NONE, // Special level that is always printed
36 LL_ACTION, // In-game actions
42 typedef u8 LogLevelMask;
43 #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
47 void addOutput(ILogOutput *out);
48 void addOutput(ILogOutput *out, LogLevel lev);
49 void addOutputMasked(ILogOutput *out, LogLevelMask mask);
50 void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
51 LogLevelMask removeOutput(ILogOutput *out);
52 void setLevelSilenced(LogLevel lev, bool silenced);
54 void registerThread(const std::string &name);
55 void deregisterThread();
57 void log(LogLevel lev, const std::string &text);
58 // Logs without a prefix
59 void logRaw(LogLevel lev, const std::string &text);
61 void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
62 bool getTraceEnabled() { return m_trace_enabled; }
64 static LogLevel stringToLevel(const std::string &name);
65 static const std::string getLevelLabel(LogLevel lev);
68 void logToOutputsRaw(LogLevel, const std::string &line);
69 void logToOutputs(LogLevel, const std::string &combined,
70 const std::string &time, const std::string &thread_name,
71 const std::string &payload_text);
73 const std::string getThreadName();
75 std::vector<ILogOutput *> m_outputs[LL_MAX];
77 // Should implement atomic loads and stores (even though it's only
78 // written to when one thread has access currently).
79 // Works on all known architectures (x86, ARM, MIPS).
80 volatile bool m_silenced_levels[LL_MAX];
81 std::map<threadid_t, std::string> m_thread_names;
82 mutable Mutex m_mutex;
88 virtual void logRaw(LogLevel, const std::string &line) = 0;
89 virtual void log(LogLevel, const std::string &combined,
90 const std::string &time, const std::string &thread_name,
91 const std::string &payload_text) = 0;
94 class ICombinedLogOutput : public ILogOutput {
96 void log(LogLevel lev, const std::string &combined,
97 const std::string &time, const std::string &thread_name,
98 const std::string &payload_text)
100 logRaw(lev, combined);
104 class StreamLogOutput : public ICombinedLogOutput {
106 StreamLogOutput(std::ostream &stream) :
111 void logRaw(LogLevel lev, const std::string &line)
113 m_stream << line << std::endl;
117 std::ostream &m_stream;
120 class FileLogOutput : public ICombinedLogOutput {
122 void open(const std::string &filename);
124 void logRaw(LogLevel lev, const std::string &line)
126 m_stream << line << std::endl;
130 std::ofstream m_stream;
133 class LogOutputBuffer : public ICombinedLogOutput {
135 LogOutputBuffer(Logger &logger, LogLevel lev) :
138 m_logger.addOutput(this, lev);
143 m_logger.removeOutput(this);
146 void logRaw(LogLevel lev, const std::string &line)
153 return m_buffer.empty();
160 std::string s = m_buffer.front();
166 std::queue<std::string> m_buffer;
171 extern StreamLogOutput stdout_output;
172 extern StreamLogOutput stderr_output;
173 extern std::ostream null_stream;
175 extern std::ostream *dout_con_ptr;
176 extern std::ostream *derr_con_ptr;
177 extern std::ostream *dout_server_ptr;
178 extern std::ostream *derr_server_ptr;
181 extern std::ostream *dout_client_ptr;
182 extern std::ostream *derr_client_ptr;
185 extern Logger g_logger;
187 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
188 extern std::ostream rawstream;
190 extern std::ostream errorstream;
191 extern std::ostream warningstream;
192 extern std::ostream actionstream;
193 extern std::ostream infostream;
194 extern std::ostream verbosestream;
195 extern std::ostream dstream;
197 #define TRACEDO(x) do { \
198 if (g_logger.getTraceEnabled()) { \
203 #define TRACESTREAM(x) TRACEDO(verbosestream x)
205 #define dout_con (*dout_con_ptr)
206 #define derr_con (*derr_con_ptr)
207 #define dout_server (*dout_server_ptr)
208 #define derr_server (*derr_server_ptr)
211 #define dout_client (*dout_client_ptr)
212 #define derr_client (*derr_client_ptr)