diff --git a/src/main.cpp b/src/main.cpp index 762594d..2a3fe12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include #include #include "readFileWrapper.h" +#include #include "romfs_helper.h" #include "filelist.h" @@ -51,12 +52,11 @@ INITIALIZE_PLUGIN() { } ON_APPLICATION_START(args) { - socket_lib_init(); - log_init(); - DEBUG_FUNCTION_LINE("IN PLUGIN\n"); + WHBLogUdpInit(); + DEBUG_FUNCTION_LINE("IN PLUGIN"); if (_SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY) != OSGetTitleID()) { - DEBUG_FUNCTION_LINE("gHomebrewLaunched to FALSE\n"); + DEBUG_FUNCTION_LINE("gHomebrewLaunched to FALSE"); gHomebrewLaunched = FALSE; } } @@ -69,7 +69,7 @@ ON_APPLICATION_END() { void fillXmlForTitleID(uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml *out_buf) { int32_t id = getIDByLowerTitleID(titleid_lower); if (id < 0) { - DEBUG_FUNCTION_LINE("Failed to get id by titleid\n"); + DEBUG_FUNCTION_LINE("Failed to get id by titleid"); return; } if (id >= FILE_INFO_SIZE) { @@ -121,7 +121,7 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount, int j = 0; for (int i = 0; i < dirList.GetFilecount(); i++) { if (j >= FILE_INFO_SIZE) { - DEBUG_FUNCTION_LINE("TOO MANY TITLES\n"); + DEBUG_FUNCTION_LINE("TOO MANY TITLES"); break; } //! skip our own application in the listing @@ -173,7 +173,7 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount, template_title.sdkVersion = __OSGetProcessSDKVersion(); template_title.unk0x60 = 0; - DEBUG_FUNCTION_LINE("[%d] %s [%016llX]\n", j, gFileInfos[j].path, template_title.titleId); + DEBUG_FUNCTION_LINE("[%d] %s [%016llX]", j, gFileInfos[j].path, template_title.titleId); memcpy(&(titleList[titlecount]), &template_title, sizeof(template_title)); @@ -216,7 +216,7 @@ DECL_FUNCTION(int32_t, ACPCheckTitleLaunchByTitleListTypeEx, MCPTitleListType *t if ((title->titleId & TITLE_ID_HOMEBREW_MASK) == TITLE_ID_HOMEBREW_MASK) { int32_t id = getIDByLowerTitleID(title->titleId & 0xFFFFFFFF); if (id >= 0) { - DEBUG_FUNCTION_LINE("Started homebrew\n"); + DEBUG_FUNCTION_LINE("Started homebrew"); gHomebrewLaunched = TRUE; fillXmlForTitleID((title->titleId & 0xFFFFFFFF00000000) >> 32, (title->titleId & 0xFFFFFFFF), &gLaunchXML); @@ -239,7 +239,7 @@ DECL_FUNCTION(int32_t, ACPCheckTitleLaunchByTitleListTypeEx, MCPTitleListType *t strncpy(request.path, gFileInfos[id].path, 255); - DEBUG_FUNCTION_LINE("Loading file %s size: %08X offset: %08X\n", request.path, request.filesize, request.fileoffset); + DEBUG_FUNCTION_LINE("Loading file %s size: %08X offset: %08X", request.path, request.filesize, request.fileoffset); DCFlushRange(&request, sizeof(LOAD_REQUEST)); @@ -282,7 +282,7 @@ DECL_FUNCTION(int, FSOpenFile, FSClient *client, FSCmdBlock *block, char *path, sscanf(id, "%08X", &lowerTitleID); int32_t idVal = getIDByLowerTitleID(lowerTitleID); if (idVal < 0) { - DEBUG_FUNCTION_LINE("Failed to find id for titleID %08X\n", lowerTitleID); + DEBUG_FUNCTION_LINE("Failed to find id for titleID %08X", lowerTitleID); } else { if (FSOpenFile_for_ID(idVal, ending, handle) < 0) { return res; @@ -290,14 +290,12 @@ DECL_FUNCTION(int, FSOpenFile, FSClient *client, FSCmdBlock *block, char *path, } return FS_STATUS_OK; } else if (gHomebrewLaunched) { - socket_lib_init(); - log_init(); if (StringTools::EndsWith(path, iconTex)) { *handle = 0x13371337; - DEBUG_FUNCTION_LINE("yooo let's do it\n"); + DEBUG_FUNCTION_LINE("yooo let's do it"); return FS_STATUS_OK; } else { - DEBUG_FUNCTION_LINE("%s\n", path); + DEBUG_FUNCTION_LINE("%s", path); } } @@ -314,11 +312,11 @@ DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFile if ((handle & 0xFF000000) == 0xFF000000) { int32_t fd = (handle & 0x00000FFF); int32_t romid = (handle & 0x00FFF000) >> 12; - DEBUG_FUNCTION_LINE("Close %d %d\n", fd, romid); + DEBUG_FUNCTION_LINE("Close %d %d", fd, romid); DeInitFile(fd); if (gFileInfos[romid].openedFiles--) { if (gFileInfos[romid].openedFiles <= 0) { - DEBUG_FUNCTION_LINE("unmount romfs no more handles\n"); + DEBUG_FUNCTION_LINE("unmount romfs no more handles"); unmountRomfs(romid); } } @@ -335,7 +333,7 @@ DECL_FUNCTION(FSStatus, FSReadFile, FSClient *client, FSCmdBlock *block, uint8_t cpySize = sizeof(gIconCache); } memcpy(buffer, gIconCache, cpySize); - DEBUG_FUNCTION_LINE("DUMMY\n"); + DEBUG_FUNCTION_LINE("DUMMY"); return (FSStatus) (cpySize / size); } else if (handle == 0x13371338) { uint32_t cpySize = size * count; @@ -343,14 +341,14 @@ DECL_FUNCTION(FSStatus, FSReadFile, FSClient *client, FSCmdBlock *block, uint8_t cpySize = iconTex_tga_size; } memcpy(buffer, iconTex_tga, cpySize); - DEBUG_FUNCTION_LINE("DUMMY\n"); + DEBUG_FUNCTION_LINE("DUMMY"); return (FSStatus) (cpySize / size); } if ((handle & 0xFF000000) == 0xFF000000) { int32_t fd = (handle & 0x00000FFF); int32_t romid = (handle & 0x00FFF000) >> 12; - DEBUG_FUNCTION_LINE("READ %d from %d rom: %d\n", size * count, fd, romid); + DEBUG_FUNCTION_LINE("READ %d from %d rom: %d", size * count, fd, romid); int readSize = readFile(fd, buffer, (size * count)); 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..ceb3111 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -6,15 +6,7 @@ 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,15 +15,13 @@ 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 } diff --git a/src/utils/utils.c b/src/utils/utils.c index ef03e74..fe3055d 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -1,10 +1,8 @@ #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,28 +11,28 @@ 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); } } }