Skip to content

Commit 836dd4a

Browse files
authoredMay 14, 2020
Add chat_log_level setting (#9223)
Log all higher levels in LogOutputBuffer Move StreamLogOutput::logRaw to source file like LogOutputBuffer::logRaw for compiling speed
1 parent 2d7e000 commit 836dd4a

File tree

5 files changed

+95
-51
lines changed

5 files changed

+95
-51
lines changed
 

Diff for: ‎builtin/settingtypes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,9 @@ debug_log_level (Debug log level) enum action ,none,error,warning,action,info,ve
14011401
# debug.txt is only moved if this setting is positive.
14021402
debug_log_size_max (Debug log file size threshold) int 50
14031403

1404+
# Minimal level of logging to be written to chat.
1405+
chat_log_level (Chat log level) enum error ,none,error,warning,action,info,verbose
1406+
14041407
# Enable IPv6 support (for both client and server).
14051408
# Required for IPv6 connections to work at all.
14061409
enable_ipv6 (IPv6) bool true

Diff for: ‎src/client/game.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ class Game {
855855
SoundMaker *soundmaker = nullptr;
856856

857857
ChatBackend *chat_backend = nullptr;
858+
LogOutputBuffer m_chat_log_buf;
858859

859860
EventManager *eventmgr = nullptr;
860861
QuicktuneShortcutter *quicktune = nullptr;
@@ -926,6 +927,7 @@ class Game {
926927
};
927928

928929
Game::Game() :
930+
m_chat_log_buf(g_logger),
929931
m_game_ui(new GameUI())
930932
{
931933
g_settings->registerChangedCallback("doubletap_jump",
@@ -1192,6 +1194,7 @@ void Game::shutdown()
11921194

11931195
chat_backend->addMessage(L"", L"# Disconnected.");
11941196
chat_backend->addMessage(L"", L"");
1197+
m_chat_log_buf.clear();
11951198

11961199
if (client) {
11971200
client->Stop();
@@ -2903,18 +2906,9 @@ void Game::processClientEvents(CameraOrientation *cam)
29032906

29042907
void Game::updateChat(f32 dtime, const v2u32 &screensize)
29052908
{
2906-
// Add chat log output for errors to be shown in chat
2907-
static LogOutputBuffer chat_log_error_buf(g_logger, LL_ERROR);
2908-
29092909
// Get new messages from error log buffer
2910-
while (!chat_log_error_buf.empty()) {
2911-
std::wstring error_message = utf8_to_wide(chat_log_error_buf.get());
2912-
if (!g_settings->getBool("disable_escape_sequences")) {
2913-
error_message.insert(0, L"\x1b(c@red)");
2914-
error_message.append(L"\x1b(c@white)");
2915-
}
2916-
chat_backend->addMessage(L"", error_message);
2917-
}
2910+
while (!m_chat_log_buf.empty())
2911+
chat_backend->addMessage(L"", utf8_to_wide(m_chat_log_buf.get()));
29182912

29192913
// Get new messages from client
29202914
std::wstring message;

Diff for: ‎src/defaultsettings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ void set_default_settings(Settings *settings)
400400
settings->setDefault("remote_media", "");
401401
settings->setDefault("debug_log_level", "action");
402402
settings->setDefault("debug_log_size_max", "50");
403+
settings->setDefault("chat_log_level", "error");
403404
settings->setDefault("emergequeue_limit_total", "512");
404405
settings->setDefault("emergequeue_limit_diskonly", "64");
405406
settings->setDefault("emergequeue_limit_generate", "64");

Diff for: ‎src/log.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include "debug.h"
2424
#include "gettime.h"
2525
#include "porting.h"
26+
#include "settings.h"
2627
#include "config.h"
2728
#include "exceptions.h"
2829
#include "util/numeric.h"
@@ -338,7 +339,80 @@ void FileLogOutput::setFile(const std::string &filename, s64 file_size_max)
338339
"-------------\n" << std::endl;
339340
}
340341

342+
void StreamLogOutput::logRaw(LogLevel lev, const std::string &line)
343+
{
344+
bool colored_message = (Logger::color_mode == LOG_COLOR_ALWAYS) ||
345+
(Logger::color_mode == LOG_COLOR_AUTO && is_tty);
346+
if (colored_message) {
347+
switch (lev) {
348+
case LL_ERROR:
349+
// error is red
350+
m_stream << "\033[91m";
351+
break;
352+
case LL_WARNING:
353+
// warning is yellow
354+
m_stream << "\033[93m";
355+
break;
356+
case LL_INFO:
357+
// info is a bit dark
358+
m_stream << "\033[37m";
359+
break;
360+
case LL_VERBOSE:
361+
// verbose is darker than info
362+
m_stream << "\033[2m";
363+
break;
364+
default:
365+
// action is white
366+
colored_message = false;
367+
}
368+
}
341369

370+
m_stream << line << std::endl;
371+
372+
if (colored_message) {
373+
// reset to white color
374+
m_stream << "\033[0m";
375+
}
376+
}
377+
378+
void LogOutputBuffer::updateLogLevel()
379+
{
380+
const std::string &conf_loglev = g_settings->get("chat_log_level");
381+
LogLevel log_level = Logger::stringToLevel(conf_loglev);
382+
if (log_level == LL_MAX) {
383+
warningstream << "Supplied unrecognized chat_log_level; "
384+
"showing none." << std::endl;
385+
log_level = LL_NONE;
386+
}
387+
388+
m_logger.removeOutput(this);
389+
m_logger.addOutputMaxLevel(this, log_level);
390+
}
391+
392+
void LogOutputBuffer::logRaw(LogLevel lev, const std::string &line)
393+
{
394+
std::string color;
395+
396+
if (!g_settings->getBool("disable_escape_sequences")) {
397+
switch (lev) {
398+
case LL_ERROR: // red
399+
color = "\x1b(c@#F00)";
400+
break;
401+
case LL_WARNING: // yellow
402+
color = "\x1b(c@#EE0)";
403+
break;
404+
case LL_INFO: // grey
405+
color = "\x1b(c@#BBB)";
406+
break;
407+
case LL_VERBOSE: // dark grey
408+
color = "\x1b(c@#888)";
409+
break;
410+
default: break;
411+
}
412+
}
413+
414+
m_buffer.push(color.append(line));
415+
}
342416

343417
////
344418
//// *Buffer methods

Diff for: ‎src/log.h

+12-40
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,7 @@ class StreamLogOutput : public ICombinedLogOutput {
124124
#endif
125125
}
126126

127-
void logRaw(LogLevel lev, const std::string &line)
128-
{
129-
bool colored_message = (Logger::color_mode == LOG_COLOR_ALWAYS) ||
130-
(Logger::color_mode == LOG_COLOR_AUTO && is_tty);
131-
if (colored_message)
132-
switch (lev) {
133-
case LL_ERROR:
134-
// error is red
135-
m_stream << "\033[91m";
136-
break;
137-
case LL_WARNING:
138-
// warning is yellow
139-
m_stream << "\033[93m";
140-
break;
141-
case LL_INFO:
142-
// info is a bit dark
143-
m_stream << "\033[37m";
144-
break;
145-
case LL_VERBOSE:
146-
// verbose is darker than info
147-
m_stream << "\033[2m";
148-
break;
149-
default:
150-
// action is white
151-
colored_message = false;
152-
}
153-
154-
m_stream << line << std::endl;
155-
156-
if (colored_message)
157-
// reset to white color
158-
m_stream << "\033[0m";
159-
}
127+
void logRaw(LogLevel lev, const std::string &line);
160128

161129
private:
162130
std::ostream &m_stream;
@@ -178,23 +146,27 @@ class FileLogOutput : public ICombinedLogOutput {
178146

179147
class LogOutputBuffer : public ICombinedLogOutput {
180148
public:
181-
LogOutputBuffer(Logger &logger, LogLevel lev) :
149+
LogOutputBuffer(Logger &logger) :
182150
m_logger(logger)
183151
{
184-
m_logger.addOutput(this, lev);
185-
}
152+
updateLogLevel();
153+
};
186154

187-
~LogOutputBuffer()
155+
virtual ~LogOutputBuffer()
188156
{
189157
m_logger.removeOutput(this);
190158
}
191159

192-
void logRaw(LogLevel lev, const std::string &line)
160+
void updateLogLevel();
161+
162+
void logRaw(LogLevel lev, const std::string &line);
163+
164+
void clear()
193165
{
194-
m_buffer.push(line);
166+
m_buffer = std::queue<std::string>();
195167
}
196168

197-
bool empty()
169+
bool empty() const
198170
{
199171
return m_buffer.empty();
200172
}

0 commit comments

Comments
 (0)
Please sign in to comment.