Codechange: put stack variables in global variables for gamelog/crashlog

This commit is contained in:
Rubidium 2023-01-27 22:40:30 +01:00 committed by rubidium42
parent ed7685910d
commit d51d08ddcb
7 changed files with 21 additions and 55 deletions

View File

@ -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{ "<none>" };
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);

View File

@ -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;

View File

@ -189,7 +189,7 @@ typedef SmallMap<uint32, GRFPresence> GrfIDMapping;
* Prints active gamelog
* @param proc the procedure to draw with
*/
void GamelogPrint(GamelogPrintProc *proc)
void GamelogPrint(std::function<void(const char*)> 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);
});
}

View File

@ -10,6 +10,7 @@
#ifndef GAMELOG_H
#define GAMELOG_H
#include <functional>
#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<void(const char *)> proc);
void GamelogPrintDebug(int level);
void GamelogPrintConsole();

View File

@ -80,7 +80,7 @@ class CrashLogOSX : public CrashLog {
" Message: %s\n\n",
strsignal(this->signum),
this->signum,
message == nullptr ? "<none>" : message
message
);
}

View File

@ -66,7 +66,7 @@ class CrashLogUnix : public CrashLog {
" Message: %s\n\n",
strsignal(this->signum),
this->signum,
message == nullptr ? "<none>" : message
message
);
}

View File

@ -114,7 +114,7 @@ public:
" Message: %s\n\n",
(int)ep->ExceptionRecord->ExceptionCode,
(size_t)ep->ExceptionRecord->ExceptionAddress,
message == nullptr ? "<none>" : message
message
);
}