Add no-op logger to fix leak of CFutureLogger pending messages

Fix leak of pending future logger log messages if the future logger is not set, in particular when the `logfile` config variable is not set or the file could not be opened, by setting a logger that discards all log messages in this case.

Closes #8265.
This commit is contained in:
Robert Müller 2024-04-28 16:25:35 +02:00
parent 516315e0e2
commit b3bcfd39bc
4 changed files with 33 additions and 2 deletions

View file

@ -483,6 +483,19 @@ std::unique_ptr<ILogger> log_logger_windows_debugger()
}
#endif
class CLoggerNoOp : public ILogger
{
public:
void Log(const CLogMessage *pMessage) override
{
// no-op
}
};
std::unique_ptr<ILogger> log_logger_noop()
{
return std::make_unique<CLoggerNoOp>();
}
void CFutureLogger::Set(std::shared_ptr<ILogger> pLogger)
{
const CLockScope LockScope(m_PendingLock);

View file

@ -210,6 +210,13 @@ std::unique_ptr<ILogger> log_logger_stdout();
*/
std::unique_ptr<ILogger> log_logger_windows_debugger();
/**
* @ingroup Log
*
* Logger which discards all logs.
*/
std::unique_ptr<ILogger> log_logger_noop();
/**
* @ingroup Log
*

View file

@ -4499,9 +4499,9 @@ int main(int argc, const char **argv)
pSteam->ClearConnectAddress();
}
const int Mode = g_Config.m_Logappend ? IOFLAG_APPEND : IOFLAG_WRITE;
if(g_Config.m_Logfile[0])
{
const int Mode = g_Config.m_Logappend ? IOFLAG_APPEND : IOFLAG_WRITE;
IOHANDLE Logfile = pStorage->OpenFile(g_Config.m_Logfile, Mode, IStorage::TYPE_SAVE_OR_ABSOLUTE);
if(Logfile)
{
@ -4510,8 +4510,13 @@ int main(int argc, const char **argv)
else
{
log_error("client", "failed to open '%s' for logging", g_Config.m_Logfile);
pFutureFileLogger->Set(log_logger_noop());
}
}
else
{
pFutureFileLogger->Set(log_logger_noop());
}
// Register protocol and file extensions
#if defined(CONF_FAMILY_WINDOWS)

View file

@ -170,9 +170,9 @@ int main(int argc, const char **argv)
pConfigManager->SetReadOnly("sv_test_cmds", true);
pConfigManager->SetReadOnly("sv_rescue", true);
const int Mode = g_Config.m_Logappend ? IOFLAG_APPEND : IOFLAG_WRITE;
if(g_Config.m_Logfile[0])
{
const int Mode = g_Config.m_Logappend ? IOFLAG_APPEND : IOFLAG_WRITE;
IOHANDLE Logfile = pStorage->OpenFile(g_Config.m_Logfile, Mode, IStorage::TYPE_SAVE_OR_ABSOLUTE);
if(Logfile)
{
@ -181,8 +181,14 @@ int main(int argc, const char **argv)
else
{
log_error("server", "failed to open '%s' for logging", g_Config.m_Logfile);
pFutureFileLogger->Set(log_logger_noop());
}
}
else
{
pFutureFileLogger->Set(log_logger_noop());
}
auto pServerLogger = std::make_shared<CServerLogger>(pServer);
pEngine->SetAdditionalLogger(pServerLogger);