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
31 #include "irrlichttypes.h"
36 LL_NONE, // Special level that is always printed
39 LL_ACTION, // In-game actions
51 typedef u8 LogLevelMask;
52 #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
56 void addOutput(ILogOutput *out);
57 void addOutput(ILogOutput *out, LogLevel lev);
58 void addOutputMasked(ILogOutput *out, LogLevelMask mask);
59 void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
60 LogLevelMask removeOutput(ILogOutput *out);
61 void setLevelSilenced(LogLevel lev, bool silenced);
63 void registerThread(const std::string &name);
64 void deregisterThread();
66 void log(LogLevel lev, const std::string &text);
67 // Logs without a prefix
68 void logRaw(LogLevel lev, const std::string &text);
70 void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
71 bool getTraceEnabled() { return m_trace_enabled; }
73 static LogLevel stringToLevel(const std::string &name);
74 static const std::string getLevelLabel(LogLevel lev);
76 static LogColor color_mode;
79 void logToOutputsRaw(LogLevel, const std::string &line);
80 void logToOutputs(LogLevel, const std::string &combined,
81 const std::string &time, const std::string &thread_name,
82 const std::string &payload_text);
84 const std::string getThreadName();
86 std::vector<ILogOutput *> m_outputs[LL_MAX];
88 // Should implement atomic loads and stores (even though it's only
89 // written to when one thread has access currently).
90 // Works on all known architectures (x86, ARM, MIPS).
91 volatile bool m_silenced_levels[LL_MAX];
92 std::map<std::thread::id, std::string> m_thread_names;
93 mutable std::mutex m_mutex;
99 virtual void logRaw(LogLevel, const std::string &line) = 0;
100 virtual void log(LogLevel, const std::string &combined,
101 const std::string &time, const std::string &thread_name,
102 const std::string &payload_text) = 0;
105 class ICombinedLogOutput : public ILogOutput {
107 void log(LogLevel lev, const std::string &combined,
108 const std::string &time, const std::string &thread_name,
109 const std::string &payload_text)
111 logRaw(lev, combined);
115 class StreamLogOutput : public ICombinedLogOutput {
117 StreamLogOutput(std::ostream &stream) :
121 is_tty = isatty(fileno(stdout));
127 void logRaw(LogLevel lev, const std::string &line)
129 bool colored_message = (Logger::color_mode == LOG_COLOR_ALWAYS) ||
130 (Logger::color_mode == LOG_COLOR_AUTO && is_tty);
135 m_stream << "\033[91m";
139 m_stream << "\033[93m";
142 // info is a bit dark
143 m_stream << "\033[37m";
146 // verbose is darker than info
147 m_stream << "\033[2m";
151 colored_message = false;
154 m_stream << line << std::endl;
157 // reset to white color
158 m_stream << "\033[0m";
162 std::ostream &m_stream;
166 class FileLogOutput : public ICombinedLogOutput {
168 void setFile(const std::string &filename, s64 file_size_max);
170 void logRaw(LogLevel lev, const std::string &line)
172 m_stream << line << std::endl;
176 std::ofstream m_stream;
179 class LogOutputBuffer : public ICombinedLogOutput {
181 LogOutputBuffer(Logger &logger, LogLevel lev) :
184 m_logger.addOutput(this, lev);
189 m_logger.removeOutput(this);
192 void logRaw(LogLevel lev, const std::string &line)
199 return m_buffer.empty();
206 std::string s = m_buffer.front();
212 std::queue<std::string> m_buffer;
217 extern StreamLogOutput stdout_output;
218 extern StreamLogOutput stderr_output;
219 extern std::ostream null_stream;
221 extern std::ostream *dout_con_ptr;
222 extern std::ostream *derr_con_ptr;
223 extern std::ostream *dout_server_ptr;
224 extern std::ostream *derr_server_ptr;
227 extern std::ostream *dout_client_ptr;
228 extern std::ostream *derr_client_ptr;
231 extern Logger g_logger;
233 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
234 extern std::ostream rawstream;
236 extern std::ostream errorstream;
237 extern std::ostream warningstream;
238 extern std::ostream actionstream;
239 extern std::ostream infostream;
240 extern std::ostream verbosestream;
241 extern std::ostream dstream;
243 #define TRACEDO(x) do { \
244 if (g_logger.getTraceEnabled()) { \
249 #define TRACESTREAM(x) TRACEDO(verbosestream x)
251 #define dout_con (*dout_con_ptr)
252 #define derr_con (*derr_con_ptr)
253 #define dout_server (*dout_server_ptr)
256 #define dout_client (*dout_client_ptr)