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.
32 LL_NONE, // Special level that is always printed
35 LL_ACTION, // In-game actions
43 void addOutput(ILogOutput *out);
44 void addOutput(ILogOutput *out, LogLevel lev);
45 void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
46 void removeOutput(ILogOutput *out);
47 void setLevelSilenced(LogLevel lev, bool silenced);
49 void registerThread(const std::string &name);
50 void deregisterThread();
52 void log(LogLevel lev, const std::string &text);
53 // Logs without a prefix
54 void logRaw(LogLevel lev, const std::string &text);
56 void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
57 bool getTraceEnabled() { return m_trace_enabled; }
59 static LogLevel stringToLevel(const std::string &name);
62 void logToSystem(LogLevel, const std::string &text);
63 void logToOutputs(LogLevel, const std::string &text);
65 const std::string getLevelLabel(LogLevel lev);
66 const std::string getThreadName();
68 std::vector<ILogOutput *> m_outputs[LL_MAX];
70 // Should implement atomic loads and stores (even though it's only
71 // written to when one thread has access currently).
72 // Works on all known architectures (x86, ARM, MIPS).
73 volatile bool m_silenced_levels[LL_MAX];
74 std::map<threadid_t, std::string> m_thread_names;
75 mutable Mutex m_mutex;
81 virtual void log(const std::string &line) = 0;
84 class StreamLogOutput : public ILogOutput {
86 StreamLogOutput(std::ostream &stream) :
91 void log(const std::string &line)
93 stream << line << std::endl;
100 class FileLogOutput : public ILogOutput {
102 void open(const std::string &filename);
104 void log(const std::string &line)
106 stream << line << std::endl;
110 std::ofstream stream;
113 class LogOutputBuffer : public ILogOutput {
115 LogOutputBuffer(Logger &logger, LogLevel lev) :
118 logger.addOutput(this, lev);
123 logger.removeOutput(this);
126 virtual void log(const std::string &line)
133 return buffer.empty();
140 std::string s = buffer.front();
146 std::queue<std::string> buffer;
151 extern StreamLogOutput stdout_output;
152 extern StreamLogOutput stderr_output;
153 extern std::ostream null_stream;
155 extern std::ostream *dout_con_ptr;
156 extern std::ostream *derr_con_ptr;
157 extern std::ostream *dout_server_ptr;
158 extern std::ostream *derr_server_ptr;
161 extern std::ostream *dout_client_ptr;
162 extern std::ostream *derr_client_ptr;
165 extern Logger g_logger;
167 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
168 extern std::ostream rawstream;
170 extern std::ostream errorstream;
171 extern std::ostream warningstream;
172 extern std::ostream actionstream;
173 extern std::ostream infostream;
174 extern std::ostream verbosestream;
175 extern std::ostream dstream;
177 #define TRACEDO(x) do { \
178 if (g_logger.getTraceEnabled()) { \
183 #define TRACESTREAM(x) TRACEDO(verbosestream x)
185 #define dout_con (*dout_con_ptr)
186 #define derr_con (*derr_con_ptr)
187 #define dout_server (*dout_server_ptr)
188 #define derr_server (*derr_server_ptr)
191 #define dout_client (*dout_client_ptr)
192 #define derr_client (*derr_client_ptr)