From fcba5a9f1c221a4af62dd96a80e5f62affaed514 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 26 Jan 2022 13:24:56 +0100 Subject: [PATCH] Implement logging, but only when built with `make DEBUG=1` --- Makefile | 9 +++++++-- source/logger.c | 36 ++++++++++++++++++++++++++++++++++++ source/logger.h | 31 ++++++++++++++++++++++--------- source/main.cpp | 15 +++++++++++---- source/memory_mapping.cpp | 2 +- 5 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 source/logger.c diff --git a/Makefile b/Makefile index da3cd0c..73f5f5e 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ INCLUDES := source #------------------------------------------------------------------------------- # options for code generation #------------------------------------------------------------------------------- -CFLAGS := -g -Wall -O2 -ffunction-sections\ +CFLAGS := -Wall -O2 -ffunction-sections\ $(MACHDEP) CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ @@ -36,7 +36,12 @@ CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ CXXFLAGS := $(CFLAGS) -std=c++17 -fno-exceptions -fno-rtti ASFLAGS := -g $(ARCH) -LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libkernel.ld -T$(WUMS_ROOT)/share/libfunctionpatcher.ld $(WUMSSPECS) +LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libkernel.ld -T$(WUMS_ROOT)/share/libfunctionpatcher.ld $(WUMSSPECS) + +ifeq ($(DEBUG),1) +CXXFLAGS += -DDEBUG -g +CCFLAGS += -DDEBUG -g +endif LIBS := -lwums -lwut -lkernel -lfunctionpatcher diff --git a/source/logger.c b/source/logger.c new file mode 100644 index 0000000..0411db7 --- /dev/null +++ b/source/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/source/logger.h b/source/logger.h index 9da61f6..6ff5ae2 100644 --- a/source/logger.h +++ b/source/logger.h @@ -1,20 +1,18 @@ #pragma once +#include +#include + #ifdef __cplusplus extern "C" { #endif -#include -#include +#ifdef DEBUG #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) -#define OSFATAL_FUNCTION_LINE(FMT, ARGS...)do { \ - OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0) - -#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while(0) +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ @@ -22,8 +20,23 @@ extern "C" { #define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0); + } while (0) + +#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) + +#endif + +void initLogging(); + +void deinitLogging(); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif + diff --git a/source/main.cpp b/source/main.cpp index 50c0f64..336fcb1 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,11 +1,8 @@ #include -#include -#include -#include #include "memory_mapping.h" #include -#include "logger.h" #include "function_replacements.h" +#include "logger.h" WUMS_MODULE_EXPORT_NAME("homebrew_memorymapping"); WUMS_MODULE_SKIP_INIT_FINI(); @@ -23,6 +20,16 @@ WUMS_INITIALIZE(args) { FunctionPatcherPatchFunction(function_replacements, function_replacements_size); } +#ifdef DEBUG +WUMS_APPLICATION_STARTS() { + initLogging(); +} + +WUMS_APPLICATION_REQUESTS_EXIT() { + deinitLogging(); +} +#endif + void MemoryMappingFree(void *ptr) { //DEBUG_FUNCTION_LINE("[%08X] free", ptr); MemoryMapping_free(ptr); diff --git a/source/memory_mapping.cpp b/source/memory_mapping.cpp index 7d63486..c90fda4 100644 --- a/source/memory_mapping.cpp +++ b/source/memory_mapping.cpp @@ -61,7 +61,7 @@ void readAndPrintSegmentRegister(CThread *thread, void *arg) { DCFlushRange(&srTable, sizeof(srTable)); for (int32_t i = 0; i < 16; i++) { - DEBUG_FUNCTION_LINE_VERBOSE("[%d] SR[%d]=%08X", core, i, srTable.value[i]); + DEBUG_FUNCTION_LINE_VERBOSE("[%d] SR[%d]=%08X", OSGetThreadAffinity(OSGetCurrentThread()) / 2, i, srTable.value[i]); } uint32_t pageTable[0x8000];