Use WHB for logging

This commit is contained in:
Maschell 2020-06-27 12:27:39 +02:00
parent f6c8116c7c
commit b46d5f444f
7 changed files with 57 additions and 150 deletions

View File

@ -12,17 +12,17 @@
BackgroundThread * BackgroundThread::instance = NULL; BackgroundThread * BackgroundThread::instance = NULL;
BackgroundThread::BackgroundThread(): BackgroundThreadWrapper(BackgroundThread::getPriority()) { BackgroundThread::BackgroundThread(): BackgroundThreadWrapper(BackgroundThread::getPriority()) {
DEBUG_FUNCTION_LINE("Create new Server\n"); DEBUG_FUNCTION_LINE("Create new Server");
mutex.lock(); mutex.lock();
this->serverSocket = create_server(PORT); this->serverSocket = create_server(PORT);
DCFlushRange(&(this->serverSocket), 4); DCFlushRange(&(this->serverSocket), 4);
mutex.unlock(); mutex.unlock();
DEBUG_FUNCTION_LINE("handle %d\n", this->serverSocket); DEBUG_FUNCTION_LINE("handle %d", this->serverSocket);
resumeThread(); resumeThread();
} }
BackgroundThread::~BackgroundThread() { BackgroundThread::~BackgroundThread() {
DEBUG_FUNCTION_LINE("Clean up FTP\n"); DEBUG_FUNCTION_LINE("Clean up FTP");
if(this->serverSocket != -1){ if(this->serverSocket != -1){
mutex.lock(); mutex.lock();
cleanup_ftp(); cleanup_ftp();
@ -30,7 +30,7 @@ BackgroundThread::~BackgroundThread() {
mutex.unlock(); mutex.unlock();
this->serverSocket = -1; this->serverSocket = -1;
} }
DEBUG_FUNCTION_LINE("Cleaned up FTP\n"); DEBUG_FUNCTION_LINE("Cleaned up FTP");
} }
BOOL BackgroundThread::whileLoop() { BOOL BackgroundThread::whileLoop() {
@ -39,7 +39,7 @@ BOOL BackgroundThread::whileLoop() {
network_down = process_ftp_events(this->serverSocket); network_down = process_ftp_events(this->serverSocket);
mutex.unlock(); mutex.unlock();
if(network_down) { if(network_down) {
DEBUG_FUNCTION_LINE("Network is down %d\n", this->serverSocket); DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket);
mutex.lock(); mutex.lock();
cleanup_ftp(); cleanup_ftp();
network_close(this->serverSocket); network_close(this->serverSocket);

View File

@ -25,6 +25,7 @@
#include <whb/libmanager.h> #include <whb/libmanager.h>
#include "utils/logger.h" #include "utils/logger.h"
#include "utils/utils.h" #include "utils/utils.h"
#include <whb/log_udp.h>
#include "virtualpath.h" #include "virtualpath.h"
#include "net.h" #include "net.h"
@ -59,7 +60,7 @@ ON_APPLICATION_START(args) {
ACGetAssignedAddress(&hostIpAddress); ACGetAssignedAddress(&hostIpAddress);
log_init(); WHBLogUdpInit();
//!******************************************************************* //!*******************************************************************
//! Initialize FS * //! Initialize FS *
@ -67,22 +68,22 @@ ON_APPLICATION_START(args) {
int fsaFd = -1; int fsaFd = -1;
DEBUG_FUNCTION_LINE("IOSUHAX_Open\n"); DEBUG_FUNCTION_LINE("IOSUHAX_Open");
int res = IOSUHAX_Open(NULL); int res = IOSUHAX_Open(NULL);
if(res < 0) { if(res < 0) {
DEBUG_FUNCTION_LINE("IOSUHAX_open failed\n"); DEBUG_FUNCTION_LINE("IOSUHAX_open failed");
VirtualMountDevice("fs:/"); VirtualMountDevice("fs:/");
} else { } else {
iosuhaxMount = 1; iosuhaxMount = 1;
//fatInitDefault(); //fatInitDefault();
DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open\n"); DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open");
fsaFd = IOSUHAX_FSA_Open(); fsaFd = IOSUHAX_FSA_Open();
if(fsaFd < 0) { if(fsaFd < 0) {
DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open failed\n"); DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open failed");
} }
DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open\n"); DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open");
mount_fs("slccmpt01", fsaFd, "/dev/slccmpt01", "/vol/storage_slccmpt01"); mount_fs("slccmpt01", fsaFd, "/dev/slccmpt01", "/vol/storage_slccmpt01");
mount_fs("storage_odd_tickets", fsaFd, "/dev/odd01", "/vol/storage_odd_tickets"); mount_fs("storage_odd_tickets", fsaFd, "/dev/odd01", "/vol/storage_odd_tickets");
@ -114,7 +115,7 @@ void stopThread(){
} }
ON_APPLICATION_END(){ ON_APPLICATION_END(){
DEBUG_FUNCTION_LINE("Ending ftp server\n"); DEBUG_FUNCTION_LINE("Ending ftp server");
stopThread(); stopThread();
if(iosuhaxMount) { if(iosuhaxMount) {

View File

@ -12,19 +12,19 @@ BackgroundThreadWrapper::BackgroundThreadWrapper(int32_t priority): CThread(CThr
BackgroundThreadWrapper::~BackgroundThreadWrapper() { BackgroundThreadWrapper::~BackgroundThreadWrapper() {
exitThread = 1; exitThread = 1;
DCFlushRange((void*)&exitThread, 4); DCFlushRange((void*)&exitThread, 4);
DEBUG_FUNCTION_LINE("Exit thread\n"); DEBUG_FUNCTION_LINE("Exit thread");
} }
void BackgroundThreadWrapper::executeThread() { void BackgroundThreadWrapper::executeThread() {
while (1) { while (1) {
if(exitThread) { if(exitThread) {
DEBUG_FUNCTION_LINE("We want to exit\n"); DEBUG_FUNCTION_LINE("We want to exit");
break; break;
} }
if(!whileLoop()){ if(!whileLoop()){
break; break;
} }
} }
DEBUG_FUNCTION_LINE("Exit!\n"); DEBUG_FUNCTION_LINE("Exit!");
} }

View File

@ -1,82 +0,0 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <utils/logger.h>
#include <nsysnet/socket.h>
#include <coreinit/debug.h>
#include <coreinit/systeminfo.h>
#include <coreinit/thread.h>
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);
}

View File

@ -1,17 +1,11 @@
#ifndef __LOGGER_H_ #pragma once
#define __LOGGER_H_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <string.h> #include <string.h>
#include <whb/log.h>
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, ...);
#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__)
@ -20,19 +14,14 @@ void OSFatal_printf(const char *format, ...);
OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \
} while (0) } 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 { \ #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \
log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \
} while (0) } while (0);
#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \
WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \
} while (0);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif

View File

@ -1,40 +1,37 @@
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <whb/log.h>
#include <malloc.h> #include "utils/logger.h"
#include <utils/logger.h>
// https://gist.github.com/ccbrown/9722406 // https://gist.github.com/ccbrown/9722406
void dumpHex(const void* data, size_t size) { void dumpHex(const void *data, size_t size) {
char ascii[17]; char ascii[17];
size_t i, j; size_t i, j;
ascii[16] = '\0'; ascii[16] = '\0';
DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data);
for (i = 0; i < size; ++i) { 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] <= '~') { if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i]; ascii[i % 16] = ((unsigned char *) data)[i];
} else { } else {
ascii[i % 16] = '.'; ascii[i % 16] = '.';
} }
if ((i+1) % 8 == 0 || i+1 == size) { if ((i + 1) % 8 == 0 || i + 1 == size) {
log_printf(" "); WHBLogWritef(" ");
if ((i+1) % 16 == 0) { if ((i + 1) % 16 == 0) {
log_printf("| %s \n", ascii); WHBLogPrintf("| %s ", ascii);
if(i + 1 < size) { if (i + 1 < size) {
DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1,i+1); DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1);
} }
} else if (i+1 == size) { } else if (i + 1 == size) {
ascii[(i+1) % 16] = '\0'; ascii[(i + 1) % 16] = '\0';
if ((i+1) % 16 <= 8) { if ((i + 1) % 16 <= 8) {
log_printf(" "); WHBLogWritef(" ");
} }
for (j = (i+1) % 16; j < 16; ++j) { for (j = (i + 1) % 16; j < 16; ++j) {
log_printf(" "); WHBLogWritef(" ");
} }
log_printf("| %s \n", ascii); WHBLogPrintf("| %s ", ascii);
} }
} }
} }

View File

@ -1,5 +1,4 @@
#ifndef __UTILS_H_ #pragma once
#define __UTILS_H_
#include <malloc.h> #include <malloc.h>
@ -7,12 +6,12 @@
extern "C" { extern "C" {
#endif #endif
#define LIMIT(x, min, max) \ #define LIMIT(x, min, max) \
({ \ ({ \
typeof( x ) _x = x; \ typeof( x ) _x = x; \
typeof( min ) _min = min; \ typeof( min ) _min = min; \
typeof( max ) _max = max; \ typeof( max ) _max = max; \
( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \
}) })
#define DegToRad(a) ( (a) * 0.01745329252f ) #define DegToRad(a) ( (a) * 0.01745329252f )
@ -21,15 +20,18 @@ extern "C" {
#define ALIGN4(x) (((x) + 3) & ~3) #define ALIGN4(x) (((x) + 3) & ~3)
#define ALIGN32(x) (((x) + 31) & ~31) #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 le16(i) ((((uint16_t) ((i) & 0xFF)) << 8) | ((uint16_t) (((i) & 0xFF00) >> 8))) #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))) #define le32(i) ((((uint32_t)le16((i) & 0xFFFF)) << 16) | ((uint32_t)le16(((i) & 0xFFFF0000) >> 16)))
#define le64(i) ((((uint64_t)le32((i) & 0xFFFFFFFFLL)) << 32) | ((uint64_t)le32(((i) & 0xFFFFFFFF00000000LL) >> 32))) #define le64(i) ((((uint64_t)le32((i) & 0xFFFFFFFFLL)) << 32) | ((uint64_t)le32(((i) & 0xFFFFFFFF00000000LL) >> 32)))
//Needs to have log_init() called beforehand. //Needs to have log_init() called beforehand.
void dumpHex(const void* data, size_t size); void dumpHex(const void *data, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // __UTILS_H_