2020-04-28 14:43:07 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2020-04-29 12:05:39 +02:00
|
|
|
#include "logger.h"
|
2020-04-28 14:43:07 +02:00
|
|
|
#include <coreinit/debug.h>
|
|
|
|
#include <coreinit/systeminfo.h>
|
|
|
|
#include <coreinit/thread.h>
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
static int log_socket __attribute__((section(".data"))) = -1;
|
2020-04-28 14:43:07 +02:00
|
|
|
static struct sockaddr_in connect_addr __attribute__((section(".data")));
|
|
|
|
static volatile int log_lock __attribute__((section(".data"))) = 0;
|
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
#define SOL_SOCKET -1
|
|
|
|
|
|
|
|
|
|
|
|
#define SOCK_DGRAM 2
|
|
|
|
|
|
|
|
|
|
|
|
#define INADDR_ANY 0
|
|
|
|
#define INADDR_BROADCAST 0xFFFFFFFF
|
|
|
|
|
|
|
|
#define PF_UNSPEC 0
|
|
|
|
#define PF_INET 2
|
|
|
|
#define PF_INET6 23
|
|
|
|
|
|
|
|
#define AF_UNSPEC PF_UNSPEC
|
|
|
|
#define AF_INET PF_INET
|
|
|
|
#define AF_INET6 PF_INET6
|
|
|
|
|
|
|
|
#define IPPROTO_UDP 17
|
|
|
|
|
|
|
|
#define SO_BROADCAST 0x0020 // broadcast
|
|
|
|
|
|
|
|
typedef uint16_t sa_family_t;
|
|
|
|
|
2021-09-23 18:38:29 +02:00
|
|
|
struct in_addr {
|
|
|
|
unsigned int s_addr;
|
2021-04-17 11:53:51 +02:00
|
|
|
};
|
|
|
|
|
2021-09-23 18:38:29 +02:00
|
|
|
struct sockaddr {
|
|
|
|
sa_family_t sa_family;
|
|
|
|
char sa_data[];
|
2021-04-17 11:53:51 +02:00
|
|
|
};
|
|
|
|
|
2021-09-23 18:38:29 +02:00
|
|
|
struct sockaddr_in {
|
|
|
|
unsigned short sin_family;
|
|
|
|
unsigned short sin_port;
|
|
|
|
struct in_addr sin_addr;
|
|
|
|
char sin_zero[8];
|
2021-04-17 11:53:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef uint32_t socklen_t;
|
|
|
|
|
|
|
|
extern int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
|
2021-09-23 18:38:29 +02:00
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
extern int socket(int domain, int type, int protocol);
|
2021-09-23 18:38:29 +02:00
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
extern int socketclose(int sockfd);
|
2021-09-23 18:38:29 +02:00
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
extern int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
|
2021-09-23 18:38:29 +02:00
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
extern uint32_t htonl(uint32_t val);
|
|
|
|
|
|
|
|
void log_init() {
|
2020-04-28 14:43:07 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
void log_deinit() {
|
2021-09-23 18:38:29 +02:00
|
|
|
if (log_socket < 0) {
|
2021-04-17 11:53:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-23 18:38:29 +02:00
|
|
|
if (socketclose(log_socket) != 0) {
|
|
|
|
return;
|
2021-04-17 11:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_print(const char *str) {
|
2021-11-06 23:37:52 +01:00
|
|
|
OSReport(str);
|
2020-04-28 14:43:07 +02:00
|
|
|
// socket is always 0 initially as it is in the BSS
|
2020-05-17 19:05:51 +02:00
|
|
|
if (log_socket < 0) {
|
2020-04-28 14:43:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-17 19:05:51 +02:00
|
|
|
while (log_lock) {
|
2020-04-28 14:43:07 +02:00
|
|
|
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
|
2020-05-17 19:05:51 +02:00
|
|
|
ret = sendto(log_socket, str, block, 0, (struct sockaddr *) &connect_addr, sizeof(struct sockaddr_in));
|
|
|
|
if (ret < 0)
|
2020-04-28 14:43:07 +02:00
|
|
|
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);
|
2020-05-17 19:05:51 +02:00
|
|
|
if ((vsprintf(tmp, format, va) >= 0)) {
|
2020-04-28 14:43:07 +02:00
|
|
|
OSFatal(tmp);
|
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2021-04-17 11:53:51 +02:00
|
|
|
void log_printf(const char *format, ...) {
|
2020-05-17 19:05:51 +02:00
|
|
|
if (log_socket < 0) {
|
2020-04-28 14:43:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmp[512];
|
|
|
|
tmp[0] = 0;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
2020-05-17 19:05:51 +02:00
|
|
|
if ((vsprintf(tmp, format, va) >= 0)) {
|
2021-04-17 11:53:51 +02:00
|
|
|
log_print(tmp);
|
2020-04-28 14:43:07 +02:00
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|