Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
log_internal.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2
3#pragma once
4
5#include <atomic>
6#include <map>
7#include <vector>
8#include <string_view>
9#include <fstream>
10#include <thread>
11#include <mutex>
13#include "util/basic_macros.h"
14#include "irrlichttypes.h"
15
16class ILogOutput;
17
19 LL_NONE, // Special level that is always printed
22 LL_ACTION, // In-game actions
27};
28
34
40
41typedef u8 LogLevelMask;
42#define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
43
44class Logger {
45public:
46 void addOutput(ILogOutput *out);
47 void addOutput(ILogOutput *out, LogLevel lev);
51 void setLevelSilenced(LogLevel lev, bool silenced);
52
53 void registerThread(std::string_view name);
54 void deregisterThread();
55
56 void log(LogLevel lev, std::string_view text);
57 // Logs without a prefix
58 void logRaw(LogLevel lev, std::string_view text);
59
60 static LogLevel stringToLevel(std::string_view name);
61 static const char *getLevelLabel(LogLevel lev);
62
63 bool hasOutput(LogLevel level) {
64 return m_has_outputs[level].load(std::memory_order_relaxed);
65 }
66
68 return m_silenced_levels[level].load(std::memory_order_relaxed);
69 }
70
73
74private:
75 void logToOutputsRaw(LogLevel, std::string_view line);
76 void logToOutputs(LogLevel, const std::string &combined,
77 const std::string &time, const std::string &thread_name,
78 std::string_view payload_text);
79
80 const std::string &getThreadName();
81 static std::string getLogTimestamp();
82
83 mutable std::mutex m_mutex;
84 std::vector<ILogOutput *> m_outputs[LL_MAX];
85 std::atomic<bool> m_has_outputs[LL_MAX];
86 std::atomic<bool> m_silenced_levels[LL_MAX];
87 std::map<std::thread::id, std::string> m_thread_names;
88};
89
91public:
92 virtual void logRaw(LogLevel, std::string_view line) = 0;
93 virtual void log(LogLevel, const std::string &combined,
94 const std::string &time, const std::string &thread_name,
95 std::string_view payload_text) = 0;
96};
97
99public:
100 void log(LogLevel lev, const std::string &combined,
101 const std::string &time, const std::string &thread_name,
102 std::string_view payload_text)
103 {
104 logRaw(lev, combined);
105 }
106};
107
109public:
110 StreamLogOutput(std::ostream &stream);
111
112 void logRaw(LogLevel lev, std::string_view line);
113
114private:
115 std::ostream &m_stream;
116 bool is_tty = false;
117};
118
120public:
121 void setFile(const std::string &filename, s64 file_size_max);
122
123 void logRaw(LogLevel lev, std::string_view line)
124 {
125 m_stream << line << std::endl;
126 }
127
128private:
129 std::ofstream m_stream;
130};
131
132struct LogEntry {
134 std::string timestamp;
135 std::string thread_name;
136 std::string text;
137 // "timestamp: level[thread_name]: text"
138 std::string combined;
139};
140
142public:
145
147 {
148 m_logger.addOutput(this);
149 }
150
152 {
154 }
155
157 {
159 m_logger.addOutputMaxLevel(this, level);
160 }
161
162 void logRaw(LogLevel lev, std::string_view line) override
163 {
165 m_entries.emplace_back(LogEntry{lev, "", "", std::string(line), std::string(line)});
166 }
167
168 void log(LogLevel lev, const std::string &combined,
169 const std::string &time, const std::string &thread_name,
170 std::string_view payload_text) override
171 {
173 m_entries.emplace_back(LogEntry{lev, time, thread_name, std::string(payload_text), combined});
174 }
175
176 // Take the log entries currently stored, clearing the buffer.
177 std::vector<LogEntry> take()
178 {
179 std::vector<LogEntry> entries;
181 std::swap(m_entries, entries);
182 return entries;
183 }
184
185private:
187 // g_logger serializes calls to log/logRaw with a mutex, but that
188 // doesn't prevent take() from being called on top of it.
189 // This mutex prevents that.
190 std::mutex m_mutex;
191 std::vector<LogEntry> m_entries;
192};
193
194
195#ifdef __ANDROID__
196class AndroidLogOutput : public ICombinedLogOutput {
197public:
198 void logRaw(LogLevel lev, std::string_view line);
199};
200#endif
201
202#ifdef __ANDROID__
203extern AndroidLogOutput stdout_output;
204extern AndroidLogOutput stderr_output;
205#else
208#endif
209
210extern Logger g_logger;
Definition log_internal.h:141
void setLogLevel(LogLevel level)
Definition log_internal.h:156
std::vector< LogEntry > m_entries
Definition log_internal.h:191
std::vector< LogEntry > take()
Definition log_internal.h:177
DISABLE_CLASS_COPY(CaptureLogOutput)
void logRaw(LogLevel lev, std::string_view line) override
Definition log_internal.h:162
~CaptureLogOutput()
Definition log_internal.h:151
void log(LogLevel lev, const std::string &combined, const std::string &time, const std::string &thread_name, std::string_view payload_text) override
Definition log_internal.h:168
Logger & m_logger
Definition log_internal.h:186
CaptureLogOutput()=delete
CaptureLogOutput(Logger &logger)
Definition log_internal.h:146
std::mutex m_mutex
Definition log_internal.h:190
Definition log_internal.h:119
void setFile(const std::string &filename, s64 file_size_max)
Definition log.cpp:298
void logRaw(LogLevel lev, std::string_view line)
Definition log_internal.h:123
std::ofstream m_stream
Definition log_internal.h:129
Definition log_internal.h:98
void log(LogLevel lev, const std::string &combined, const std::string &time, const std::string &thread_name, std::string_view payload_text)
Definition log_internal.h:100
Definition log_internal.h:90
virtual void log(LogLevel, const std::string &combined, const std::string &time, const std::string &thread_name, std::string_view payload_text)=0
virtual void logRaw(LogLevel, std::string_view line)=0
Definition log_internal.h:44
void addOutputMasked(ILogOutput *out, LogLevelMask mask)
Definition log.cpp:145
static LogColor color_mode
Definition log_internal.h:71
std::mutex m_mutex
Definition log_internal.h:83
void logToOutputs(LogLevel, const std::string &combined, const std::string &time, const std::string &thread_name, std::string_view payload_text)
Definition log.cpp:285
void registerThread(std::string_view name)
Definition log.cpp:186
std::vector< ILogOutput * > m_outputs[LL_MAX]
Definition log_internal.h:84
void deregisterThread()
Definition log.cpp:193
void log(LogLevel lev, std::string_view text)
Definition log.cpp:252
void logRaw(LogLevel lev, std::string_view text)
Definition log.cpp:270
const std::string & getThreadName()
Definition log.cpp:219
bool isLevelSilenced(LogLevel level)
Definition log_internal.h:67
static LogTimestamp timestamp_mode
Definition log_internal.h:72
void logToOutputsRaw(LogLevel, std::string_view line)
Definition log.cpp:278
void setLevelSilenced(LogLevel lev, bool silenced)
Definition log.cpp:181
static const char * getLevelLabel(LogLevel lev)
Definition log.cpp:200
LogLevelMask removeOutput(ILogOutput *out)
Definition log.cpp:166
void addOutput(ILogOutput *out)
Definition log.cpp:135
std::atomic< bool > m_silenced_levels[LL_MAX]
Definition log_internal.h:86
std::map< std::thread::id, std::string > m_thread_names
Definition log_internal.h:87
static LogLevel stringToLevel(std::string_view name)
Definition log.cpp:115
std::atomic< bool > m_has_outputs[LL_MAX]
Definition log_internal.h:85
bool hasOutput(LogLevel level)
Definition log_internal.h:63
static std::string getLogTimestamp()
Definition log.cpp:238
void addOutputMaxLevel(ILogOutput *out, LogLevel lev)
Definition log.cpp:156
Definition log_internal.h:108
StreamLogOutput(std::ostream &stream)
Definition log.cpp:325
void logRaw(LogLevel lev, std::string_view line)
Definition log.cpp:336
bool is_tty
Definition log_internal.h:116
std::ostream & m_stream
Definition log_internal.h:115
StreamLogOutput stderr_output
StreamLogOutput stdout_output
Logger g_logger
Definition log.cpp:56
LogTimestamp
Definition log_internal.h:35
@ LOG_TIMESTAMP_RELATIVE
Definition log_internal.h:38
@ LOG_TIMESTAMP_NONE
Definition log_internal.h:36
@ LOG_TIMESTAMP_WALL
Definition log_internal.h:37
LogColor
Definition log_internal.h:29
@ LOG_COLOR_NEVER
Definition log_internal.h:30
@ LOG_COLOR_AUTO
Definition log_internal.h:32
@ LOG_COLOR_ALWAYS
Definition log_internal.h:31
LogLevel
Definition log_internal.h:18
@ LL_INFO
Definition log_internal.h:23
@ LL_ERROR
Definition log_internal.h:20
@ LL_NONE
Definition log_internal.h:19
@ LL_WARNING
Definition log_internal.h:21
@ LL_TRACE
Definition log_internal.h:25
@ LL_ACTION
Definition log_internal.h:22
@ LL_MAX
Definition log_internal.h:26
@ LL_VERBOSE
Definition log_internal.h:24
u8 LogLevelMask
Definition log_internal.h:41
std::lock_guard< std::mutex > MutexAutoLock
Definition mutex_auto_lock.h:31
Definition log_internal.h:132
LogLevel level
Definition log_internal.h:133
std::string timestamp
Definition log_internal.h:134
std::string thread_name
Definition log_internal.h:135
std::string combined
Definition log_internal.h:138
std::string text
Definition log_internal.h:136