From 3e1d862aff3aa730db212480b6c911d0ab7afe68 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 27 Apr 2024 12:25:32 +0200 Subject: [PATCH] Fix logging --- include/log.h | 2 +- source/log.cpp | 6 ++-- source/wiiu/logger.c | 42 +++++++++++++++++++++++++ source/wiiu/logger.h | 67 ++++++++++++++++++++++++++++++++++++++++ source/wiiu/platform.cpp | 23 ++++++++------ 5 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 source/wiiu/logger.c create mode 100644 source/wiiu/logger.h diff --git a/include/log.h b/include/log.h index a0a8659..29122fa 100644 --- a/include/log.h +++ b/include/log.h @@ -27,7 +27,7 @@ /// \brief Log level enum LogLevel { - DEBUG, + DEBUGLOG, INFO, ERROR, COMMAND, diff --git a/source/log.cpp b/source/log.cpp index 00c2e1d..bdcaa5f 100644 --- a/source/log.cpp +++ b/source/log.cpp @@ -50,7 +50,7 @@ bool s_logUpdated = true; /// \brief Message prefix static char const *const s_prefix[] = { - [DEBUG] = "[DEBUG]", + [DEBUGLOG] = "[DEBUG]", [INFO] = "[INFO]", [ERROR] = "[ERROR]", [COMMAND] = "[COMMAND]", @@ -114,7 +114,7 @@ void drawLog () #ifdef CLASSIC char const *const s_colors[] = { - [DEBUG] = "\x1b[33;1m", // yellow + [DEBUGLOG] = "\x1b[33;1m", // yellow [INFO] = "\x1b[37;1m", // white [ERROR] = "\x1b[31;1m", // red [COMMAND] = "\x1b[32;1m", // green @@ -204,7 +204,7 @@ void debug (char const *const fmt_, ...) va_list ap; va_start (ap, fmt_); - addLog (DEBUG, fmt_, ap); + addLog (DEBUGLOG, fmt_, ap); va_end (ap); #endif } diff --git a/source/wiiu/logger.c b/source/wiiu/logger.c new file mode 100644 index 0000000..f1ce793 --- /dev/null +++ b/source/wiiu/logger.c @@ -0,0 +1,42 @@ +#ifdef DEBUG +#include +#include +#include +#include + +uint32_t moduleLogInit = false; +uint32_t cafeLogInit = false; +uint32_t udpLogInit = false; +#endif // DEBUG + +void initLogging () +{ +#ifdef DEBUG + if (!(moduleLogInit = WHBLogModuleInit ())) + { + cafeLogInit = WHBLogCafeInit (); + udpLogInit = WHBLogUdpInit (); + } +#endif // DEBUG +} + +void deinitLogging () +{ +#ifdef DEBUG + if (moduleLogInit) + { + WHBLogModuleDeinit (); + moduleLogInit = false; + } + if (cafeLogInit) + { + WHBLogCafeDeinit (); + cafeLogInit = false; + } + if (udpLogInit) + { + WHBLogUdpDeinit (); + udpLogInit = false; + } +#endif // DEBUG +} \ No newline at end of file diff --git a/source/wiiu/logger.h b/source/wiiu/logger.h new file mode 100644 index 0000000..4b27cf5 --- /dev/null +++ b/source/wiiu/logger.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define LOG_APP_TYPE "P" +#define LOG_APP_NAME "homebrew_on_menu" + +#define __FILENAME_X__ (strrchr (__FILE__, '\\') ? strrchr (__FILE__, '\\') + 1 : __FILE__) +#define __FILENAME__ (strrchr (__FILE__, '/') ? strrchr (__FILE__, '/') + 1 : __FILENAME_X__) + +#define LOG(LOG_FUNC, FMT, ARGS...) LOG_EX (LOG_FUNC, "", "", FMT, ##ARGS) + +#define LOG_EX(LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ARGS...) \ + do \ + { \ + LOG_FUNC ("[(%s)%18s][%23s]%30s@L%04d: " LOG_LEVEL "" FMT "" LINE_END, \ + LOG_APP_TYPE, \ + LOG_APP_NAME, \ + __FILENAME__, \ + __FUNCTION__, \ + __LINE__, \ + ##ARGS); \ + } while (0) + +#ifdef DEBUG + +#ifdef VERBOSE_DEBUG +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) LOG (WHBLogPrintf, FMT, ##ARGS) +#else +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) +#endif + +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) LOG (WHBLogPrintf, FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) LOG (WHBLogWritef, FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX (WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX (WHBLogPrintf, "##WARN ## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX (WHBLogPrintf, "##INFO ## ", "", FMT, ##ARGS) + +#else + +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX (OSReport, "##ERROR## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX (OSReport, "##WARN ## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX (OSReport, "##INFO ## ", "\n", FMT, ##ARGS) + +#endif + +void initLogging (); + +void deinitLogging (); + +#ifdef __cplusplus +} +#endif diff --git a/source/wiiu/platform.cpp b/source/wiiu/platform.cpp index afadaab..392ddc8 100644 --- a/source/wiiu/platform.cpp +++ b/source/wiiu/platform.cpp @@ -24,6 +24,7 @@ #include "IOAbstraction.h" #include "ftpServer.h" #include "log.h" +#include "logger.h" #include #include @@ -86,7 +87,8 @@ MochaUtilsStatus MountWrapper (const char *mount, const char *dev, const char *m } else { - error ("Failed to mount %s: %s [%d]", mount, Mocha_GetStatusStr (res), res); + DEBUG_FUNCTION_LINE_ERR ( + "Failed to mount %s: %s [%d]", mount, Mocha_GetStatusStr (res), res); } return res; } @@ -169,7 +171,8 @@ void start_server () } else { - OSReport ("Failed to init libmocha: %s [%d]\n", Mocha_GetStatusStr (res), res); + DEBUG_FUNCTION_LINE_ERR ( + "Failed to init libmocha: %s [%d]\n", Mocha_GetStatusStr (res), res); } server = FtpServer::create (); @@ -209,7 +212,7 @@ static void gFTPServerRunningChanged (ConfigItemBoolean *item, bool newValue) auto res = WUPSStorageAPI::Store (FTPIIU_ENABLED_STRING, sFTPServerEnabled); if (res != WUPS_STORAGE_ERROR_SUCCESS) { - OSReport ("Failed to store gFTPServerEnabled: %s (%d)\n", + DEBUG_FUNCTION_LINE_ERR ("Failed to store gFTPServerEnabled: %s (%d)\n", WUPSStorageAPI::GetStatusStr (res).data (), res); } @@ -232,7 +235,7 @@ static void gSystemFilesAllowedChanged (ConfigItemBoolean *item, bool newValue) auto res = WUPSStorageAPI::Store (SYSTEM_FILES_ALLOWED_STRING, sSystemFilesAllowed); if (res != WUPS_STORAGE_ERROR_SUCCESS) { - OSReport ("Failed to store gSystemFilesAllowed: %s (%d)\n", + DEBUG_FUNCTION_LINE_ERR ("Failed to store gSystemFilesAllowed: %s (%d)\n", WUPSStorageAPI::GetStatusStr (res).data (), res); } @@ -300,6 +303,7 @@ INITIALIZE_PLUGIN () if (WUPSConfigAPI_Init (configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR ("Failed to init config api"); OSFatal ("ftpiiu plugin: Failed to init config api"); } @@ -308,7 +312,7 @@ INITIALIZE_PLUGIN () FTPIIU_ENABLED_STRING, sFTPServerEnabled, DEFAULT_FTPIIU_ENABLED_VALUE)) != WUPS_STORAGE_ERROR_SUCCESS) { - OSReport ("ftpiiu plugin: Failed to get or create item \"%s\": %s (%d)\n", + DEBUG_FUNCTION_LINE_ERR ("Failed to get or create item \"%s\": %s (%d)\n", FTPIIU_ENABLED_STRING, WUPSStorageAPI_GetStatusStr (err), err); @@ -317,7 +321,7 @@ INITIALIZE_PLUGIN () sSystemFilesAllowed, DEFAULT_SYSTEM_FILES_ALLOWED_VALUE)) != WUPS_STORAGE_ERROR_SUCCESS) { - OSReport ("ftpiiu plugin: Failed to get or create item \"%s\": %s (%d)\n", + DEBUG_FUNCTION_LINE_ERR ("Failed to get or create item \"%s\": %s (%d)\n", SYSTEM_FILES_ALLOWED_STRING, WUPSStorageAPI_GetStatusStr (err), err); @@ -325,9 +329,8 @@ INITIALIZE_PLUGIN () if ((err = WUPSStorageAPI::SaveStorage ()) != WUPS_STORAGE_ERROR_SUCCESS) { - OSReport ("ftpiiu plugin: Failed to save storage: %s (%d)\n", - WUPSStorageAPI_GetStatusStr (err), - err); + DEBUG_FUNCTION_LINE_ERR ( + "Failed to save storage: %s (%d)\n", WUPSStorageAPI_GetStatusStr (err), err); } } @@ -343,6 +346,7 @@ void wiiu_init () ON_APPLICATION_START () { + initLogging (); nn::ac::Initialize (); nn::ac::ConnectAsync (); @@ -352,6 +356,7 @@ ON_APPLICATION_START () ON_APPLICATION_ENDS () { stop_server (); + deinitLogging (); } bool platform::init ()