From b5aa5083395e226d3297063fa44200b67fa6b000 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 26 Jan 2022 14:25:34 +0100 Subject: [PATCH] Do logging only when built with `make DEBUG = 1` --- Makefile | 7 ++++++- src/main.cpp | 21 +++++++++------------ src/utils/logger.c | 36 ++++++++++++++++++++++++++++++++++++ src/utils/logger.h | 34 ++++++++++++++++++++++------------ 4 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/utils/logger.c diff --git a/Makefile b/Makefile index 1be53ce..1ff1c35 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ INCLUDES := src #------------------------------------------------------------------------------- # options for code generation #------------------------------------------------------------------------------- -CFLAGS := -g -Wall -Wextra -O2 -ffunction-sections\ +CFLAGS := -Wall -Wextra -Os -ffunction-sections\ $(MACHDEP) CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ @@ -39,6 +39,11 @@ CXXFLAGS := $(CFLAGS) -std=c++17 ASFLAGS := -g $(ARCH) LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libfunctionpatcher.ld $(WUMSSPECS) +ifeq ($(DEBUG),1) +CXXFLAGS += -DDEBUG -g +CCFLAGS += -DDEBUG -g +endif + LIBS := -lwums -lwut -lfunctionpatcher -lromfs -lz #------------------------------------------------------------------------------- diff --git a/src/main.cpp b/src/main.cpp index db6e244..46c75cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,6 @@ #include #include -#include -#include -#include + #include #include #include @@ -20,14 +18,11 @@ #include WUMS_MODULE_EXPORT_NAME("homebrew_rpx_loader"); - WUMS_USE_WUT_DEVOPTAB(); + WUMS_INITIALIZE() { - if (!WHBLogModuleInit()) { - WHBLogCafeInit(); - WHBLogUdpInit(); - } + initLogging(); DEBUG_FUNCTION_LINE("Patch functions"); // we only patch static functions, we don't need re-patch them and every launch FunctionPatcherPatchFunction(fs_file_function_replacements, fs_file_function_replacements_size); @@ -35,6 +30,8 @@ WUMS_INITIALIZE() { FunctionPatcherPatchFunction(rpx_utils_function_replacements, rpx_utils_function_replacements_size); DEBUG_FUNCTION_LINE("Patch functions finished"); gReplacementInfo = {}; + + deinitLogging(); } @@ -56,6 +53,8 @@ WUMS_APPLICATION_ENDS() { free(gFSClient); } free(gFSCmd); + + deinitLogging(); } WUMS_APPLICATION_STARTS() { @@ -63,14 +62,12 @@ WUMS_APPLICATION_STARTS() { if (upid != 2 && upid != 15) { return; } + initLogging(); if (gReplacementInfo.rpxReplacementInfo.willRPXBeReplaced) { gReplacementInfo.rpxReplacementInfo.willRPXBeReplaced = false; gReplacementInfo.rpxReplacementInfo.isRPXReplaced = true; } - if (!WHBLogModuleInit()) { - WHBLogCafeInit(); - WHBLogUdpInit(); - } + if (gReplacementInfo.contentReplacementInfo.mode == CONTENTREDIRECT_FROM_PATH) { auto fsClient = (FSClient *) memalign(0x20, sizeof(FSClient)); auto fsCmd = (FSCmdBlock *) memalign(0x20, sizeof(FSCmdBlock)); diff --git a/src/utils/logger.c b/src/utils/logger.c new file mode 100644 index 0000000..0411db7 --- /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 4f19b37..6e45bec 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -1,32 +1,42 @@ #pragma once +#include +#include + #ifdef __cplusplus extern "C" { #endif -#include -#include -#include "utils.h" +#ifdef DEBUG #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) +#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); \ } while (0) -#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...) DEBUG_FUNCTION_LINE(FMT, ##ARGS) -#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) - #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 +