5410: Add `log_set_loglevel` instead of depending on engine config r=heinrich5991 a=Robyt3

Lets the logger have its own loglevel, which is set by the server and client initially after loading the config and when the `loglevel` config variable changes.

The default logger loglevel is `LEVEL_INFO`, so this fixes the log output not working when the config is not initialized (in tools). Closes #5349. Supersedes #5350.

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2022-06-13 21:33:37 +00:00 committed by GitHub
commit 1f4aa60391
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 3 deletions

View file

@ -3,8 +3,6 @@
#include "color.h"
#include "system.h"
#include <engine/shared/config.h>
#include <atomic>
#include <cstdio>
@ -19,10 +17,16 @@
extern "C" {
std::atomic<LEVEL> loglevel = LEVEL_INFO;
std::atomic<ILogger *> global_logger = nullptr;
thread_local ILogger *scope_logger = nullptr;
thread_local bool in_logger = false;
void log_set_loglevel(LEVEL level)
{
loglevel.store(level, std::memory_order_release);
}
void log_set_global_logger(ILogger *logger)
{
ILogger *null = nullptr;
@ -69,7 +73,7 @@ void log_set_scope_logger(ILogger *logger)
void log_log_impl(LEVEL level, bool have_color, LOG_COLOR color, const char *sys, const char *fmt, va_list args)
{
if(level > g_Config.m_Loglevel)
if(level > loglevel.load(std::memory_order_acquire))
return;
// Make sure we're not logging recursively.

View file

@ -81,6 +81,16 @@ public:
virtual void GlobalFinish() {}
};
/**
* @ingroup Log
*
* Sets the global loglevel to the given value. Log messages with a less severe
* loglevel will not be forwarded to loggers.
*
* @param level The loglevel.
*/
void log_set_loglevel(LEVEL level);
/**
* @ingroup Log
*

View file

@ -4206,6 +4206,12 @@ void CClient::ConchainTimeoutSeed(IConsole::IResult *pResult, void *pUserData, I
pSelf->m_GenerateTimeoutSeed = false;
}
void CClient::ConchainLoglevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
pfnCallback(pResult, pCallbackUserData);
log_set_loglevel((LEVEL)g_Config.m_Loglevel);
}
void CClient::ConchainPassword(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
CClient *pSelf = (CClient *)pUserData;
@ -4286,6 +4292,7 @@ void CClient::RegisterCommands()
m_pConsole->Chain("cl_timeout_seed", ConchainTimeoutSeed, this);
m_pConsole->Chain("cl_replays", ConchainReplays, this);
m_pConsole->Chain("loglevel", ConchainLoglevel, this);
m_pConsole->Chain("password", ConchainPassword, this);
// used for server browser update
@ -4528,6 +4535,7 @@ int main(int argc, const char **argv)
pSteam->ClearConnectAddress();
}
log_set_loglevel((LEVEL)g_Config.m_Loglevel);
if(g_Config.m_Logfile[0])
{
IOHANDLE Logfile = io_open(g_Config.m_Logfile, IOFLAG_WRITE);

View file

@ -453,6 +453,7 @@ public:
static void ConchainWindowScreen(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainWindowVSync(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainTimeoutSeed(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainLoglevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainPassword(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainReplays(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);

View file

@ -3478,6 +3478,12 @@ void CServer::ConDumpSqlServers(IConsole::IResult *pResult, void *pUserData)
}
}
void CServer::ConchainLoglevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
pfnCallback(pResult, pCallbackUserData);
log_set_loglevel((LEVEL)g_Config.m_Loglevel);
}
void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
pfnCallback(pResult, pCallbackUserData);
@ -3707,6 +3713,7 @@ void CServer::RegisterCommands()
Console()->Register("name_bans", "", CFGFLAG_SERVER, ConNameBans, this, "List all name bans");
Console()->Chain("sv_name", ConchainSpecialInfoupdate, this);
Console()->Chain("loglevel", ConchainLoglevel, this);
Console()->Chain("password", ConchainSpecialInfoupdate, this);
Console()->Chain("sv_max_clients_per_ip", ConchainMaxclientsperipUpdate, this);
@ -3885,6 +3892,7 @@ int main(int argc, const char **argv)
pConsole->Register("sv_test_cmds", "", CFGFLAG_SERVER, CServer::ConTestingCommands, pConsole, "Turns testing commands aka cheats on/off (setting only works in initial config)");
pConsole->Register("sv_rescue", "", CFGFLAG_SERVER, CServer::ConRescue, pConsole, "Allow /rescue command so players can teleport themselves out of freeze (setting only works in initial config)");
log_set_loglevel((LEVEL)g_Config.m_Loglevel);
if(g_Config.m_Logfile[0])
{
IOHANDLE Logfile = io_open(g_Config.m_Logfile, IOFLAG_WRITE);

View file

@ -408,6 +408,7 @@ public:
static void ConAddSqlServer(IConsole::IResult *pResult, void *pUserData);
static void ConDumpSqlServers(IConsole::IResult *pResult, void *pUserData);
static void ConchainLoglevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
static void ConchainCommandAccessUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);