wasplib: add inv management helpers
[waspsaliva.git] / src / log.h
blob6ed6b1fb710b272ae66a8e81614850cdad14ba63
1 /*
2 Minetest
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.
20 #pragma once
22 #include <map>
23 #include <queue>
24 #include <string>
25 #include <fstream>
26 #include <thread>
27 #include <mutex>
28 #if !defined(_WIN32) // POSIX
29 #include <unistd.h>
30 #endif
31 #include "irrlichttypes.h"
33 class ILogOutput;
35 enum LogLevel {
36 LL_NONE, // Special level that is always printed
37 LL_ERROR,
38 LL_WARNING,
39 LL_ACTION, // In-game actions
40 LL_INFO,
41 LL_VERBOSE,
42 LL_MAX,
45 enum LogColor {
46 LOG_COLOR_NEVER,
47 LOG_COLOR_ALWAYS,
48 LOG_COLOR_AUTO,
51 typedef u8 LogLevelMask;
52 #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
54 class Logger {
55 public:
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;
78 private:
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;
94 bool m_trace_enabled;
97 class ILogOutput {
98 public:
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 {
106 public:
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 {
116 public:
117 StreamLogOutput(std::ostream &stream) :
118 m_stream(stream)
120 #if !defined(_WIN32)
121 is_tty = isatty(fileno(stdout));
122 #else
123 is_tty = false;
124 #endif
127 void logRaw(LogLevel lev, const std::string &line);
129 private:
130 std::ostream &m_stream;
131 bool is_tty;
134 class FileLogOutput : public ICombinedLogOutput {
135 public:
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;
143 private:
144 std::ofstream m_stream;
147 class LogOutputBuffer : public ICombinedLogOutput {
148 public:
149 LogOutputBuffer(Logger &logger) :
150 m_logger(logger)
152 updateLogLevel();
155 virtual ~LogOutputBuffer()
157 m_logger.removeOutput(this);
160 void updateLogLevel();
162 void logRaw(LogLevel lev, const std::string &line);
164 void clear()
166 m_buffer = std::queue<std::string>();
169 bool empty() const
171 return m_buffer.empty();
174 std::string get()
176 if (empty())
177 return "";
178 std::string s = m_buffer.front();
179 m_buffer.pop();
180 return s;
183 private:
184 std::queue<std::string> m_buffer;
185 Logger &m_logger;
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 *derr_server_ptr;
197 extern Logger g_logger;
199 // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
200 extern std::ostream rawstream;
202 extern std::ostream errorstream;
203 extern std::ostream warningstream;
204 extern std::ostream actionstream;
205 extern std::ostream infostream;
206 extern std::ostream verbosestream;
207 extern std::ostream dstream;
209 #define TRACEDO(x) do { \
210 if (g_logger.getTraceEnabled()) { \
211 x; \
213 } while (0)
215 #define TRACESTREAM(x) TRACEDO(verbosestream x)
217 #define dout_con (*dout_con_ptr)
218 #define derr_con (*derr_con_ptr)