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.
29 #include "irrlichttypes.h"
34 LL_NONE, // Special level that is always printed
37 LL_ACTION, // In-game actions
43 typedef u8 LogLevelMask;
44 #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
48 void addOutput(ILogOutput *out);
49 void addOutput(ILogOutput *out, LogLevel lev);
50 void addOutputMasked(ILogOutput *out, LogLevelMask mask);
51 void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
52 LogLevelMask removeOutput(ILogOutput *out);
53 void setLevelSilenced(LogLevel lev, bool silenced);
55 void registerThread(const std::string &name);
56 void deregisterThread();
58 void log(LogLevel lev, const std::string &text);
59 // Logs without a prefix
60 void logRaw(LogLevel lev, const std::string &text);
62 void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
63 bool getTraceEnabled() { return m_trace_enabled; }
65 static LogLevel stringToLevel(const std::string &name);
66 static const std::string getLevelLabel(LogLevel lev);
69 void logToOutputsRaw(LogLevel, const std::string &line);
70 void logToOutputs(LogLevel, const std::string &combined,
71 const std::string &time, const std::string &thread_name,
72 const std::string &payload_text);
74 const std::string getThreadName();
76 std::vector<ILogOutput *> m_outputs[LL_MAX];
78 // Should implement atomic loads and stores (even though it's only
79 // written to when one thread has access currently).
80 // Works on all known architectures (x86, ARM, MIPS).
81 volatile bool m_silenced_levels[LL_MAX];
82 std::map<std::thread::id, std::string> m_thread_names;
83 mutable std::mutex m_mutex;
89 virtual void logRaw(LogLevel, const std::string &line) = 0;
90 virtual void log(LogLevel, const std::string &combined,
91 const std::string &time, const std::string &thread_name,
92 const std::string &payload_text) = 0;
95 class ICombinedLogOutput : public ILogOutput {
97 void log(LogLevel lev, const std::string &combined,
98 const std::string &time, const std::string &thread_name,
99 const std::string &payload_text)
101 logRaw(lev, combined);
105 class StreamLogOutput : public ICombinedLogOutput {
107 StreamLogOutput(std::ostream &stream) :
112 void logRaw(LogLevel lev, const std::string &line)
114 m_stream << line << std::endl;
118 std::ostream &m_stream;
121 class FileLogOutput : public ICombinedLogOutput {
123 void open(const std::string &filename);
125 void logRaw(LogLevel lev, const std::string &line)
127 m_stream << line << std::endl;
131 std::ofstream m_stream;
134 class LogOutputBuffer : public ICombinedLogOutput {
136 LogOutputBuffer(Logger &logger, LogLevel lev) :
139 m_logger.addOutput(this, lev);
144 m_logger.removeOutput(this);
147 void logRaw(LogLevel lev, const std::string &line)
154 return m_buffer.empty();
161 std::string s = m_buffer.front();
167 std::queue<std::string> m_buffer;
172 extern StreamLogOutput stdout_output;
173 extern StreamLogOutput stderr_output;
174 extern std::ostream null_stream;
176 extern std::ostream *dout_con_ptr;
177 extern std::ostream *derr_con_ptr;
178 extern std::ostream *dout_server_ptr;
179 extern std::ostream *derr_server_ptr;
182 extern std::ostream *dout_client_ptr;
183 extern std::ostream *derr_client_ptr;
186 extern Logger g_logger;
188 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
189 extern std::ostream rawstream;
191 extern std::ostream errorstream;
192 extern std::ostream warningstream;
193 extern std::ostream actionstream;
194 extern std::ostream infostream;
195 extern std::ostream verbosestream;
196 extern std::ostream dstream;
198 #define TRACEDO(x) do { \
199 if (g_logger.getTraceEnabled()) { \
204 #define TRACESTREAM(x) TRACEDO(verbosestream x)
206 #define dout_con (*dout_con_ptr)
207 #define derr_con (*derr_con_ptr)
208 #define dout_server (*dout_server_ptr)
209 #define derr_server (*derr_server_ptr)
212 #define dout_client (*dout_client_ptr)
213 #define derr_client (*derr_client_ptr)