Server logging cleanup/functionality

Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/Makefile b/Makefile
index 7c58192..ea3306b 100644
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@
 		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 6aaad01..d9e3857 100644
--- a/client.c
+++ b/client.c
@@ -126,6 +126,8 @@
 
 	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 0000000..8396522
--- /dev/null
+++ b/infolog.c
@@ -0,0 +1,38 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdarg.h>
+
+#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 28852fb..87052b9 100644
--- a/init.c
+++ b/init.c
@@ -177,16 +177,6 @@
 	},
 };
 
-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 eea1129..916f464 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 1749e73..4a78f38 100644
--- a/server.c
+++ b/server.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <limits.h>
 #include <errno.h>
@@ -367,11 +368,12 @@
 	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 @@
 
 	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 c2cb994..969659d 100644
--- a/server.h
+++ b/server.h
@@ -2,6 +2,7 @@
 #define FIO_SERVER_H
 
 #include <inttypes.h>
+#include <string.h>
 #include <endian.h>
 
 /*
@@ -34,7 +35,8 @@
 };
 
 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 *);