Skip to content

Commit

Permalink
log: make logGetLastMessage() return a heap buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkMatterCore committed Jan 20, 2024
1 parent 643847b commit 5ffe065
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
5 changes: 3 additions & 2 deletions include/core/nxdt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions source/core/gamecard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
25 changes: 20 additions & 5 deletions source/core/nxdt_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions source/core/nxdt_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit 5ffe065

Please sign in to comment.