diff --git a/code_templates/nsp_dumper_stor.c b/code_templates/nsp_dumper_stor.c index c6a8111..4a02097 100644 --- a/code_templates/nsp_dumper_stor.c +++ b/code_templates/nsp_dumper_stor.c @@ -757,16 +757,14 @@ int main(int argc, char *argv[]) int ret = 0; - consoleInit(NULL); - - consolePrint("initializing...\n"); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + u32 app_count = 0; TitleApplicationMetadata **app_metadata = NULL; TitleUserApplicationData user_app_data = {0}; diff --git a/code_templates/nsp_dumper_usb.c b/code_templates/nsp_dumper_usb.c index 79350ad..ed537d6 100644 --- a/code_templates/nsp_dumper_usb.c +++ b/code_templates/nsp_dumper_usb.c @@ -915,17 +915,14 @@ int main(int argc, char *argv[]) int ret = 0; - consoleInit(NULL); - - consolePrint("initializing...\n"); - consoleRefresh(); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + u32 app_count = 0; TitleApplicationMetadata **app_metadata = NULL; TitleUserApplicationData user_app_data = {0}; diff --git a/code_templates/sd_romfs_dumper.c b/code_templates/sd_romfs_dumper.c index 6af3bd1..963d35a 100644 --- a/code_templates/sd_romfs_dumper.c +++ b/code_templates/sd_romfs_dumper.c @@ -332,16 +332,14 @@ int main(int argc, char *argv[]) int ret = 0; - consoleInit(NULL); - - consolePrint("initializing...\n"); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + u32 app_count = 0; TitleApplicationMetadata **app_metadata = NULL; TitleUserApplicationData user_app_data = {0}; diff --git a/code_templates/system_title_dumper.c b/code_templates/system_title_dumper.c index 3aa9464..b801884 100644 --- a/code_templates/system_title_dumper.c +++ b/code_templates/system_title_dumper.c @@ -229,16 +229,14 @@ int main(int argc, char *argv[]) int ret = 0; - consoleInit(NULL); - - consolePrint("initializing...\n"); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + u32 app_count = 0; TitleApplicationMetadata **app_metadata = NULL; TitleInfo *cur_title_info = NULL; diff --git a/code_templates/usb_gc_dumper.c b/code_templates/usb_gc_dumper.c index 35cf852..0e50452 100644 --- a/code_templates/usb_gc_dumper.c +++ b/code_templates/usb_gc_dumper.c @@ -192,16 +192,14 @@ int main(int argc, char *argv[]) Menu *cur_menu = &g_rootMenu; u32 element_count = menuGetElementCount(cur_menu), page_size = 30; - consoleInit(NULL); - - consolePrint("initializing...\n"); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + while(appletMainLoop()) { consoleClear(); diff --git a/code_templates/usb_romfs_dumper.c b/code_templates/usb_romfs_dumper.c index f6269de..4388e61 100644 --- a/code_templates/usb_romfs_dumper.c +++ b/code_templates/usb_romfs_dumper.c @@ -311,16 +311,14 @@ int main(int argc, char *argv[]) int ret = 0; - consoleInit(NULL); - - consolePrint("initializing...\n"); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + u32 app_count = 0; TitleApplicationMetadata **app_metadata = NULL; TitleUserApplicationData user_app_data = {0}; diff --git a/code_templates/xml_generator.c b/code_templates/xml_generator.c index 9c5e92d..e159201 100644 --- a/code_templates/xml_generator.c +++ b/code_templates/xml_generator.c @@ -57,16 +57,14 @@ int main(int argc, char *argv[]) int ret = 0; - consoleInit(NULL); - - consolePrint("initializing...\n"); - if (!utilsInitializeResources()) { ret = -1; goto out; } + consoleInit(NULL); + u32 app_count = 0; TitleApplicationMetadata **app_metadata = NULL; TitleUserApplicationData user_app_data = {0}; diff --git a/source/log.c b/source/log.c index 529604c..59f5c8b 100644 --- a/source/log.c +++ b/source/log.c @@ -21,13 +21,15 @@ #include "utils.h" #define LOG_FILE_NAME APP_TITLE ".log" -#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_BUF_SIZE 0x400000 /* 4 MiB. */ +#define LOG_FORCE_FLUSH 0 /* Forces a log buffer flush each time the logfile is written to. */ /* Global variables. */ static Mutex g_logMutex = 0; +static char g_lastLogMsg[0x100] = {0}; + static FsFile g_logFile = {0}; static s64 g_logFileOffset = 0; @@ -41,7 +43,7 @@ static const char *g_logLineBreak = "\r\n"; /* Function prototypes. */ 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); @@ -57,7 +59,7 @@ void logWriteFormattedStringToLogFile(const char *func_name, const char *fmt, .. { va_list args; va_start(args, fmt); - _logWriteFormattedStringToLogFile(func_name, fmt, args, true); + _logWriteFormattedStringToLogFile(true, func_name, fmt, args, true); va_end(args); } @@ -144,7 +146,7 @@ void logWriteBinaryDataToLogFile(const void *data, size_t data_size, const char /* Write formatted string. */ va_start(args, fmt); - _logWriteFormattedStringToLogFile(func_name, fmt, args, false); + _logWriteFormattedStringToLogFile(false, func_name, fmt, args, false); va_end(args); /* Write hex string representation. */ @@ -190,6 +192,13 @@ void logCloseLogFile(void) 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) { if (lock) @@ -258,7 +267,7 @@ end: 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; @@ -281,9 +290,6 @@ static void _logWriteFormattedStringToLogFile(const char *func_name, const char ts->tm_year += 1900; 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. */ 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; @@ -293,6 +299,22 @@ static void _logWriteFormattedStringToLogFile(const char *func_name, const char 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. */ if (log_str_len < LOG_BUF_SIZE) { diff --git a/source/log.h b/source/log.h index 9120aad..f38fe2a 100644 --- a/source/log.h +++ b/source/log.h @@ -47,6 +47,9 @@ void logFlushLogFile(void); /// Closes the logfile. 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. /// Use with caution. void logControlMutex(bool lock); diff --git a/source/utils.c b/source/utils.c index feee804..e267a99 100644 --- a/source/utils.c +++ b/source/utils.c @@ -69,6 +69,8 @@ static void utilsUnmountEmmcBisSystemPartitionStorage(void); static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param); +static void utilsPrintConsoleError(void); + bool utilsInitializeResources(void) { mutexLock(&g_resourcesMutex); @@ -194,6 +196,8 @@ bool utilsInitializeResources(void) end: mutexUnlock(&g_resourcesMutex); + if (!ret) utilsPrintConsoleError(); + return ret; } @@ -750,6 +754,24 @@ static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param) 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); } + +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); +}