From 20c7dbf2fbd9eb57694c19be99a744b87fb0bcca Mon Sep 17 00:00:00 2001 From: CreeperMario Date: Sun, 20 Aug 2017 18:17:07 +0930 Subject: [PATCH] whb: Add ability to remove logging functions * WHBRemoveLogHandler will look through the "registry" of logging functions, and will set the entry to NULL if it matches what the user passes. * WHBLogUdpDeinit will correctly close the socket and shut down the socket library in addition to removing its entry in the registry. --- samples/gx2/triangle/src/main.c | 1 + src/libwhb/include/whb/log.h | 3 +++ src/libwhb/include/whb/log_cafe.h | 3 +++ src/libwhb/include/whb/log_udp.h | 3 +++ src/libwhb/src/log.c | 15 +++++++++++++++ src/libwhb/src/log_cafe.c | 9 +++++++-- src/libwhb/src/log_udp.c | 14 ++++++++++++-- 7 files changed, 44 insertions(+), 4 deletions(-) diff --git a/samples/gx2/triangle/src/main.c b/samples/gx2/triangle/src/main.c index 02deaee7..e0bd4310 100644 --- a/samples/gx2/triangle/src/main.c +++ b/samples/gx2/triangle/src/main.c @@ -116,5 +116,6 @@ exit: WHBUnmountSdCard(); WHBGfxShutdown(); WHBProcShutdown(); + WHBLogUdpDeinit(); return result; } diff --git a/src/libwhb/include/whb/log.h b/src/libwhb/include/whb/log.h index df3c6c2d..f2c1964d 100644 --- a/src/libwhb/include/whb/log.h +++ b/src/libwhb/include/whb/log.h @@ -16,6 +16,9 @@ typedef void (*LogHandlerFn)(const char *msg); BOOL WHBAddLogHandler(LogHandlerFn fn); +BOOL +WHBRemoveLogHandler(LogHandlerFn fn); + BOOL WHBLogWrite(const char *str); diff --git a/src/libwhb/include/whb/log_cafe.h b/src/libwhb/include/whb/log_cafe.h index bc405155..023b5fe8 100644 --- a/src/libwhb/include/whb/log_cafe.h +++ b/src/libwhb/include/whb/log_cafe.h @@ -14,6 +14,9 @@ extern "C" { BOOL WHBLogCafeInit(); +BOOL +WHBLogCafeDeinit(); + #ifdef __cplusplus } #endif diff --git a/src/libwhb/include/whb/log_udp.h b/src/libwhb/include/whb/log_udp.h index 28ba5d75..00900c66 100644 --- a/src/libwhb/include/whb/log_udp.h +++ b/src/libwhb/include/whb/log_udp.h @@ -14,6 +14,9 @@ extern "C" { BOOL WHBLogUdpInit(); +BOOL +WHBLogUdpDeinit(); + #ifdef __cplusplus } #endif diff --git a/src/libwhb/src/log.c b/src/libwhb/src/log.c index 2ee80208..9972bc92 100644 --- a/src/libwhb/src/log.c +++ b/src/libwhb/src/log.c @@ -25,6 +25,21 @@ WHBAddLogHandler(LogHandlerFn fn) return FALSE; } +BOOL +WHBRemoveLogHandler(LogHandlerFn fn) +{ + int i; + + for(i = 0; i < MAX_HANDLERS; ++i) { + if(sHandlers[i] == fn) { + sHandlers[i] = NULL; + return TRUE; + } + } + + return FALSE; +} + BOOL WHBLogWrite(const char *str) { diff --git a/src/libwhb/src/log_cafe.c b/src/libwhb/src/log_cafe.c index e3bea7ac..34d067e5 100644 --- a/src/libwhb/src/log_cafe.c +++ b/src/libwhb/src/log_cafe.c @@ -19,6 +19,11 @@ cafeLogHandler(const char * msg) BOOL WHBLogCafeInit() { - WHBAddLogHandler(cafeLogHandler); - return TRUE; + return WHBAddLogHandler(cafeLogHandler); +} + +BOOL +WHBLogCafeDeinit() +{ + return WHBRemoveLogHandler(cafeLogHandler); } diff --git a/src/libwhb/src/log_udp.c b/src/libwhb/src/log_udp.c index 713d4cc2..ba405b18 100644 --- a/src/libwhb/src/log_udp.c +++ b/src/libwhb/src/log_udp.c @@ -42,6 +42,16 @@ WHBLogUdpInit() sSendAddr.sin_port = htons(SERVER_PORT); sSendAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); - WHBAddLogHandler(udpLogHandler); - return TRUE; + return WHBAddLogHandler(udpLogHandler); +} + +BOOL +WHBLogUdpDeinit() +{ + if(shutdown(sSocket, SHUT_WR) != 0) { + return FALSE; + } + + socket_lib_finish(); + return WHBRemoveLogHandler(udpLogHandler); }