From f13234902146c9051806083879683854edea03a8 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 16 Dec 2020 15:10:17 +0100 Subject: [PATCH] Update the example plugin --- plugins/example_plugin/README.md | 1 - plugins/example_plugin/src/main.cpp | 17 +++-- plugins/example_plugin/src/utils/logger.c | 82 ----------------------- plugins/example_plugin/src/utils/logger.h | 25 +++---- 4 files changed, 16 insertions(+), 109 deletions(-) delete mode 100644 plugins/example_plugin/src/utils/logger.c diff --git a/plugins/example_plugin/README.md b/plugins/example_plugin/README.md index 8c8d81f..df01f44 100644 --- a/plugins/example_plugin/README.md +++ b/plugins/example_plugin/README.md @@ -7,6 +7,5 @@ This is just a simple example plugin which can be used as a template. For building you need: - [wups](https://github.com/Maschell/WiiUPluginSystem) - [wut](https://github.com/decaf-emu/wut) -- [libutils](https://github.com/Maschell/libutils/tree/wut) for common functions (WUT version). Install them (in this order) according to their README's. Don't forget the dependencies of the libs itself. \ No newline at end of file diff --git a/plugins/example_plugin/src/main.cpp b/plugins/example_plugin/src/main.cpp index 25c8234..1c82662 100644 --- a/plugins/example_plugin/src/main.cpp +++ b/plugins/example_plugin/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include /** Mandatory plugin information. @@ -42,17 +43,16 @@ WUPS_USE_WUT_CRT() // Use the wut malloc wrapper Get's called ONCE when the loader exits, but BEFORE the ON_APPLICATION_START gets called or functions are overridden. **/ INITIALIZE_PLUGIN(){ - WHBInitializeSocketLibrary(); - log_init(); - DEBUG_FUNCTION_LINE("INITIALIZE_PLUGIN of example_plugin!\n"); + WHBLogUdpInit(); + DEBUG_FUNCTION_LINE("INITIALIZE_PLUGIN of example_plugin!"); } /** Gets called when the plugin loader is re-entered => when the plugin is unloaded. The overridden functions are restored before this is getting called. **/ -DEINITIALIZE_PLUGIN(){ - DEBUG_FUNCTION_LINE("DEINITIALIZE_PLUGIN of example_plugin!\n"); +DEINITIALIZE_PLUGIN(){ + DEBUG_FUNCTION_LINE("DEINITIALIZE_PLUGIN of example_plugin!"); } /** @@ -61,17 +61,16 @@ DEINITIALIZE_PLUGIN(){ Make sure to initialize all functions you're using in the overridden functions! **/ ON_APPLICATION_START(){ - WHBInitializeSocketLibrary(); - log_init(); + WHBLogUdpInit(); - DEBUG_FUNCTION_LINE("ON_APPLICATION_START of example_plugin!\n"); + DEBUG_FUNCTION_LINE("ON_APPLICATION_START of example_plugin!"); } /** Gets called when an application ends. A good place for freeing memory. **/ ON_APPLICATION_END(){ - DEBUG_FUNCTION_LINE("ON_APPLICATION_ENDING of example_plugin!\n"); + DEBUG_FUNCTION_LINE("ON_APPLICATION_ENDING of example_plugin!"); } /** diff --git a/plugins/example_plugin/src/utils/logger.c b/plugins/example_plugin/src/utils/logger.c deleted file mode 100644 index 0922230..0000000 --- a/plugins/example_plugin/src/utils/logger.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -static int log_socket __attribute__((section(".data")))= -1; -static struct sockaddr_in connect_addr __attribute__((section(".data"))); -static volatile int log_lock __attribute__((section(".data"))) = 0; - -void log_init_() { - int broadcastEnable = 1; - log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (log_socket < 0) - return; - - setsockopt(log_socket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); - - memset(&connect_addr, 0, sizeof(struct sockaddr_in)); - connect_addr.sin_family = AF_INET; - connect_addr.sin_port = 4405; - connect_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); -} - -void log_print_(const char *str) { - // socket is always 0 initially as it is in the BSS - if(log_socket < 0) { - return; - } - - while(log_lock) - OSSleepTicks(OSMicrosecondsToTicks(1000)); - log_lock = 1; - - int len = strlen(str); - int ret; - while (len > 0) { - int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet - ret = sendto(log_socket, str, block, 0, (struct sockaddr *)&connect_addr, sizeof(struct sockaddr_in)); - if(ret < 0) - break; - - len -= ret; - str += ret; - } - - log_lock = 0; -} - -void OSFatal_printf(const char *format, ...) { - char tmp[512]; - tmp[0] = 0; - va_list va; - va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { - OSFatal(tmp); - } - va_end(va); -} - -void log_printf_(const char *format, ...) { - if(log_socket < 0) { - return; - } - - char tmp[512]; - tmp[0] = 0; - - va_list va; - va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { - log_print_(tmp); - } - va_end(va); -} - diff --git a/plugins/example_plugin/src/utils/logger.h b/plugins/example_plugin/src/utils/logger.h index 6e753b8..95f28f9 100644 --- a/plugins/example_plugin/src/utils/logger.h +++ b/plugins/example_plugin/src/utils/logger.h @@ -1,17 +1,11 @@ -#ifndef __LOGGER_H_ -#define __LOGGER_H_ +#pragma once #ifdef __cplusplus extern "C" { #endif #include - -void log_init_(); -//void log_deinit_(void); -void log_print_(const char *str); -void log_printf_(const char *format, ...); -void OSFatal_printf(const char *format, ...); +#include #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) @@ -20,17 +14,14 @@ void OSFatal_printf(const char *format, ...); OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ } while (0) -#define log_init() log_init_() -//#define log_deinit() log_deinit_() -#define log_print(str) log_print_(str) -#define log_printf(FMT, ARGS...) log_printf_(FMT, ## ARGS); - #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ - log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0) + WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ + } while (0); + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ + WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ + } while (0); #ifdef __cplusplus } #endif - -#endif