From 5ffe06508e6219615723a4223a6f7fad99ce999f Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Sat, 20 Jan 2024 23:12:55 +0100 Subject: [PATCH] log: make logGetLastMessage() return a heap buffer --- include/core/nxdt_log.h | 5 +++-- source/core/gamecard.c | 2 -- source/core/nxdt_log.c | 25 ++++++++++++++++++++----- source/core/nxdt_utils.c | 14 +++++++++++--- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/core/nxdt_log.h b/include/core/nxdt_log.h index b67670f..74c1503 100644 --- a/include/core/nxdt_log.h +++ b/include/core/nxdt_log.h @@ -110,8 +110,9 @@ void logFlushLogFile(void); /// Write any pending data to the logfile, flushes it and then closes it. void logCloseLogFile(void); -/// Stores the last log message in the provided buffer. -void logGetLastMessage(char *dst, size_t dst_size); +/// Returns a pointer to a dynamically allocated buffer that holds the last error message string, or NULL if there's none. +/// The allocated buffer must be freed by the calling function using free(). +char *logGetLastMessage(void); /// (Un)locks the log mutex. Can be used to block other threads and prevent them from writing data to the logfile. /// Use with caution. diff --git a/source/core/gamecard.c b/source/core/gamecard.c index a6c3a6b..1ac5b62 100644 --- a/source/core/gamecard.c +++ b/source/core/gamecard.c @@ -24,8 +24,6 @@ #include "gamecard.h" #include "keys.h" -#define GAMECARD_HFS0_MAGIC 0x48465330 /* "HFS0". */ - #define GAMECARD_READ_BUFFER_SIZE 0x800000 /* 8 MiB. */ #define GAMECARD_ACCESS_DELAY 3 /* Seconds. */ diff --git a/source/core/nxdt_log.c b/source/core/nxdt_log.c index 52cd90e..177b5d1 100644 --- a/source/core/nxdt_log.c +++ b/source/core/nxdt_log.c @@ -27,7 +27,7 @@ static Mutex g_logMutex = 0; -static char g_lastLogMsg[0x100] = {0}; +static char *g_lastLogMsg = NULL; static FsFile g_logFile = {0}; static s64 g_logFileOffset = 0; @@ -189,6 +189,13 @@ void logCloseLogFile(void) utilsCommitSdCardFileSystemChanges(); } + /* Free last message bugger. */ + if (g_lastLogMsg) + { + free(g_lastLogMsg); + g_lastLogMsg = NULL; + } + /* Free log buffer. */ if (g_logBuffer) { @@ -201,12 +208,16 @@ void logCloseLogFile(void) } } -void logGetLastMessage(char *dst, size_t dst_size) +char *logGetLastMessage(void) { + char *ret = NULL; + SCOPED_LOCK(&g_logMutex) { - if (dst && dst_size > 1 && *g_lastLogMsg) snprintf(dst, dst_size, "%s", g_lastLogMsg); + if (g_lastLogMsg) ret = strdup(g_lastLogMsg); } + + return ret; } void logControlMutex(bool lock) @@ -310,11 +321,15 @@ static void _logWriteFormattedStringToLogFile(bool save, u8 level, const char *f log_str_len = (size_t)(str1_len + str2_len + 2); - /* Save log message to our global stack buffer (if needed). */ + /* Save log message (if needed). */ if (save) { + if (g_lastLogMsg) free(g_lastLogMsg); + tmp_len = (strlen(func_name) + 2); - if ((tmp_len + (size_t)str2_len) < sizeof(g_lastLogMsg)) + + g_lastLogMsg = calloc(tmp_len + (size_t)str2_len + 1, sizeof(char)); + if (g_lastLogMsg) { sprintf(g_lastLogMsg, "%s: ", func_name); vsprintf(g_lastLogMsg + tmp_len, fmt, args); diff --git a/source/core/nxdt_utils.c b/source/core/nxdt_utils.c index 059736b..3f3f1af 100644 --- a/source/core/nxdt_utils.c +++ b/source/core/nxdt_utils.c @@ -274,9 +274,12 @@ bool utilsInitializeResources(void) #if LOG_LEVEL <= LOG_LEVEL_ERROR /* Get last log message. */ - char log_msg[0x100] = {0}; - logGetLastMessage(log_msg, sizeof(log_msg)); - if (*log_msg) utilsAppendFormattedStringToBuffer(&msg, &msg_size, "\n\n%s", log_msg); + char *log_msg = logGetLastMessage(); + if (log_msg) + { + utilsAppendFormattedStringToBuffer(&msg, &msg_size, "\n\n%s", log_msg); + free(log_msg); + } #endif /* Print error message. */ @@ -1092,7 +1095,12 @@ void utilsPrintConsoleError(const char *msg) printf("An error occurred."); } +#if LOG_LEVEL < LOG_LEVEL_NONE printf("\n\nFor more information, please check the logfile. Press any button to exit."); +#else + printf("\n\nPress any button to exit."); +#endif + consoleUpdate(NULL); /* Wait until the user presses a button. */