libutils/source/utils/logger.c

87 lines
1.9 KiB
C
Raw Normal View History

2017-10-29 09:24:06 +01:00
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "logger.h"
#include <dynamic_libs/os_functions.h>
#include <dynamic_libs/socket_functions.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_(){
InitOSFunctionPointers();
InitSocketFunctionPointers();
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;
2017-12-27 12:44:24 +01:00
connect_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
2017-10-29 09:24:06 +01:00
}
void log_print_(const char *str){
// socket is always 0 initially as it is in the BSS
if(log_socket < 0) {
return;
}
2017-12-27 12:44:24 +01:00
2017-10-29 09:24:06 +01:00
while(log_lock)
os_usleep(1000);
log_lock = 1;
2017-12-27 12:44:24 +01:00
2017-10-29 09:24:06 +01:00
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;
}
2017-12-27 12:44:24 +01:00
void OSFatal_printf(const char *format, ...){
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp){
OSFatal(tmp);
}
va_end(va);
}
2017-10-29 09:24:06 +01:00
void log_printf_(const char *format, ...)
{
if(log_socket < 0) {
return;
}
2017-12-27 12:44:24 +01:00
2017-10-29 09:24:06 +01:00
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
log_print_(tmp);
}
va_end(va);
if(tmp)
free(tmp);
}