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);
130 std::ostream &m_stream;
134 class FileLogOutput : public ICombinedLogOutput {
136 void setFile(const std::string &filename, s64 file_size_max);
138 void logRaw(LogLevel lev, const std::string &line)
140 m_stream << line << std::endl;
144 std::ofstream m_stream;
147 class LogOutputBuffer : public ICombinedLogOutput {
149 LogOutputBuffer(Logger &logger) :
155 virtual ~LogOutputBuffer()
157 m_logger.removeOutput(this);
160 void updateLogLevel();
162 void logRaw(LogLevel lev, const std::string &line);
166 m_buffer = std::queue<std::string>();
171 return m_buffer.empty();
178 std::string s = m_buffer.front();
184 std::queue<std::string> m_buffer;
189 extern StreamLogOutput stdout_output;
190 extern StreamLogOutput stderr_output;
191 extern std::ostream null_stream;
193 extern std::ostream *dout_con_ptr;
194 extern std::ostream *derr_con_ptr;
195 extern std::ostream *dout_server_ptr;
196 extern std::ostream *derr_server_ptr;
199 extern std::ostream *dout_client_ptr;
200 extern std::ostream *derr_client_ptr;
203 extern Logger g_logger;
205 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
206 extern std::ostream rawstream;
208 extern std::ostream errorstream;
209 extern std::ostream warningstream;
210 extern std::ostream actionstream;
211 extern std::ostream infostream;
212 extern std::ostream verbosestream;
213 extern std::ostream dstream;
215 #define TRACEDO(x) do { \
216 if (g_logger.getTraceEnabled()) { \
221 #define TRACESTREAM(x) TRACEDO(verbosestream x)
223 #define dout_con (*dout_con_ptr)
224 #define derr_con (*derr_con_ptr)
225 #define dout_server (*dout_server_ptr)
228 #define dout_client (*dout_client_ptr)