1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-09 12:11:44 +00:00

log: make logGetLastMessage() return a heap buffer

This commit is contained in:
Pablo Curiel 2024-01-20 23:12:55 +01:00
parent 643847be39
commit 5ffe06508e
4 changed files with 34 additions and 12 deletions

View file

@ -110,8 +110,9 @@ void logFlushLogFile(void);
/// Write any pending data to the logfile, flushes it and then closes it. /// Write any pending data to the logfile, flushes it and then closes it.
void logCloseLogFile(void); void logCloseLogFile(void);
/// Stores the last log message in the provided buffer. /// Returns a pointer to a dynamically allocated buffer that holds the last error message string, or NULL if there's none.
void logGetLastMessage(char *dst, size_t dst_size); /// 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. /// (Un)locks the log mutex. Can be used to block other threads and prevent them from writing data to the logfile.
/// Use with caution. /// Use with caution.

View file

@ -24,8 +24,6 @@
#include "gamecard.h" #include "gamecard.h"
#include "keys.h" #include "keys.h"
#define GAMECARD_HFS0_MAGIC 0x48465330 /* "HFS0". */
#define GAMECARD_READ_BUFFER_SIZE 0x800000 /* 8 MiB. */ #define GAMECARD_READ_BUFFER_SIZE 0x800000 /* 8 MiB. */
#define GAMECARD_ACCESS_DELAY 3 /* Seconds. */ #define GAMECARD_ACCESS_DELAY 3 /* Seconds. */

View file

@ -27,7 +27,7 @@
static Mutex g_logMutex = 0; static Mutex g_logMutex = 0;
static char g_lastLogMsg[0x100] = {0}; static char *g_lastLogMsg = NULL;
static FsFile g_logFile = {0}; static FsFile g_logFile = {0};
static s64 g_logFileOffset = 0; static s64 g_logFileOffset = 0;
@ -189,6 +189,13 @@ void logCloseLogFile(void)
utilsCommitSdCardFileSystemChanges(); utilsCommitSdCardFileSystemChanges();
} }
/* Free last message bugger. */
if (g_lastLogMsg)
{
free(g_lastLogMsg);
g_lastLogMsg = NULL;
}
/* Free log buffer. */ /* Free log buffer. */
if (g_logBuffer) 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) 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) 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); 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 (save)
{ {
if (g_lastLogMsg) free(g_lastLogMsg);
tmp_len = (strlen(func_name) + 2); 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); sprintf(g_lastLogMsg, "%s: ", func_name);
vsprintf(g_lastLogMsg + tmp_len, fmt, args); vsprintf(g_lastLogMsg + tmp_len, fmt, args);

View file

@ -274,9 +274,12 @@ bool utilsInitializeResources(void)
#if LOG_LEVEL <= LOG_LEVEL_ERROR #if LOG_LEVEL <= LOG_LEVEL_ERROR
/* Get last log message. */ /* Get last log message. */
char log_msg[0x100] = {0}; char *log_msg = logGetLastMessage();
logGetLastMessage(log_msg, sizeof(log_msg)); if (log_msg)
if (*log_msg) utilsAppendFormattedStringToBuffer(&msg, &msg_size, "\n\n%s", log_msg); {
utilsAppendFormattedStringToBuffer(&msg, &msg_size, "\n\n%s", log_msg);
free(log_msg);
}
#endif #endif
/* Print error message. */ /* Print error message. */
@ -1092,7 +1095,12 @@ void utilsPrintConsoleError(const char *msg)
printf("An error occurred."); printf("An error occurred.");
} }
#if LOG_LEVEL < LOG_LEVEL_NONE
printf("\n\nFor more information, please check the logfile. Press any button to exit."); 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); consoleUpdate(NULL);
/* Wait until the user presses a button. */ /* Wait until the user presses a button. */