From 2f4d549d1901d3bb32815b28e03c20cbb51ff7cf Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 27 Jun 2020 12:18:26 +0200 Subject: [PATCH] Use WHB for logging, minor formatting and cleanup --- src/main.cpp | 12 ++---- src/utils/TcpReceiver.cpp | 46 +++++++++------------- src/utils/logger.c | 82 --------------------------------------- src/utils/logger.h | 29 ++++---------- src/utils/utils.c | 66 ++++--------------------------- src/utils/utils.h | 12 +++--- 6 files changed, 43 insertions(+), 204 deletions(-) delete mode 100644 src/utils/logger.c diff --git a/src/main.cpp b/src/main.cpp index baf98f5..9ee1495 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include #include "utils/TcpReceiver.h" -#include +#include WUPS_PLUGIN_NAME("Wiiload"); WUPS_PLUGIN_DESCRIPTION("Wiiload Server"); @@ -14,10 +14,8 @@ TcpReceiver *thread = NULL; /* Entry point */ ON_APPLICATION_START(args) { - WHBInitializeSocketLibrary(); - - log_init(); - DEBUG_FUNCTION_LINE("Started wiiload thread\n"); + WHBLogUdpInit(); + DEBUG_FUNCTION_LINE("Started wiiload thread"); thread = new TcpReceiver(4299); } @@ -30,9 +28,7 @@ void stopThread() { ON_APPLICATION_END() { - DEBUG_FUNCTION_LINE("Kill thread\n"); + DEBUG_FUNCTION_LINE("Kill thread"); stopThread(); - - DEBUG_FUNCTION_LINE("Unmount SD\n"); } diff --git a/src/utils/TcpReceiver.cpp b/src/utils/TcpReceiver.cpp index cb82104..aa4fc35 100644 --- a/src/utils/TcpReceiver.cpp +++ b/src/utils/TcpReceiver.cpp @@ -42,10 +42,9 @@ TcpReceiver::~TcpReceiver() { void TcpReceiver::executeThread() { serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); - if (serverSocket < 0) + if (serverSocket < 0) { return; - - DEBUG_FUNCTION_LINE("\n"); + } uint32_t enable = 1; setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); @@ -56,8 +55,6 @@ void TcpReceiver::executeThread() { bindAddress.sin_port = serverPort; bindAddress.sin_addr.s_addr = INADDR_ANY; - DEBUG_FUNCTION_LINE("\n"); - socklen_t len; int32_t ret; if ((ret = bind(serverSocket, (struct sockaddr *) &bindAddress, 16)) < 0) { @@ -65,27 +62,21 @@ void TcpReceiver::executeThread() { return; } - DEBUG_FUNCTION_LINE("\n"); + DEBUG_FUNCTION_LINE(""); if ((ret = listen(serverSocket, 1)) < 0) { socketclose(serverSocket); return; } - DEBUG_FUNCTION_LINE("\n"); - struct sockaddr_in clientAddr; memset(&clientAddr, 0, sizeof(clientAddr)); int32_t addrlen = sizeof(struct sockaddr); while (!exitRequested) { - - DEBUG_FUNCTION_LINE("\n"); len = 16; int32_t clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &len); if (clientSocket >= 0) { - - DEBUG_FUNCTION_LINE("\n"); uint32_t ipAddress = clientAddr.sin_addr.s_addr; //serverReceiveStart(this, ipAddress); int32_t result = loadToMemory(clientSocket, ipAddress); @@ -95,7 +86,7 @@ void TcpReceiver::executeThread() { if (result > 0) break; } else { - DEBUG_FUNCTION_LINE("Server socket accept failed %i %d\n", clientSocket, wiiu_geterrno()); + DEBUG_FUNCTION_LINE("Server socket accept failed %i %d", clientSocket, wiiu_geterrno()); OSSleepTicks(OSMicrosecondsToTicks(100000)); } } @@ -112,7 +103,7 @@ typedef struct __attribute((packed)) { } LOAD_REQUEST; int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { - DEBUG_FUNCTION_LINE("Loading file from ip %08X\n", ipAddress); + DEBUG_FUNCTION_LINE("Loading file from ip %08X", ipAddress); uint32_t fileSize = 0; uint32_t fileSizeUnc = 0; @@ -130,7 +121,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { uint32_t bytesRead = 0; in.s_addr = ipAddress; - DEBUG_FUNCTION_LINE("transfer start\n"); + DEBUG_FUNCTION_LINE("transfer start"); unsigned char *loadAddress = (unsigned char *) memalign(0x40, fileSize); if (!loadAddress) { @@ -147,7 +138,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { int32_t ret = recv(clientSocket, loadAddress + bytesRead, blockSize, 0); if (ret <= 0) { - DEBUG_FUNCTION_LINE("Failure on reading file\n"); + DEBUG_FUNCTION_LINE("Failure on reading file"); break; } @@ -156,7 +147,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { if (bytesRead != fileSize) { free(loadAddress); - DEBUG_FUNCTION_LINE("File loading not finished, %i of %i bytes received\n", bytesRead, fileSize); + DEBUG_FUNCTION_LINE("File loading not finished, %i of %i bytes received", bytesRead, fileSize); return FILE_READ_ERROR; } @@ -224,7 +215,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { uLongf f = fileSizeUnc; int32_t result = uncompress((Bytef *) &inflatedData[0], &f, (Bytef *) loadAddress, fileSize); if (result != Z_OK) { - DEBUG_FUNCTION_LINE("uncompress failed %i\n", result); + DEBUG_FUNCTION_LINE("uncompress failed %i", result); return FILE_READ_ERROR; } @@ -234,13 +225,12 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { } if (inflatedData[0x7] == 0xCA && inflatedData[0x8] == 0xFE && inflatedData[0x9] != 0xDE && inflatedData[0xA] != 0xAD) { - DEBUG_FUNCTION_LINE("Try to load a rpx\n"); + DEBUG_FUNCTION_LINE("Try to load a rpx"); FSUtils::CreateSubfolder(RPX_TEMP_PATH); res = FSUtils::saveBufferToFile(RPX_TEMP_FILE, inflatedData, fileSize); free(inflatedData); loadedRPX = true; } else if (inflatedData[0x7] == 0xCA && inflatedData[0x8] == 0xFE && inflatedData[0x9] == 0xDE && inflatedData[0xA] == 0xAD) { - auto newContainer = PluginUtils::getPluginForBuffer((char *) inflatedData, fileSize); if (newContainer) { auto oldPlugins = PluginUtils::getLoadedPlugins(8); @@ -260,14 +250,14 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { } for (auto &plugin : finalList) { - DEBUG_FUNCTION_LINE("name: %s\n", plugin.getMetaInformation().getName().c_str()); - DEBUG_FUNCTION_LINE("author: %s\n", plugin.getMetaInformation().getAuthor().c_str()); - DEBUG_FUNCTION_LINE("handle: %08X\n", plugin.getPluginData().getHandle()); - DEBUG_FUNCTION_LINE("====\n"); + DEBUG_FUNCTION_LINE("name: %s", plugin.getMetaInformation().getName().c_str()); + DEBUG_FUNCTION_LINE("author: %s", plugin.getMetaInformation().getAuthor().c_str()); + DEBUG_FUNCTION_LINE("handle: %08X", plugin.getPluginData().getHandle()); + DEBUG_FUNCTION_LINE("===="); } if (PluginUtils::LoadAndLinkOnRestart(finalList) != 0) { - DEBUG_FUNCTION_LINE("Failed to load& link\n"); + DEBUG_FUNCTION_LINE("Failed to load& link"); PluginUtils::destroyPluginContainer(finalList); } else { PluginUtils::destroyPluginContainer(finalList); @@ -279,14 +269,14 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { return fileSize; } else { - DEBUG_FUNCTION_LINE("Failed to parse plugin\n"); + DEBUG_FUNCTION_LINE("Failed to parse plugin"); } free(inflatedData); } } else { if (loadAddress[0x7] == 0xCA && loadAddress[0x8] == 0xFE) { - DEBUG_FUNCTION_LINE("Try to load a rpx\n"); + DEBUG_FUNCTION_LINE("Try to load a rpx"); FSUtils::CreateSubfolder(RPX_TEMP_PATH); res = FSUtils::saveBufferToFile(RPX_TEMP_FILE, loadAddress, fileSize); free(loadAddress); @@ -305,7 +295,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { LOAD_REQUEST request; memset(&request, 0, sizeof(request)); - log_printf("Loading file %s\n", RPX_TEMP_FILE_EX); + DEBUG_FUNCTION_LINE("Loading file %s", RPX_TEMP_FILE_EX); request.command = 0xFC; // IPC_CUSTOM_LOAD_CUSTOM_RPX; request.target = 0; // LOAD_FILE_TARGET_SD_CARD request.filesize = 0; // unknown diff --git a/src/utils/logger.c b/src/utils/logger.c deleted file mode 100644 index 5413a70..0000000 --- a/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/src/utils/logger.h b/src/utils/logger.h index b6f1040..95f28f9 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -1,20 +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__) @@ -23,18 +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 diff --git a/src/utils/utils.c b/src/utils/utils.c index 8bc2982..f96117a 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -1,10 +1,7 @@ #include -#include -#include #include -#include -#include -#include +#include +#include "utils/logger.h" // https://gist.github.com/ccbrown/9722406 void dumpHex(const void *data, size_t size) { @@ -13,76 +10,29 @@ void dumpHex(const void *data, size_t size) { ascii[16] = '\0'; DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); for (i = 0; i < size; ++i) { - log_printf("%02X ", ((unsigned char *) data)[i]); + WHBLogWritef("%02X ", ((unsigned char *) data)[i]); if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') { ascii[i % 16] = ((unsigned char *) data)[i]; } else { ascii[i % 16] = '.'; } if ((i + 1) % 8 == 0 || i + 1 == size) { - log_printf(" "); + WHBLogWritef(" "); if ((i + 1) % 16 == 0) { - log_printf("| %s \n", ascii); + WHBLogPrintf("| %s ", ascii); if (i + 1 < size) { DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1); } } else if (i + 1 == size) { ascii[(i + 1) % 16] = '\0'; if ((i + 1) % 16 <= 8) { - log_printf(" "); + WHBLogWritef(" "); } for (j = (i + 1) % 16; j < 16; ++j) { - log_printf(" "); + WHBLogWritef(" "); } - log_printf("| %s \n", ascii); + WHBLogPrintf("| %s ", ascii); } } } } - -// You must free the result if result is non-NULL. -char *str_replace(char *orig, char *rep, char *with) { - char *result; // the return string - char *ins; // the next insert point - char *tmp; // varies - int len_rep; // length of rep (the string to remove) - int len_with; // length of with (the string to replace rep with) - int len_front; // distance between rep and end of last rep - int count; // number of replacements - - // sanity checks and initialization - if (!orig || !rep) - return NULL; - len_rep = strlen(rep); - if (len_rep == 0) - return NULL; // empty rep causes infinite loop during count - if (!with) - with = ""; - len_with = strlen(with); - - // count the number of replacements needed - ins = orig; - for (count = 0; tmp = strstr(ins, rep); ++count) { - ins = tmp + len_rep; - } - - tmp = result = (char *) malloc(strlen(orig) + (len_with - len_rep) * count + 1); - - if (!result) - return NULL; - - // first time through the loop, all the variable are set correctly - // from here on, - // tmp points to the end of the result string - // ins points to the next occurrence of rep in orig - // orig points to the remainder of orig after "end of rep" - while (count--) { - ins = strstr(orig, rep); - len_front = ins - orig; - tmp = strncpy(tmp, orig, len_front) + len_front; - tmp = strcpy(tmp, with) + len_with; - orig += len_front + len_rep; // move to next "end of rep" - } - strcpy(tmp, orig); - return result; -} diff --git a/src/utils/utils.h b/src/utils/utils.h index c53bebb..4097970 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -1,5 +1,4 @@ -#ifndef __UTILS_H_ -#define __UTILS_H_ +#pragma once #include @@ -21,8 +20,10 @@ extern "C" { #define ALIGN4(x) (((x) + 3) & ~3) #define ALIGN32(x) (((x) + 31) & ~31) +// those work only in powers of 2 +#define ROUNDDOWN(val, align) ((val) & ~(align-1)) +#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align-1)), align) -#define ROUNDUP(x, align) (((x) + ((align) - 1)) & ~((align) - 1)) #define le16(i) ((((uint16_t) ((i) & 0xFF)) << 8) | ((uint16_t) (((i) & 0xFF00) >> 8))) #define le32(i) ((((uint32_t)le16((i) & 0xFFFF)) << 16) | ((uint32_t)le16(((i) & 0xFFFF0000) >> 16))) @@ -30,10 +31,7 @@ extern "C" { //Needs to have log_init() called beforehand. void dumpHex(const void *data, size_t size); -char *str_replace(char *orig, char *rep, char *with); #ifdef __cplusplus } -#endif - -#endif // __UTILS_H_ +#endif \ No newline at end of file