From: Michel Pollet <buserror@gmail.com>
Date: Wed, 27 Nov 2013 21:16:36 +0000 (+0000)
Subject: simavr: Removed the avr_global_logger global
X-Git-Tag: v1.2~51
X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=270166e66646ce916f091abb980e59303a042dc5;p=sx%2Fsimavr.git

simavr: Removed the avr_global_logger global

It was not very clean; Instead there is a new accessor for
setting/getting a global logging function pointer.
Also made sure it is not compiled for the cores, since they use AVR
headers and have no stdarg.h anyway

Signed-off-by: Michel Pollet <buserror@gmail.com>
---

diff --git a/Makefile.common b/Makefile.common
index d44058b..3976b65 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -41,7 +41,7 @@ ARCH 		= ${shell $(CC) -dumpmachine | sed -e 's/^x/i/' -e 's/\(.\).*/\1/'}
 
 CFLAGS		+= -O2 -Wall
 CFLAGS		+= -g
-CORE_CFLAGS	= -nostdinc
+CORE_CFLAGS	= -nostdinc -DAVR_CORE=1
 
 ifeq (${shell uname}, Darwin)
 # gcc 4.2 from MacOS is really not up to scratch anymore 
diff --git a/simavr/sim/sim_avr.c b/simavr/sim/sim_avr.c
index 894bc0b..fe53d18 100644
--- a/simavr/sim/sim_avr.c
+++ b/simavr/sim/sim_avr.c
@@ -35,8 +35,36 @@
 #define AVR_KIND_DECL
 #include "sim_core_decl.h"
 
-static void std_logger(avr_t * avr, const int level, const char * format, ...);
-avr_logger_p avr_global_logger = std_logger;
+static void std_logger(avr_t * avr, const int level, const char * format, va_list ap);
+static avr_logger_p _avr_global_logger = std_logger;
+
+void
+avr_global_logger(
+		struct avr_t* avr, 
+		const int level, 
+		const char * format, 
+		... )
+{
+	va_list args;
+	va_start(args, format);
+	if (_avr_global_logger)
+		_avr_global_logger(avr, level, format, args);
+	va_end(args);	
+}
+
+void
+avr_global_logger_set(
+		avr_logger_p logger)
+{
+	_avr_global_logger = logger ? logger : std_logger;
+}
+
+avr_logger_p
+avr_global_logger_get()
+{
+	return _avr_global_logger;
+}
+
 
 int avr_init(avr_t * avr)
 {
@@ -353,13 +381,10 @@ avr_make_mcu_by_name(
 	return avr;
 }
 
-static void std_logger(avr_t* avr, const int level, const char * format, ...)
+static void std_logger(avr_t* avr, const int level, const char * format, va_list ap)
 {
 	if (!avr || avr->log >= level) {
-		va_list args;
-		va_start(args, format);
-		vfprintf((level > LOG_ERROR) ?  stdout : stderr , format, args);
-		va_end(args);
+		vfprintf((level > LOG_ERROR) ?  stdout : stderr , format, ap);
 	}
 }
 
diff --git a/simavr/sim/sim_avr.h b/simavr/sim/sim_avr.h
index dde8873..00bfb3a 100644
--- a/simavr/sim/sim_avr.h
+++ b/simavr/sim/sim_avr.h
@@ -72,8 +72,7 @@ enum {
 	LOG_TRACE,
 };
 
-typedef void (*avr_logger_p)(struct avr_t* avr, const int level, const char * format, ... );
-extern avr_logger_p avr_global_logger;
+
 #ifndef AVR_LOG
 #define AVR_LOG(avr, level, ...) \
 	do { \
@@ -373,6 +372,31 @@ avr_sadly_crashed(
 		avr_t *avr,
 		uint8_t signal);
 
+/*
+ * Logs a message using the current logger
+ */
+void
+avr_global_logger(
+		struct avr_t* avr, 
+		const int level, 
+		const char * format, 
+		... );
+
+#ifndef AVR_CORE
+#include <stdarg.h>
+/*
+ * Type for custom logging functions
+ */
+typedef void (*avr_logger_p)(struct avr_t* avr, const int level, const char * format, va_list ap);
+
+/* Sets a global logging function in place of the default */
+void
+avr_global_logger_set(
+		avr_logger_p logger);
+/* Gets the current global logger function */
+avr_logger_p
+avr_global_logger_get();
+#endif
 
 /*
  * These are callbacks for the two 'main' behaviour in simavr