Store last log message using a small stack buffer.

Will be used to print error messages down the road.
This commit is contained in:
Pablo Curiel 2021-03-08 10:44:11 -04:00
parent 43f744326f
commit 8be5460229
10 changed files with 71 additions and 39 deletions

View file

@ -757,16 +757,14 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
consoleInit(NULL);
consolePrint("initializing...\n");
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
u32 app_count = 0; u32 app_count = 0;
TitleApplicationMetadata **app_metadata = NULL; TitleApplicationMetadata **app_metadata = NULL;
TitleUserApplicationData user_app_data = {0}; TitleUserApplicationData user_app_data = {0};

View file

@ -915,17 +915,14 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
consoleInit(NULL);
consolePrint("initializing...\n");
consoleRefresh();
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
u32 app_count = 0; u32 app_count = 0;
TitleApplicationMetadata **app_metadata = NULL; TitleApplicationMetadata **app_metadata = NULL;
TitleUserApplicationData user_app_data = {0}; TitleUserApplicationData user_app_data = {0};

View file

@ -332,16 +332,14 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
consoleInit(NULL);
consolePrint("initializing...\n");
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
u32 app_count = 0; u32 app_count = 0;
TitleApplicationMetadata **app_metadata = NULL; TitleApplicationMetadata **app_metadata = NULL;
TitleUserApplicationData user_app_data = {0}; TitleUserApplicationData user_app_data = {0};

View file

@ -229,16 +229,14 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
consoleInit(NULL);
consolePrint("initializing...\n");
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
u32 app_count = 0; u32 app_count = 0;
TitleApplicationMetadata **app_metadata = NULL; TitleApplicationMetadata **app_metadata = NULL;
TitleInfo *cur_title_info = NULL; TitleInfo *cur_title_info = NULL;

View file

@ -192,16 +192,14 @@ int main(int argc, char *argv[])
Menu *cur_menu = &g_rootMenu; Menu *cur_menu = &g_rootMenu;
u32 element_count = menuGetElementCount(cur_menu), page_size = 30; u32 element_count = menuGetElementCount(cur_menu), page_size = 30;
consoleInit(NULL);
consolePrint("initializing...\n");
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
while(appletMainLoop()) while(appletMainLoop())
{ {
consoleClear(); consoleClear();

View file

@ -311,16 +311,14 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
consoleInit(NULL);
consolePrint("initializing...\n");
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
u32 app_count = 0; u32 app_count = 0;
TitleApplicationMetadata **app_metadata = NULL; TitleApplicationMetadata **app_metadata = NULL;
TitleUserApplicationData user_app_data = {0}; TitleUserApplicationData user_app_data = {0};

View file

@ -57,16 +57,14 @@ int main(int argc, char *argv[])
int ret = 0; int ret = 0;
consoleInit(NULL);
consolePrint("initializing...\n");
if (!utilsInitializeResources()) if (!utilsInitializeResources())
{ {
ret = -1; ret = -1;
goto out; goto out;
} }
consoleInit(NULL);
u32 app_count = 0; u32 app_count = 0;
TitleApplicationMetadata **app_metadata = NULL; TitleApplicationMetadata **app_metadata = NULL;
TitleUserApplicationData user_app_data = {0}; TitleUserApplicationData user_app_data = {0};

View file

@ -21,13 +21,15 @@
#include "utils.h" #include "utils.h"
#define LOG_FILE_NAME APP_TITLE ".log" #define LOG_FILE_NAME APP_TITLE ".log"
#define LOG_BUF_SIZE 0x400000 /* 4 MiB. */ #define LOG_BUF_SIZE 0x400000 /* 4 MiB. */
#define LOG_FORCE_FLUSH 0 /* Forces a log buffer flush each time the logfile is written to. */ #define LOG_FORCE_FLUSH 0 /* Forces a log buffer flush each time the logfile is written to. */
/* Global variables. */ /* Global variables. */
static Mutex g_logMutex = 0; static Mutex g_logMutex = 0;
static char g_lastLogMsg[0x100] = {0};
static FsFile g_logFile = {0}; static FsFile g_logFile = {0};
static s64 g_logFileOffset = 0; static s64 g_logFileOffset = 0;
@ -41,7 +43,7 @@ static const char *g_logLineBreak = "\r\n";
/* Function prototypes. */ /* Function prototypes. */
static void _logWriteStringToLogFile(const char *src, bool lock); static void _logWriteStringToLogFile(const char *src, bool lock);
static void _logWriteFormattedStringToLogFile(const char *func_name, const char *fmt, va_list args, bool lock); static void _logWriteFormattedStringToLogFile(bool save, const char *func_name, const char *fmt, va_list args, bool lock);
static void _logFlushLogFile(bool lock); static void _logFlushLogFile(bool lock);
@ -57,7 +59,7 @@ void logWriteFormattedStringToLogFile(const char *func_name, const char *fmt, ..
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
_logWriteFormattedStringToLogFile(func_name, fmt, args, true); _logWriteFormattedStringToLogFile(true, func_name, fmt, args, true);
va_end(args); va_end(args);
} }
@ -144,7 +146,7 @@ void logWriteBinaryDataToLogFile(const void *data, size_t data_size, const char
/* Write formatted string. */ /* Write formatted string. */
va_start(args, fmt); va_start(args, fmt);
_logWriteFormattedStringToLogFile(func_name, fmt, args, false); _logWriteFormattedStringToLogFile(false, func_name, fmt, args, false);
va_end(args); va_end(args);
/* Write hex string representation. */ /* Write hex string representation. */
@ -190,6 +192,13 @@ void logCloseLogFile(void)
mutexUnlock(&g_logMutex); mutexUnlock(&g_logMutex);
} }
void logGetLastMessage(char *dst, size_t dst_size)
{
mutexLock(&g_logMutex);
if (dst && dst_size > 1 && *g_lastLogMsg) snprintf(dst, dst_size, "%s", g_lastLogMsg);
mutexUnlock(&g_logMutex);
}
void logControlMutex(bool lock) void logControlMutex(bool lock)
{ {
if (lock) if (lock)
@ -258,7 +267,7 @@ end:
if (lock) mutexUnlock(&g_logMutex); if (lock) mutexUnlock(&g_logMutex);
} }
static void _logWriteFormattedStringToLogFile(const char *func_name, const char *fmt, va_list args, bool lock) static void _logWriteFormattedStringToLogFile(bool save, const char *func_name, const char *fmt, va_list args, bool lock)
{ {
if (!func_name || !*func_name || !fmt || !*fmt) return; if (!func_name || !*func_name || !fmt || !*fmt) return;
@ -281,9 +290,6 @@ static void _logWriteFormattedStringToLogFile(const char *func_name, const char
ts->tm_year += 1900; ts->tm_year += 1900;
ts->tm_mon++; ts->tm_mon++;
/* Make sure we have allocated memory for the log buffer and opened the logfile. */
if (!logAllocateLogBuffer() || !logOpenLogFile()) goto end;
/* Get formatted string length. */ /* Get formatted string length. */
str1_len = snprintf(NULL, 0, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name); str1_len = snprintf(NULL, 0, g_logStrFormat, ts->tm_year, ts->tm_mon, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, now.tv_nsec, func_name);
if (str1_len <= 0) goto end; if (str1_len <= 0) goto end;
@ -293,6 +299,22 @@ static void _logWriteFormattedStringToLogFile(const char *func_name, const char
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). */
if (save)
{
tmp_len = (strlen(func_name) + 2);
if ((tmp_len + (size_t)str2_len) < sizeof(g_lastLogMsg))
{
sprintf(g_lastLogMsg, "%s: ", func_name);
vsprintf(g_lastLogMsg + tmp_len, fmt, args);
}
tmp_len = 0;
}
/* Make sure we have allocated memory for the log buffer and opened the logfile. */
if (!logAllocateLogBuffer() || !logOpenLogFile()) goto end;
/* Check if the formatted string length is less than the log buffer size. */ /* Check if the formatted string length is less than the log buffer size. */
if (log_str_len < LOG_BUF_SIZE) if (log_str_len < LOG_BUF_SIZE)
{ {

View file

@ -47,6 +47,9 @@ void logFlushLogFile(void);
/// Closes the logfile. /// Closes the logfile.
void logCloseLogFile(void); void logCloseLogFile(void);
/// Stores the last log message in the provided buffer.
void logGetLastMessage(char *dst, size_t dst_size);
/// (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.
void logControlMutex(bool lock); void logControlMutex(bool lock);

View file

@ -69,6 +69,8 @@ static void utilsUnmountEmmcBisSystemPartitionStorage(void);
static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param); static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param);
static void utilsPrintConsoleError(void);
bool utilsInitializeResources(void) bool utilsInitializeResources(void)
{ {
mutexLock(&g_resourcesMutex); mutexLock(&g_resourcesMutex);
@ -194,6 +196,8 @@ bool utilsInitializeResources(void)
end: end:
mutexUnlock(&g_resourcesMutex); mutexUnlock(&g_resourcesMutex);
if (!ret) utilsPrintConsoleError();
return ret; return ret;
} }
@ -750,6 +754,24 @@ static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param)
if (hook != AppletHookType_OnOperationMode && hook != AppletHookType_OnPerformanceMode) return; if (hook != AppletHookType_OnOperationMode && hook != AppletHookType_OnPerformanceMode) return;
/* To do: read config here to actually know the value to use with utilsOverclockSystem. */ /* TO DO: read config here to actually know the value to use with utilsOverclockSystem. */
utilsOverclockSystem(false); utilsOverclockSystem(false);
} }
static void utilsPrintConsoleError(void)
{
char msg[0x100] = {0};
logGetLastMessage(msg, sizeof(msg));
consoleInit(NULL);
printf("An error occurred while initializing resources.\n\n");
if (*msg) printf("%s\n\n", msg);
printf("For more information, please check the logfile. Press any button to exit.");
consoleUpdate(NULL);
utilsWaitForButtonPress(0);
consoleExit(NULL);
}