From c286c947013717a259348994d96035330379483b Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 3 Sep 2022 23:29:22 +0200 Subject: [PATCH] Improve logging --- source/logger.h | 23 +++++++++++++++++++++++ source/utils.cpp | 13 +++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 source/logger.h diff --git a/source/logger.h b/source/logger.h new file mode 100644 index 0000000..ebe3656 --- /dev/null +++ b/source/logger.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +#define __FILENAME__ ({ \ + const char *__filename = __FILE__; \ + const char *__pos = strrchr(__filename, '/'); \ + if (!__pos) __pos = strrchr(__filename, '\\'); \ + __pos ? __pos + 1 : __filename; \ +}) + +#define LOG_APP_TYPE "L" +#define LOG_APP_NAME "libsdutils" + +#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) + +#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 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, "##WARNING## ", "\n", FMT, ##ARGS) diff --git a/source/utils.cpp b/source/utils.cpp index fa08db0..3163b04 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,3 +1,4 @@ +#include "logger.h" #include "sdutils/sdutils.h" #include #include @@ -17,12 +18,12 @@ static SDUtilsVersion sSDUtilsVersion = SD_UTILS_MODULE_VERSION_ERROR; SDUtilsStatus SDUtils_InitLibrary() { if (OSDynLoad_Acquire("homebrew_sdhotswap", &sModuleHandle) != OS_DYNLOAD_OK) { - OSReport("SDUtils_Init: OSDynLoad_Acquire failed.\n"); + DEBUG_FUNCTION_LINE_ERR("SDUtils_Init: OSDynLoad_Acquire failed."); return SDUTILS_RESULT_MODULE_NOT_FOUND; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsGetVersion", (void **) &sSDUtilsGetVersion) != OS_DYNLOAD_OK) { - OSReport("SDUtils_Init: SDUtilsGetVersion failed.\n"); + DEBUG_FUNCTION_LINE_ERR("SDUtils_Init: SDUtilsGetVersion failed."); return SDUTILS_RESULT_MODULE_MISSING_EXPORT; } @@ -32,22 +33,22 @@ SDUtilsStatus SDUtils_InitLibrary() { } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddAttachHandler", (void **) &sSDUtilsAddAttachHandler) != OS_DYNLOAD_OK) { - OSReport("SDUtils_Init: SDUtilsAddAttachHandler failed.\n"); + DEBUG_FUNCTION_LINE_ERR("SDUtils_Init: SDUtilsAddAttachHandler failed."); return SDUTILS_RESULT_MODULE_MISSING_EXPORT; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveAttachHandler", (void **) &sSDUtilsRemoveAttachHandler) != OS_DYNLOAD_OK) { - OSReport("SDUtils_Init: SDUtilsRemoveAttachHandler failed.\n"); + DEBUG_FUNCTION_LINE_ERR("SDUtils_Init: SDUtilsRemoveAttachHandler failed."); return SDUTILS_RESULT_MODULE_MISSING_EXPORT; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddCleanUpHandlesHandler", (void **) &sSDUtilsAddCleanUpHandlesHandler) != OS_DYNLOAD_OK) { - OSReport("SDUtils_Init: SDUtilsAddCleanUpHandlesHandler failed.\n"); + DEBUG_FUNCTION_LINE_ERR("SDUtils_Init: SDUtilsAddCleanUpHandlesHandler failed."); return SDUTILS_RESULT_MODULE_MISSING_EXPORT; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveCleanUpHandlesHandler", (void **) &sSDUtilsRemoveCleanUpHandlesHandler) != OS_DYNLOAD_OK) { - OSReport("SDUtils_Init: SDUtilsRemoveCleanUpHandlesHandler failed.\n"); + DEBUG_FUNCTION_LINE_ERR("SDUtils_Init: SDUtilsRemoveCleanUpHandlesHandler failed."); return SDUTILS_RESULT_MODULE_MISSING_EXPORT; }