Cockatrice/servatrice/src/server_logger.h
BruebachL 825aba953a
[Servatrice] Fail loudly when startup initialization fails (#7602)
* [Servatrice] Exit non-zero when server initialization fails

initServer() reported every startup failure through a zero exit code: the
database could not be opened, the server printed nothing of its own, and the
process looked like a clean shutdown. Log the two refusal reasons at critical
level and return 1 so supervisors and the Docker exit status can tell the
difference.

* [Servatrice] Keep console logging independent of the logfile

logMessage() returned before the console echo unless a logfile was open and
server/writelog was true, so --log-to-console produced nothing when
server/logfile was empty or could not be opened. That is how a refused database
connection could end in a silent exit.

Print to the console before the logfile gates, drop the duplicate echo from
flushBuffer(), and read writelog/logfilters once in main instead of calling the
non-thread-safe global QSettings from every logging thread.

* [Servatrice] Uninstall the log handler before tearing down globals

The handler reaches into the global logger and settingsCache. Leaving it
installed while main deletes those objects turns any Qt warning emitted during
QCoreApplication/QFactoryLoader teardown into a use-after-free; with
QT_DEBUG_PLUGINS=1 that warning exists, and Servatrice died with SIGSEGV in
QSettings::value() on the way out.

* [Servatrice] Buffer console logs on the logger thread

Address review feedback on the startup failure change:

- Append console lines to a buffer drained by the logger thread, so a
  stalled stdout reader cannot block game threads.
- Report islServer->errorString() instead of the literal call text.
- Let the constructor reuse reloadConfig() instead of parsing the filter
  list twice.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-10-31 19:02:49 +01:00

54 lines
1.4 KiB
C++

#ifndef SERVER_LOGGER_H
#define SERVER_LOGGER_H
#include <QList>
#include <QMutex>
#include <QObject>
#include <QString>
#include <QStringList>
#include <qtmetamacros.h>
class QFile;
class ServerLogger : public QObject
{
Q_OBJECT
public:
ServerLogger(bool _logToConsole, bool _writeLog, const QString &_logFilters, QObject *parent = nullptr);
~ServerLogger();
/**
* @brief Whether messages passed to logMessage() would be written anywhere.
*
* Callers use this to avoid formatting expensive debug payloads that
* logMessage() would discard.
*/
[[nodiscard]] bool isLoggingEnabled() const;
public slots:
void startLog(const QString &logFileName);
void reloadConfig(bool _writeLog, const QString &_logFilters);
void logMessage(const QString &message, void *caller = 0);
void rotateLogs();
private slots:
void flushBuffer();
signals:
void sigFlushBuffer();
private:
bool logToConsole;
bool writeLog = true;
bool messageFilterActive = false;
QStringList logFilters;
// Guards writeLog/messageFilterActive/logFilters: SIGHUP rewrites them while other threads log.
QMutex configMutex;
QFile *logFile = nullptr;
bool flushRunning;
QStringList buffer;
// Console lines wait here for the logger thread, so game threads never block on stdout.
QStringList consoleBuffer;
// Guards buffer and consoleBuffer.
QMutex bufferMutex;
};
#endif