diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 3ac2dd6035..594884c712 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -67,9 +67,7 @@ #include "safeguards.h" -/* static */ const char *CrashLog::message = nullptr; -/* static */ char *CrashLog::gamelog_buffer = nullptr; -/* static */ const char *CrashLog::gamelog_last = nullptr; +/* static */ std::string CrashLog::message{ "" }; char *CrashLog::LogCompiler(char *buffer, const char *last) const { @@ -279,15 +277,6 @@ char *CrashLog::LogLibraries(char *buffer, const char *last) const return buffer; } -/** - * Helper function for printing the gamelog. - * @param s the string to print. - */ -/* static */ void CrashLog::GamelogFillCrashLog(const char *s) -{ - CrashLog::gamelog_buffer += seprintf(CrashLog::gamelog_buffer, CrashLog::gamelog_last, "%s\n", s); -} - /** * Writes the gamelog data to the buffer. * @param buffer The begin where to write at. @@ -296,10 +285,10 @@ char *CrashLog::LogLibraries(char *buffer, const char *last) const */ char *CrashLog::LogGamelog(char *buffer, const char *last) const { - CrashLog::gamelog_buffer = buffer; - CrashLog::gamelog_last = last; - GamelogPrint(&CrashLog::GamelogFillCrashLog); - return CrashLog::gamelog_buffer + seprintf(CrashLog::gamelog_buffer, last, "\n"); + GamelogPrint([&buffer, last](const char *s) { + buffer += seprintf(buffer, last, "%s\n", s); + }); + return buffer + seprintf(buffer, last, "\n"); } /** @@ -358,7 +347,7 @@ char *CrashLog::FillCrashLog(char *buffer, const char *last) const ConvertDateToYMD(_date, &ymd); buffer += seprintf(buffer, last, "In game date: %i-%02i-%02i (%i)\n\n", ymd.year, ymd.month + 1, ymd.day, _date_fract); - buffer = this->LogError(buffer, last, CrashLog::message); + buffer = this->LogError(buffer, last, CrashLog::message.c_str()); buffer = this->LogOpenTTDVersion(buffer, last); buffer = this->LogRegisters(buffer, last); buffer = this->LogStacktrace(buffer, last); diff --git a/src/crashlog.h b/src/crashlog.h index b7945a9839..5f24fc80d6 100644 --- a/src/crashlog.h +++ b/src/crashlog.h @@ -15,16 +15,8 @@ */ class CrashLog { private: - /** Pointer to the error message. */ - static const char *message; - - /** Temporary 'local' location of the buffer. */ - static char *gamelog_buffer; - - /** Temporary 'local' location of the end of the buffer. */ - static const char *gamelog_last; - - static void GamelogFillCrashLog(const char *s); + /** Error message coming from #error(const char *, ...). */ + static std::string message; protected: /** * Writes OS' version to the buffer. @@ -46,7 +38,7 @@ protected: * Writes actually encountered error to the buffer. * @param buffer The begin where to write at. * @param last The last position in the buffer to write to. - * @param message Message passed to use for possible errors. Can be nullptr. + * @param message Message passed to use for errors. * @return the position of the \c '\0' character after the buffer. */ virtual char *LogError(char *buffer, const char *last, const char *message) const = 0; diff --git a/src/gamelog.cpp b/src/gamelog.cpp index 7880d06e42..5a6e4b477b 100644 --- a/src/gamelog.cpp +++ b/src/gamelog.cpp @@ -189,7 +189,7 @@ typedef SmallMap GrfIDMapping; * Prints active gamelog * @param proc the procedure to draw with */ -void GamelogPrint(GamelogPrintProc *proc) +void GamelogPrint(std::function proc) { char buffer[1024]; GrfIDMapping grf_names; @@ -341,25 +341,14 @@ void GamelogPrint(GamelogPrintProc *proc) } -static void GamelogPrintConsoleProc(const char *s) -{ - IConsolePrint(CC_WARNING, s); -} - /** Print the gamelog data to the console. */ void GamelogPrintConsole() { - GamelogPrint(&GamelogPrintConsoleProc); + GamelogPrint([](const char *s) { + IConsolePrint(CC_WARNING, s); + }); } -static int _gamelog_print_level = 0; ///< gamelog debug level we need to print stuff - -static void GamelogPrintDebugProc(const char *s) -{ - Debug(gamelog, _gamelog_print_level, "{}", s); -} - - /** * Prints gamelog to debug output. Code is executed even when * there will be no output. It is called very seldom, so it @@ -368,8 +357,9 @@ static void GamelogPrintDebugProc(const char *s) */ void GamelogPrintDebug(int level) { - _gamelog_print_level = level; - GamelogPrint(&GamelogPrintDebugProc); + GamelogPrint([level](const char *s) { + Debug(gamelog, level, "{}", s); + }); } diff --git a/src/gamelog.h b/src/gamelog.h index cfa6c626a2..0555ca5e77 100644 --- a/src/gamelog.h +++ b/src/gamelog.h @@ -10,6 +10,7 @@ #ifndef GAMELOG_H #define GAMELOG_H +#include #include "newgrf_config.h" /** The actions we log. */ @@ -32,13 +33,7 @@ void GamelogStopAnyAction(); void GamelogFree(struct LoggedAction *gamelog_action, uint gamelog_actions); void GamelogReset(); -/** - * Callback for printing text. - * @param s The string to print. - */ -typedef void GamelogPrintProc(const char *s); -void GamelogPrint(GamelogPrintProc *proc); // needed for WIN32 crash.log - +void GamelogPrint(std::function proc); void GamelogPrintDebug(int level); void GamelogPrintConsole(); diff --git a/src/os/macosx/crashlog_osx.cpp b/src/os/macosx/crashlog_osx.cpp index 682ed6d073..68e2e5633d 100644 --- a/src/os/macosx/crashlog_osx.cpp +++ b/src/os/macosx/crashlog_osx.cpp @@ -80,7 +80,7 @@ class CrashLogOSX : public CrashLog { " Message: %s\n\n", strsignal(this->signum), this->signum, - message == nullptr ? "" : message + message ); } diff --git a/src/os/unix/crashlog_unix.cpp b/src/os/unix/crashlog_unix.cpp index fc3682d024..cb4103e693 100644 --- a/src/os/unix/crashlog_unix.cpp +++ b/src/os/unix/crashlog_unix.cpp @@ -66,7 +66,7 @@ class CrashLogUnix : public CrashLog { " Message: %s\n\n", strsignal(this->signum), this->signum, - message == nullptr ? "" : message + message ); } diff --git a/src/os/windows/crashlog_win.cpp b/src/os/windows/crashlog_win.cpp index e1a05a8aaf..9079267358 100644 --- a/src/os/windows/crashlog_win.cpp +++ b/src/os/windows/crashlog_win.cpp @@ -114,7 +114,7 @@ public: " Message: %s\n\n", (int)ep->ExceptionRecord->ExceptionCode, (size_t)ep->ExceptionRecord->ExceptionAddress, - message == nullptr ? "" : message + message ); }