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