Commit c13c14d2 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

o Move log functions to a common library and use it in the console

o log_proxy has an extra server port to receive logs
parent cc1c3ef7
......@@ -11,17 +11,20 @@ all:
$(MAKE) -C console
controller:
$(MAKE) -C common/log
$(MAKE) -C common/utils
$(MAKE) -C common/trace
$(MAKE) -C controller
console:
$(MAKE) -C common/log
$(MAKE) -C common/utils
$(MAKE) -C common/trace
cd libnova-0.13.0 ; ./configure -enable-static -disable-shared --prefix=${CURDIR} ; make; make install
$(MAKE) -C console
clean:
$(MAKE) -C common/log clean
$(MAKE) -C common/utils clean
$(MAKE) -C common/trace clean
$(MAKE) -C controller clean
......
......@@ -31,6 +31,7 @@
#define CONSOLE_DT_MODEL_STAT_PORT 11090
#define CONSOLE_DT_MODEL_CMD_PORT 11091
#define CONSOLE_LOG_PORT 11200
#define CONSOLE_LOG_PORT_IN 11201
/* Port numbers for controller */
......
CFLAGS= -Wall -O3
CFLAGS= -Wall -O3 -I../utils
LDFLAGS= -lutils
ARCHSRCS = log.c
ARCHOBJS = $(ARCHSRCS:.c=.lo)
......
......@@ -36,9 +36,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <tcp_connect.h>
#include "log.h"
static int listen_port = 0;
static int server_port = 0;
static char *server_host = NULL;
static char *client_name = NULL;
struct log_client {
int used;
......@@ -235,7 +242,7 @@ static void *log_server(void *arg)
write(clients[i].fd, time, strlen(time));
write(clients[i].fd, " ", 1);
write(clients[i].fd, header, strlen(header));
write(clients[i].fd, msgs[msgs_rd].msg,
strlen(msgs[msgs_rd].msg));
......@@ -295,6 +302,123 @@ int log_server_start(int port, enum log_type console_level,
return 0;
}
static void *log_client(void *arg)
{
int fd = -1;
signal(SIGPIPE, SIG_IGN);
while (1) {
if (!msgs[msgs_rd].used) {
sem_wait(&queue_wait);
}
if (msgs[msgs_rd].used) {
char time[100];
char *header;
struct tm gmt;
gmtime_r(&msgs[msgs_rd].t, &gmt);
timestamp(&gmt, time);
switch(msgs[msgs_rd].type) {
case LOG_T_ERROR:
header = " ERROR: ";
break;
case LOG_T_WARNING:
header = "WARNING: ";
break;
case LOG_T_INFO:
header = " INFO: ";
break;
case LOG_T_DEBUG:
header = " DEBUG: ";
break;
default:
header = "UNKNOWN: ";
break;
}
if (fd < 0) {
fd = tcp_connect(server_host, server_port);
}
if (msgs[msgs_rd].type <= log_level_remote && fd >= 0) {
int r;
write(fd, time, strlen(time));
write(fd, " ", 1);
write(fd, header, strlen(header));
if (client_name) {
write(fd, client_name, strlen(client_name));
write(fd, ": ", 2);
}
write(fd, msgs[msgs_rd].msg,
strlen(msgs[msgs_rd].msg));
r = write(fd, "\n", 1);
fsync(fd);
if (r <= 0) {
close(fd);
fd = -1;
}
}
if (msgs[msgs_rd].type <= log_level_console)
printf("%s %s%s\n", time, header, msgs[msgs_rd].msg);
msgs[msgs_rd].used = 0;
msgs_rd++;
msgs_rd %= LOG_MAX_QUEUE;
}
}
return NULL;
}
int log_client_start(char *host, int port, enum log_type console_level,
enum log_type remote_level, char *name)
{
pthread_t thread_id;
pthread_attr_t attr;
int i;
log_level_console = console_level;
log_level_remote = remote_level;
server_port = port;
if (server_host)
free(server_host);
server_host = strdup(host);
if (client_name)
free(client_name);
client_name = strdup(name);
for (i = 0; i < LOG_MAX_QUEUE; i++) {
msgs[i].used = 0;
}
for (i = 0; i < LOG_MAX_CLIENTS; i++) {
clients[i].fd = -1;
clients[i].used = 0;
}
sem_init(&queue_wait, 0, 1);
printf("Starting log client %s:%d for %s\n", host, port, name);
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);
pthread_create(&thread_id, &attr, log_client, NULL);
return 0;
}
void log_server_flush(void)
{
sleep(1);
......
......@@ -28,8 +28,12 @@ enum log_type {
};
void log_send(enum log_type type, char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
int log_server_start(int port, enum log_type console_level,
enum log_type remote_level);
void log_server_flush(void);
int log_client_start(char *host, int port, enum log_type console_level,
enum log_type remote_level, char *name);
#endif /* _INCLUDE_LOG_H_ */
include ../build.mk
CFLAGS= -Wall -g -I../../common/utils -I../controller -Iaalib -Ipredictlib -I../../common/trace -I../../common/include -I../../include
LDFLAGS+= -L../../common/lib/ -L../../lib/ -L./lib -lutils -ltrace -static
CFLAGS= -Wall -g -I../../common/utils \
-I../controller \
-Iaalib -Ipredictlib \
-I../../common/trace \
-I../../common/include \
-I../../common/log \
-I../../include
LDFLAGS+= -L../../common/lib/ -L../../lib/ -L./lib -lutils -ltrace -static -llog
LIBNOVA=-lnova -L../../lib/
......
......@@ -41,9 +41,10 @@
#include "dt_model.h"
#include "dt_port_numbers.h"
#include "dt_host.h"
#include "command_server.h"
#include "status_server.h"
#include "log.h"
int stat_port = CONSOLE_DT_MODEL_STAT_PORT;
......@@ -133,7 +134,7 @@ static int load_ini(void)
}
}
} else {
printf("ini file load failed\n");
log_send(LOG_T_ERROR, "ini file load failed\n");
ret = -1;
}
......@@ -150,8 +151,10 @@ static int load_ini(void)
static int handle_cmd(char *name, char *val)
{
printf("name %s value %s\n", name, val);
if (!strcmp(name, "reload"))
if (!strcmp(name, "reload")) {
log_send(LOG_T_WARNING, "Reloading ini file");
load_ini();
}
return 0;
}
......@@ -207,6 +210,10 @@ int main(int argc, char **argv)
printf("Could not open listen port for status\n");
}
log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN,
LOG_T_DEBUG, LOG_T_INFO, "console/dt_model");
while (1)
{
int high = 0;
......
......@@ -51,6 +51,8 @@
#include "dt_port_numbers.h"
#include "command_server.h"
#include "status_server.h"
#include "dt_host.h"
#include "log.h"
#define TIME_OFFSET 2
......@@ -189,8 +191,11 @@ int handle_cmd(char *command)
object.ra = ln_hms_to_deg(&track_hms);
object.dec = ln_dms_to_deg(&track_dms);
if (oldra != object.ra || olddec != object.dec)
if (oldra != object.ra || olddec != object.dec) {
log_send(LOG_T_INFO, "New J2000 coordinates: ra=%f, dec=%f",
object.ra, object.dec);
tracking_command_last_az_valid = false;
}
if (switches) {
char *name, *val;
......@@ -360,6 +365,9 @@ int main(int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN,
LOG_T_DEBUG, LOG_T_INFO, "console/j2000tracker");
if (argc == 2 || argc == 4) {
command_host = argv[argc - 1];
trace_host = argv[argc - 1];
......@@ -476,7 +484,8 @@ int main(int argc, char **argv)
tracking_command_send_p180 &&
tracking_enabled) {
tracking_enabled = false;
printf("Going over 270 degree border, disabling\n\n");
log_send(LOG_T_WARNING,
"Going over 270 degree border, disabling");
}
hrz.az -= 360.0;
}
......@@ -507,7 +516,8 @@ int main(int argc, char **argv)
if (tracking_enabled && tracking_command_last_az_valid) {
if (fabs(hrz.az - tracking_command_last_az) > 1.0) {
printf("Az going to fast to track (%e), disable tracking\n",
log_send(LOG_T_WARNING,
"Az going to fast to track (%e), disable tracking",
fabs(hrz.az-tracking_command_last_az));
tracking_enabled = false;
setpoint_command_speed(sp_command_az, 0.0);
......
......@@ -47,9 +47,10 @@
#include "predictlib.h"
#include "dt_port_numbers.h"
#include "dt_host.h"
#include "command_server.h"
#include "status_server.h"
#include "log.h"
#define TIME_OFFSET 2
......@@ -88,6 +89,8 @@ bool set_tle(char newtle[139])
predict_free(predict);
predict = predict_create(tle_str, lat, lon, alt);
log_send(LOG_T_INFO, "Switching to new TLE");
return true;
}
......@@ -218,6 +221,9 @@ int main(int argc, char **argv)
command_host = argv[argc - 1];
}
log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN,
LOG_T_DEBUG, LOG_T_INFO, "console/sattracker");
sp_command_az = setpoint_command_init(command_host, command_port,
az_command_spg, "console/sattracker");
sp_command_el = setpoint_command_init(command_host, command_port,
......@@ -305,6 +311,8 @@ int main(int argc, char **argv)
}
}
if (!switch_cmd && fabs(last_az - az) > 180.0) {
log_send(LOG_T_WARNING,
"Disabling tracker to prevent large azimuth jump");
switch_enabled = false;
}
......
......@@ -26,6 +26,7 @@
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include "tcp_listen.h"
......@@ -33,9 +34,26 @@
#include "dt_port_numbers.h"
#include "dt_host.h"
#include "command_server.h"
char *logfile = "controller.log";
int rb;
char buffer[1000];
int handle_cmd(char *command)
{
strncpy(buffer, command, 998);
buffer[998] = 0;
buffer[strlen(buffer) + 1] = 0;
buffer[strlen(buffer)] = '\n';
rb = strlen(buffer);
printf("Handling command rb = %d\n", rb);
return 0;
}
int main (int argc, char **argv)
{
int fd_listen;
......@@ -44,6 +62,7 @@ int main (int argc, char **argv)
int *fd_clients = NULL;
int nr_clients = 0;
int fd_logfile = -1;
struct command_server *cmd_srv;
if (argc != 1 && argc != 2) {
printf("Usage: %s <logfile>\n", argv[0]);
......@@ -61,31 +80,54 @@ int main (int argc, char **argv)
printf("Could not open listen port\n");
return 1;
}
cmd_srv = command_server_create(CONSOLE_LOG_PORT_IN, 0, 100);
if (!cmd_srv) {
printf("Could not open listen port for logging\n");
return 1;
} else {
command_server_handler_raw_set(cmd_srv, handle_cmd);
}
signal(SIGPIPE, SIG_IGN);
ioctl(fd_listen, FIONBIO, &(int){ 1 });
ioctl(fd_listen, FIONBIO, &(int){ 1 });
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
f_log = fdopen(fd_log, "r");
while (1) {
int rb;
char buffer[1000];
int fdnew;
fd_set fd_rd;
struct timeval timeout;
int high;
timeout.tv_usec = 0;
timeout.tv_sec = 1;
FD_ZERO(&fd_rd);
FD_SET(fd_log, &fd_rd);
high = fd_log;
FD_SET(fd_listen, &fd_rd);
high = MAX(high, fd_listen);
command_server_fdset_add(cmd_srv, &fd_rd, &high);
select(high + 1, &fd_rd, NULL, NULL, &timeout);
rb = -1;
if (fd_log < 0) {
while (fd_log < 0) {
sleep(1);
printf("Trying to connect to server.\n");
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
printf("Trying to connect to server.\n");
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
if (fd_log >= 0) {
rb = sprintf(buffer,
"Established connection to controller\n");
printf(buffer);
f_log = fdopen(fd_log, "r");
printf("fd_log: %d f_log %p\n", fd_log, f_log);
}
rb = sprintf(buffer,
"Established connection to controller\n");
printf(buffer);
f_log = fdopen(fd_log, "r");
printf("fd_log: %d f_log %p\n", fd_log, f_log);
} else {
} else if (FD_ISSET(fd_log, &fd_rd)) {
char *s;
printf("Going to wait for data\n");
......@@ -105,6 +147,8 @@ int main (int argc, char **argv)
"Lost connection to controller\n");
printf(buffer);
}
} else {
command_server_fdset_handle(cmd_srv, &fd_rd);
}
if (fd_logfile < 0) {
......@@ -112,7 +156,7 @@ int main (int argc, char **argv)
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
}
if (fd_logfile >= 0) {
if (fd_logfile >= 0 && rb > 0) {
int ret;
ret = write(fd_logfile, buffer, rb);
......@@ -122,6 +166,7 @@ int main (int argc, char **argv)
}
}
fdnew = tcp_accept(fd_listen);
if (fdnew >= 0) {
nr_clients++;
......@@ -130,7 +175,7 @@ int main (int argc, char **argv)
ioctl(fdnew, FIONBIO, &(int){ 1 });
printf("New client. (now at %d)\n", nr_clients);
}
if (rb > 0) {
int i;
for (i = 0; i < nr_clients; i++) {
......
......@@ -45,6 +45,8 @@
#include "dt_port_numbers.h"
#include "dt_host.h"
#include "log.h"
struct spg_client {
struct spg_client *next;
......@@ -389,7 +391,7 @@ int handle_con_client(char *command)
setpoint_command_speed(spgs[i].spg_cmd, 0.0);
}
printf("New authorised user: %s\n",
log_send(LOG_T_INFO, "Changed setpoint source to: %s",
current_auth);
}
return 0;
......@@ -428,6 +430,11 @@ int main (int argc, char **argv)
logfile = argv[2];
}
log_client_start(dt_host_console(), CONSOLE_LOG_PORT_IN,
LOG_T_DEBUG, LOG_T_INFO, "spg_auth");
log_send(LOG_T_INFO, "spg_auth started");
for (i = 0; spgs[i].name; i++) {
spgs[i].spg_cmd = setpoint_command_init(
command_host, CTRL_COMMAND_PORT, spgs[i].name, command_id);
......
......@@ -3,10 +3,11 @@ include build.mk
CFLAGS= -Wall -O3 \
-I. \
-Icontroller \
-I../common/log \
-I../common/utils \
-I../common/trace \
-I../common/include \
-Idt_azimuth -Idt_elevation -Iec -Ishell -Ilog
-Idt_azimuth -Idt_elevation -Iec -Ishell
LDFLAGS= -lpthread -lrt -lm -Wl,-E -L../common/lib -L./lib
CFLAGS_SIM=-DUSE_AZ_SIM -DUSE_EL_SIM
......@@ -32,7 +33,7 @@ lib/libdt_elevation.la: lib/libcontroller.la
dt_ctrl: dt_ctrl.o
test: lib/libcontroller.la lib/libethercat.la lib/liblog.la
test: lib/libcontroller.la lib/libethercat.la
@echo " SUBDIR: $@"
@$(MAKE) -C test
......@@ -50,20 +51,16 @@ lib/libdt_elevation.la: lib
@echo " SUBDIR: $@"
@$(MAKE) -C dt_elevation
lib/libethercat.la: lib/liblog.la lib/libshell.la lib
lib/libethercat.la: lib/libshell.la lib
lib/libethercat.la:
@echo " SUBDIR: $@"
@$(MAKE) -C ec
lib/libshell.la: lib/liblog.la lib
lib/libshell.la: lib
@echo " SUBDIR: $@"
@$(MAKE) -C shell
lib/liblog.la: lib
@echo " SUBDIR: $@"
@$(MAKE) -C log
lib:
@mkdir lib
......@@ -80,4 +77,3 @@ clean:
$(MAKE) -C ec clean
$(MAKE) -C test clean
$(MAKE) -C shell clean
$(MAKE) -C log clean
IL2C=il2c/il2c
CFLAGS= -O3 -Wall -I../log/ -I../shell/ -I.. -I../../common/include
CFLAGS= -O3 -Wall -I../../common/log/ -I../shell/ -I.. -I../../common/include
BLOCKSRCS= \
block_add.c \
block_and2.c \
......@@ -91,7 +91,7 @@ libcontroller.la_install: libcontroller.la
@${LIBTOOL} --quiet --mode=install install libcontroller.la ${CURDIR}/../lib
clean:
rm -rf *.o *.a *.yy.* *.tab.* *.d *.lo *.la libs
rm -rf *.o *.a *.yy.* *.tab.* *.d *.lo *.la .libs
@$(MAKE) -C il2c clean
include ../build.mk
CFLAGS= -Wall -O3 -I../controller -I../ec -I../log -I../utils
CFLAGS= -Wall -O3 -I../controller -I../ec -I../../common/log -I../utils
ARCHSRCS= dt_az_safety.c dt_az_stoeber_sim.c
ARCHOBJS=$(ARCHSRCS:.c=.lo)
......
CFLAGS= -Wall -I../controller -I../ec -I../log -I../utils -g
CFLAGS= -Wall -I../controller -I../ec -I../../common/log -I../utils -g
SRCS= dt_el_safety.c dt_el_stoeber_r_sim.c dt_el_stoeber_l_sim.c
......
......@@ -2,7 +2,7 @@
OS= $(shell uname -s)
CFLAGS= -Wall -O3 \
-I. -I.. -I../log -I../controller
-I. -I.. -I../../common/log -I../controller
LDFLAGS= -lpthread -lrt -lm -L../lib
ifneq ($(OS), FreeBSD)
......
CFLAGS= -Wall -O3 -I ../log
CFLAGS= -Wall -O3 -I ../../common/log
SRCS= shell.c
ARCHOBJS = $(SRCS:.c=.lo)
......@@ -14,7 +14,7 @@ libshell.la_install: libshell.la
clean:
rm -rf *.o *.a *.d *.lo *.la libs
rm -rf *.o *.a *.d *.lo *.la .libs