Small improvements to the frontend lib for better debugging
authorMedo <smaxein@googlemail.com>
Mon, 11 Jun 2012 00:02:17 +0200
changeset 7182 076aba32abd3
parent 7179 f84805e6df03
child 7221 8d04e85ca204
Small improvements to the frontend lib for better debugging
project_files/frontlib/frontlib.c
project_files/frontlib/ipc/ipcconn.c
project_files/frontlib/util/logging.c
project_files/frontlib/util/logging.h
--- a/project_files/frontlib/frontlib.c	Sat Jun 09 03:28:38 2012 +0200
+++ b/project_files/frontlib/frontlib.c	Mon Jun 11 00:02:17 2012 +0200
@@ -64,6 +64,7 @@
 	setup.gamescheme = flib_cfg_from_ini(metaconf, "scheme_shoppa.ini");
 	setup.map = flib_map_create_maze("Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
 	setup.seed = "apsfooasdgnds";
+	setup.script = NULL;
 	setup.teamcount = 2;
 	setup.teams = calloc(2, sizeof(flib_team));
 	setup.teams[0].color = 0xffff0000;
--- a/project_files/frontlib/ipc/ipcconn.c	Sat Jun 09 03:28:38 2012 +0200
+++ b/project_files/frontlib/ipc/ipcconn.c	Mon Jun 11 00:02:17 2012 +0200
@@ -155,6 +155,22 @@
 	}
 }
 
+static void logSentMsg(const uint8_t *data, size_t len) {
+	if(flib_log_getLevel() > FLIB_LOGLEVEL_DEBUG) {
+		size_t msgStart = 0;
+		while(msgStart < len) {
+			uint8_t msglen = data[msgStart];
+			if(msgStart+msglen < len) {
+				flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
+			} else {
+				uint8_t msglen2 = len-msgStart-1;
+				flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
+			}
+			msgStart += (uint8_t)data[msgStart]+1;
+		}
+	}
+}
+
 int flib_ipcconn_send_raw(flib_ipcconn ipc, const void *data, size_t len) {
 	if(!ipc || (!data && len>0)) {
 		flib_log_e("Call to flib_ipcconn_send_raw with ipc==null or data==null");
@@ -166,6 +182,7 @@
 	}
 
 	if(flib_socket_send(ipc->sock, data, len) == len) {
+		logSentMsg(data, len);
 		if(ipc->demoBuffer) {
 			if(flib_demo_record_to_engine(ipc->demoBuffer, data, len) < 0) {
 				flib_log_w("Stopping demo recording due to an error.");
@@ -189,7 +206,6 @@
 	uint8_t sendbuf[256];
 	sendbuf[0] = len;
 	memcpy(sendbuf+1, data, len);
-
 	return flib_ipcconn_send_raw(ipc, sendbuf, len+1);
 }
 
--- a/project_files/frontlib/util/logging.c	Sat Jun 09 03:28:38 2012 +0200
+++ b/project_files/frontlib/util/logging.c	Mon Jun 11 00:02:17 2012 +0200
@@ -5,13 +5,24 @@
 #include <stdarg.h>
 #include <stdlib.h>
 
+static int flib_loglevel = FLIB_LOGLEVEL_INFO;
+static FILE *flib_logfile = NULL;
+
 char* flib_format_ip(uint32_t numip) {
 	static char ip[16];
 	snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff));
 	return ip;
 }
 
-static void log_time(FILE *file) {
+static inline FILE *flib_log_getfile() {
+	if(flib_logfile==NULL) {
+		return stdout;
+	} else {
+		return flib_logfile;
+	}
+}
+
+static void log_time() {
     time_t timer;
     char buffer[25];
     struct tm* tm_info;
@@ -20,34 +31,57 @@
     tm_info = localtime(&timer);
 
     strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
-    fprintf(file, "%s", buffer);
+    fprintf(flib_log_getfile(), "%s", buffer);
 }
 
-static void flib_vflog(FILE *file, const char *prefix, const char *fmt, va_list args) {
-	log_time(file);
-	fprintf(file, " [%s]", prefix);
-	vfprintf(file, fmt, args);
-	fprintf(file, "\n");
-	fflush(file);
+static void flib_vflog(const char *prefix, int level, const char *fmt, va_list args) {
+	FILE *logfile = flib_log_getfile();
+	if(level >= flib_loglevel) {
+		fprintf(logfile, "%s ", prefix);
+		log_time(logfile);
+		fprintf(logfile, "  ", prefix);
+		vfprintf(logfile, fmt, args);
+		fprintf(logfile, "\n");
+		fflush(logfile);
+	}
 }
 
 void flib_log_e(const char *fmt, ...) {
 	va_list argp;
 	va_start(argp, fmt);
-	flib_vflog(stderr, "E", fmt, argp);
+	flib_vflog("E", FLIB_LOGLEVEL_ERROR, fmt, argp);
 	va_end(argp);
 }
 
 void flib_log_w(const char *fmt, ...) {
 	va_list argp;
 	va_start(argp, fmt);
-	flib_vflog(stdout, "W", fmt, argp);
+	flib_vflog("W", FLIB_LOGLEVEL_WARNING, fmt, argp);
 	va_end(argp);
 }
 
 void flib_log_i(const char *fmt, ...) {
 	va_list argp;
 	va_start(argp, fmt);
-	flib_vflog(stdout, "I", fmt, argp);
+	flib_vflog("I", FLIB_LOGLEVEL_INFO, fmt, argp);
+	va_end(argp);
+}
+
+void flib_log_d(const char *fmt, ...) {
+	va_list argp;
+	va_start(argp, fmt);
+	flib_vflog("D", FLIB_LOGLEVEL_DEBUG, fmt, argp);
 	va_end(argp);
 }
+
+int flib_log_getLevel() {
+	return flib_loglevel;
+}
+
+void flib_log_setLevel(int level) {
+	flib_loglevel = level;
+}
+
+void flib_log_setFile(FILE *file) {
+	flib_logfile = file;
+}
--- a/project_files/frontlib/util/logging.h	Sat Jun 09 03:28:38 2012 +0200
+++ b/project_files/frontlib/util/logging.h	Mon Jun 11 00:02:17 2012 +0200
@@ -1,16 +1,25 @@
-/*
- *
- */
-
 #ifndef LOGGING_H_
 #define LOGGING_H_
 
 #include<stdint.h>
+#include <stdio.h>
+
+#define FLIB_LOGLEVEL_ALL -100
+#define FLIB_LOGLEVEL_DEBUG -1
+#define FLIB_LOGLEVEL_INFO 0
+#define FLIB_LOGLEVEL_WARNING 1
+#define FLIB_LOGLEVEL_ERROR 2
+#define FLIB_LOGLEVEL_NONE 100
 
 char* flib_format_ip(uint32_t numip);
 
 void flib_log_e(const char *fmt, ...);
 void flib_log_w(const char *fmt, ...);
 void flib_log_i(const char *fmt, ...);
+void flib_log_d(const char *fmt, ...);
+
+int flib_log_getLevel();
+void flib_log_setLevel(int level);
+void flib_log_setFile(FILE *logfile);
 
 #endif /* LOGGING_H_ */