From 142575e6579462656a6d0f7c50ec8c35b8a08802 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Fri, 30 Sep 2011 17:35:45 -0600 Subject: [PATCH] Server logging cleanup/functionality Signed-off-by: Jens Axboe --- Makefile | 2 +- client.c | 2 ++ infolog.c | 38 ++++++++++++++++++++++++++++++++++++++ init.c | 10 ---------- log.h | 14 ++------------ server.c | 21 ++++++++++++++++++--- server.h | 4 +++- 7 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 infolog.c diff --git a/Makefile b/Makefile index 7c581927..ea3306b3 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ SOURCE = gettime.c fio.c ioengines.c init.c stat.c log.c time.c filesetup.c \ rbtree.c smalloc.c filehash.c profile.c debug.c lib/rand.c \ lib/num2str.c $(wildcard crc/*.c) engines/cpu.c \ engines/mmap.c engines/sync.c engines/null.c engines/net.c \ - memalign.c server.c client.c + memalign.c server.c client.c infolog.c ifeq ($(UNAME), Linux) SOURCE += diskutil.c fifo.c blktrace.c helpers.c cgroup.c trim.c \ diff --git a/client.c b/client.c index 6aaad019..d9e3857e 100644 --- a/client.c +++ b/client.c @@ -126,6 +126,8 @@ int fio_handle_clients(void) while (!exit_backend) { cmd = fio_net_cmd_read(fio_client_fd); + if (!cmd) + continue; if (cmd->opcode == FIO_NET_CMD_ACK) { free(cmd); diff --git a/infolog.c b/infolog.c new file mode 100644 index 00000000..8396522e --- /dev/null +++ b/infolog.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#include "fio.h" + +int log_info(const char *format, ...) +{ + char buffer[1024]; + va_list args; + size_t len; + + va_start(args, format); + len = vsprintf(buffer, format, args); + va_end(args); + + if (is_backend) + return fio_server_text_output(buffer, len); + else + return fwrite(buffer, len, 1, f_out); +} + +int log_err(const char *format, ...) +{ + char buffer[1024]; + va_list args; + size_t len; + + va_start(args, format); + len = vsprintf(buffer, format, args); + va_end(args); + + if (is_backend) + return fio_server_text_output(buffer, len); + else + return fwrite(buffer, len, 1, f_err); +} diff --git a/init.c b/init.c index 28852fb7..87052b9e 100644 --- a/init.c +++ b/init.c @@ -177,16 +177,6 @@ static struct option l_opts[FIO_NR_OPTIONS] = { }, }; -FILE *get_f_out() -{ - return f_out; -} - -FILE *get_f_err() -{ - return f_err; -} - /* * Return a free job structure. */ diff --git a/log.h b/log.h index eea1129b..916f464d 100644 --- a/log.h +++ b/log.h @@ -6,19 +6,9 @@ extern FILE *f_out; extern FILE *f_err; -/* - * If logging output to a file, stderr should go to both stderr and f_err - */ -#define log_err(args, ...) do { \ - fprintf(f_err, args, ##__VA_ARGS__); \ - if (f_err != stderr) \ - fprintf(stderr, args, ##__VA_ARGS__); \ - } while (0) +extern int log_err(const char *format, ...); +extern int log_info(const char *format, ...); -#define log_info(args, ...) fprintf(f_out, args, ##__VA_ARGS__) #define log_valist(str, args) vfprintf(f_out, (str), (args)) -FILE *get_f_out(void); -FILE *get_f_err(void); - #endif diff --git a/server.c b/server.c index 1749e736..4a78f383 100644 --- a/server.c +++ b/server.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -367,11 +368,12 @@ int fio_server(void) return ret; } -void fio_server_text_output(const char *buf, unsigned int len) +int fio_server_text_output(const char *buf, unsigned int len) { struct fio_net_cmd *cmd; + int size = sizeof(*cmd) + len; - cmd = malloc(sizeof(*cmd) + len); + cmd = malloc(size); fio_init_net_cmd(cmd); cmd->opcode = cpu_to_le16(FIO_NET_CMD_TEXT); cmd->pdu_len = cpu_to_le32(len); @@ -379,6 +381,19 @@ void fio_server_text_output(const char *buf, unsigned int len) fio_net_cmd_crc(cmd); - fio_send_data(server_fd, cmd, sizeof(*cmd) + len); + fio_send_data(server_fd, cmd, size); free(cmd); + return size; +} + +int fio_server_log(const char *format, ...) +{ + char buffer[1024]; + va_list args; + + va_start(args, format); + sprintf(buffer, format, args); + va_end(args); + + return fio_server_text_output(buffer, strlen(buffer)); } diff --git a/server.h b/server.h index c2cb9942..969659dd 100644 --- a/server.h +++ b/server.h @@ -2,6 +2,7 @@ #define FIO_SERVER_H #include +#include #include /* @@ -34,7 +35,8 @@ enum { }; extern int fio_server(void); -extern void fio_server_text_output(const char *, unsigned int len); +extern int fio_server_text_output(const char *, unsigned int len); +extern int fio_server_log(const char *format, ...); extern int fio_client_connect(const char *); extern int fio_client_send_ini(const char *); -- 2.25.1