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 #if !defined(_WIN32) // POSIX
32 #include "irrlichttypes.h"
37 LL_NONE, // Special level that is always printed
40 LL_ACTION, // In-game actions
46 typedef u8 LogLevelMask;
47 #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
51 void addOutput(ILogOutput *out);
52 void addOutput(ILogOutput *out, LogLevel lev);
53 void addOutputMasked(ILogOutput *out, LogLevelMask mask);
54 void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
55 LogLevelMask removeOutput(ILogOutput *out);
56 void setLevelSilenced(LogLevel lev, bool silenced);
58 void registerThread(const std::string &name);
59 void deregisterThread();
61 void log(LogLevel lev, const std::string &text);
62 // Logs without a prefix
63 void logRaw(LogLevel lev, const std::string &text);
65 void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
66 bool getTraceEnabled() { return m_trace_enabled; }
68 static LogLevel stringToLevel(const std::string &name);
69 static const std::string getLevelLabel(LogLevel lev);
72 void logToOutputsRaw(LogLevel, const std::string &line);
73 void logToOutputs(LogLevel, const std::string &combined,
74 const std::string &time, const std::string &thread_name,
75 const std::string &payload_text);
77 const std::string getThreadName();
79 std::vector<ILogOutput *> m_outputs[LL_MAX];
81 // Should implement atomic loads and stores (even though it's only
82 // written to when one thread has access currently).
83 // Works on all known architectures (x86, ARM, MIPS).
84 volatile bool m_silenced_levels[LL_MAX];
85 std::map<std::thread::id, std::string> m_thread_names;
86 mutable std::mutex m_mutex;
92 virtual void logRaw(LogLevel, const std::string &line) = 0;
93 virtual void log(LogLevel, const std::string &combined,
94 const std::string &time, const std::string &thread_name,
95 const std::string &payload_text) = 0;
98 class ICombinedLogOutput : public ILogOutput {
100 void log(LogLevel lev, const std::string &combined,
101 const std::string &time, const std::string &thread_name,
102 const std::string &payload_text)
104 logRaw(lev, combined);
108 class StreamLogOutput : public ICombinedLogOutput {
110 StreamLogOutput(std::ostream &stream) :
114 is_tty = isatty(fileno(stdout));
120 void logRaw(LogLevel lev, const std::string &line)
122 static const std::string use_logcolor = g_settings->get("log_color");
124 bool colored = use_logcolor == "detect" ? is_tty : use_logcolor == "yes";
129 m_stream << "\033[91m";
133 m_stream << "\033[93m";
136 // info is a bit dark
137 m_stream << "\033[37m";
140 // verbose is darker than info
141 m_stream << "\033[2m";
148 m_stream << line << std::endl;
151 // reset to white color
152 m_stream << "\033[0m";
156 std::ostream &m_stream;
160 class FileLogOutput : public ICombinedLogOutput {
162 void open(const std::string &filename);
164 void logRaw(LogLevel lev, const std::string &line)
166 m_stream << line << std::endl;
170 std::ofstream m_stream;
173 class LogOutputBuffer : public ICombinedLogOutput {
175 LogOutputBuffer(Logger &logger, LogLevel lev) :
178 m_logger.addOutput(this, lev);
183 m_logger.removeOutput(this);
186 void logRaw(LogLevel lev, const std::string &line)
193 return m_buffer.empty();
200 std::string s = m_buffer.front();
206 std::queue<std::string> m_buffer;
211 extern StreamLogOutput stdout_output;
212 extern StreamLogOutput stderr_output;
213 extern std::ostream null_stream;
215 extern std::ostream *dout_con_ptr;
216 extern std::ostream *derr_con_ptr;
217 extern std::ostream *dout_server_ptr;
218 extern std::ostream *derr_server_ptr;
221 extern std::ostream *dout_client_ptr;
222 extern std::ostream *derr_client_ptr;
225 extern Logger g_logger;
227 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
228 extern std::ostream rawstream;
230 extern std::ostream errorstream;
231 extern std::ostream warningstream;
232 extern std::ostream actionstream;
233 extern std::ostream infostream;
234 extern std::ostream verbosestream;
235 extern std::ostream dstream;
237 #define TRACEDO(x) do { \
238 if (g_logger.getTraceEnabled()) { \
243 #define TRACESTREAM(x) TRACEDO(verbosestream x)
245 #define dout_con (*dout_con_ptr)
246 #define derr_con (*derr_con_ptr)
247 #define dout_server (*dout_server_ptr)
250 #define dout_client (*dout_client_ptr)