From 3e2aca0583bfd937e127af004a504945a7336003 Mon Sep 17 00:00:00 2001 From: Maschell Date: Mon, 17 Apr 2023 12:14:55 +0200 Subject: [PATCH] Improve logging --- Makefile | 15 ++++++++++-- src/main.cpp | 31 ++++++++++-------------- src/utils/logger.c | 36 ++++++++++++++++++++++++++++ src/utils/logger.h | 60 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 117 insertions(+), 25 deletions(-) create mode 100644 src/utils/logger.c diff --git a/Makefile b/Makefile index 53226ba..42eefd6 100644 --- a/Makefile +++ b/Makefile @@ -41,11 +41,22 @@ INCLUDES := src #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -CFLAGS := -std=c2x -g -Wall -O2 -ffunction-sections -DESPRESSO -mcpu=750 -meabi -mhard-float $(INCLUDE) -CXXFLAGS := -std=c++20 -g -Wall -O2 -ffunction-sections -DESPRESSO -mcpu=750 -meabi -mhard-float $(INCLUDE) +CFLAGS := -g -g -Wall -O2 -ffunction-sections $(MACHDEP) $(INCLUDE) -D__WIIU__ +CXXFLAGS := $(CFLAGS) -std=c++20 +CFLAGS += -std=c2x ASFLAGS := -mregnames LDFLAGS := -nostartfiles -Wl,--gc-sections,--allow-multiple-definition +ifeq ($(DEBUG),1) +CXXFLAGS += -DDEBUG -g +CFLAGS += -DDEBUG -g +endif + +ifeq ($(DEBUG),VERBOSE) +CXXFLAGS += -DDEBUG -DVERBOSE_DEBUG -g +CFLAGS += -DDEBUG -DVERBOSE_DEBUG -g +endif + #--------------------------------------------------------------------------------- Q := @ MAKEFLAGS += --no-print-directory diff --git a/src/main.cpp b/src/main.cpp index 64f9e3d..89c3df7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,27 +15,24 @@ * along with this program. If not, see . ****************************************************************************/ -#include -#include -#include -#include -#include -#include - #include "dynamic.h" #include "kernel.h" #include "utils/DirList.h" #include "utils/ElfUtils.h" #include "utils/logger.h" +#include +#include #include +#include +#include +#include +#include #include #include #include #include #include #include -#include -#include std::map get_all_payloads(const char *relativefilepath); @@ -56,10 +53,9 @@ extern "C" uint32_t start_wrapper(int argc, char **argv) { init_wut(); - WHBLogUdpInit(); - WHBLogCafeInit(); + initLogging(); - DEBUG_FUNCTION_LINE("Hello from payload.elf multiloader"); + DEBUG_FUNCTION_LINE_VERBOSE("Hello from payload.elf multiloader"); VPADReadError err; VPADStatus vpad_data; @@ -74,19 +70,18 @@ extern "C" uint32_t start_wrapper(int argc, char **argv) { std::string payload_path = "wiiu/payloads/default/payload.elf"; if ((btn & VPAD_BUTTON_B) == VPAD_BUTTON_B) { payload_path = PayloadSelectionScreen(payloads); - DEBUG_FUNCTION_LINE("Selected %s", payload_path.c_str()); + DEBUG_FUNCTION_LINE_VERBOSE("Selected %s", payload_path.c_str()); } entryPoint = load_loader_elf_from_sd(nullptr, payload_path.c_str()); if (entryPoint != 0) { - DEBUG_FUNCTION_LINE("loaded payload entrypoint at %08X", entryPoint); + DEBUG_FUNCTION_LINE("Loaded payload entrypoint at %08X", entryPoint); } else { - DEBUG_FUNCTION_LINE("failed to load elf"); + DEBUG_FUNCTION_LINE_ERR("Failed to load: %s", payload_path.c_str()); } - WHBLogUdpDeinit(); - WHBLogCafeDeinit(); + deinitLogging(); fini_wut(); @@ -112,7 +107,7 @@ extern "C" int _start(int argc, char **argv) { free(mem_ptr); leak_count++; } - OSReport("Freed %d leaked memory blocks\n", leak_count); + DEBUG_FUNCTION_LINE_INFO("Freed %d leaked memory blocks\n", leak_count); } int res = -1; diff --git a/src/utils/logger.c b/src/utils/logger.c new file mode 100644 index 0000000..f700806 --- /dev/null +++ b/src/utils/logger.c @@ -0,0 +1,36 @@ +#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/src/utils/logger.h b/src/utils/logger.h index 8b71325..d09f0e8 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -7,14 +8,63 @@ extern "C" { #endif -#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) +#define LOG_APP_TYPE "PL" +#define LOG_APP_NAME "payloadloader" -#define DEBUG_FUNCTION_LINE(FMT, ARGS...) \ - do { \ - WHBLogPrintf("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ +#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_DEFAULT(LOG_FUNC, "", "", FMT, ##ARGS) + +#define LOG_EX_DEFAULT(LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ARGS...) LOG_EX(__FILENAME__, __FUNCTION__, __LINE__, LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ##ARGS) + +#define LOG_EX(FILENAME, FUNCTION, LINE, 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) +#define DEBUG_FUNCTION_LINE_VERBOSE_EX(FILENAME, FUNCTION, LINE, FMT, ARGS...) LOG_EX(FILENAME, FUNCTION, LINE, WHBLogPrintf, "", "", FMT, ##ARGS); +#else +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) +#define DEBUG_FUNCTION_LINE_VERBOSE_EX(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_DEFAULT(WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX_DEFAULT(WHBLogPrintf, "##WARN ## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX_DEFAULT(WHBLogPrintf, "##INFO ## ", "", FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_ERR_LAMBDA(FILENAME, FUNCTION, LINE, FMT, ARGS...) LOG_EX(FILENAME, FUNCTION, LINE, WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS); + +#else + +#define DEBUG_FUNCTION_LINE_VERBOSE_EX(FMT, ARGS...) while (0) + +#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_DEFAULT(OSReport, "##ERROR## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX_DEFAULT(OSReport, "##WARN ## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX_DEFAULT(OSReport, "##INFO ## ", "\n", FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_ERR_LAMBDA(FILENAME, FUNCTION, LINE, FMT, ARGS...) LOG_EX(FILENAME, FUNCTION, LINE, OSReport, "##ERROR## ", "\n", FMT, ##ARGS); + +#endif + +void initLogging(); + +void deinitLogging(); + #ifdef __cplusplus } #endif