Allow switching between different log methods at runtime.
authorAndre Noll <maan@systemlinux.org>
Mon, 29 Aug 2011 17:17:38 +0000 (19:17 +0200)
committerAndre Noll <maan@systemlinux.org>
Sun, 20 Nov 2011 14:08:41 +0000 (15:08 +0100)
Currently, para_client defines its para_log function via the
INIT_STDERR_LOGGING() macro which generates a log function that
writes to stderr. However, we will need to switch to a different,
curses-aware logging function when operating in interactive mode.

To support more than one log method the type of para_log is changed
from a function to a (public) pointer variable. This variable is
supposed to point to the log function currently in use so that the
application can simply set para_log differently in order to switch
between log functions.

The patch also changes the INIT_STDERR_LOGGING() macro to receive the
name of the log function to be defined and to let para_log point to
the newly defined function.

audiod.c
daemon.c
daemon.h
fade.c
gui.c
para.h
server.c

index 8008e2cb6459f84d3b258bd9efeb0fffc77e4016..7085ef0f4da3e303df923c4ea6dd1b705911968d 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -33,6 +33,7 @@
 #include "signal.h"
 #include "version.h"
 
 #include "signal.h"
 #include "version.h"
 
+__printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
 /** define the array of error lists needed by para_audiod */
 INIT_AUDIOD_ERRLISTS;
 /** define the array containing all supported audio formats */
 /** define the array of error lists needed by para_audiod */
 INIT_AUDIOD_ERRLISTS;
 /** define the array containing all supported audio formats */
index 0bf2f0ac9e81d72dff8623d7ebc54014007c40e4..f06cf2f5a12d57015f3b66982a9aaab4f7e96622 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -357,7 +357,7 @@ __malloc char *get_server_uptime_str(const struct timeval *current_time)
  * \param ll The log level.
  * \param fmt The format string describing the log message.
  */
  * \param ll The log level.
  * \param fmt The format string describing the log message.
  */
-__printf_2_3 void para_log(int ll, const char* fmt,...)
+__printf_2_3 void daemon_log(int ll, const char* fmt,...)
 {
        va_list argp;
        FILE *fp;
 {
        va_list argp;
        FILE *fp;
index 3fe72ea9149a56af3a9b8fdfe9c9f5b493962f94..fd435577e6258aa24fe2fe1edc9bde2ce50c7d5f 100644 (file)
--- a/daemon.h
+++ b/daemon.h
@@ -16,6 +16,7 @@ void daemon_clear_flag(unsigned flag);
 void daemon_set_loglevel(char *loglevel);
 void daemon_set_default_log_colors(void);
 void daemon_set_log_color_or_die(char const *arg);
 void daemon_set_loglevel(char *loglevel);
 void daemon_set_default_log_colors(void);
 void daemon_set_log_color_or_die(char const *arg);
+__printf_2_3 void daemon_log(int ll, const char* fmt,...);
 
 /** Daemon log configuration flags. */
 enum daemon_flags {
 
 /** Daemon log configuration flags. */
 enum daemon_flags {
diff --git a/fade.c b/fade.c
index 04ca923770ec069bda49d3a069fac795fd461ac1..1aced9df5ad732aa4208dcbd87105b555425303b 100644 (file)
--- a/fade.c
+++ b/fade.c
@@ -27,7 +27,7 @@
 INIT_FADE_ERRLISTS;
 static struct fade_args_info conf;
 
 INIT_FADE_ERRLISTS;
 static struct fade_args_info conf;
 
-__printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
+__printf_2_3 void date_log(__a_unused int ll, const char *fmt, ...)
 {
        va_list argp;
        time_t t1;
 {
        va_list argp;
        time_t t1;
@@ -40,6 +40,7 @@ __printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
        vprintf(fmt, argp);
        va_end(argp);
 }
        vprintf(fmt, argp);
        va_end(argp);
 }
+__printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
 
 /*
  * Open the mixer device.
 
 /*
  * Open the mixer device.
diff --git a/gui.c b/gui.c
index a3c07ec796bd9d179ef633b3d81d91c14b42c834..3fc81965233ca2983cb7ae3c33238a6544ef87c9 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -489,7 +489,7 @@ static int add_output_line(char *line, __a_unused void *data)
 
 static int loglevel;
 
 
 static int loglevel;
 
-__printf_2_3 void para_log(int ll, const char *fmt,...)
+__printf_2_3 void curses_log(int ll, const char *fmt,...)
 {
        int color;
        char *msg;
 {
        int color;
        char *msg;
@@ -510,6 +510,7 @@ __printf_2_3 void para_log(int ll, const char *fmt,...)
        rb_add_entry(color, msg);
        wrefresh(bot.win);
 }
        rb_add_entry(color, msg);
        wrefresh(bot.win);
 }
+__printf_2_3 void (*para_log)(int, const char*, ...) = curses_log;
 
 static void setup_signal_handling(void)
 {
 
 static void setup_signal_handling(void)
 {
diff --git a/para.h b/para.h
index bc6aa929539ee4a9c1abb8e0d2b14695d88870cc..96492baaf35ce666c37e31c6a60e8af5a6573333 100644 (file)
--- a/para.h
+++ b/para.h
        typeof(x) _x = (x); \
        _x > 0? _x : -_x; })
 
        typeof(x) _x = (x); \
        _x > 0? _x : -_x; })
 
+
+extern __printf_2_3 void (*para_log)(int, const char*, ...);
 /**
  * Define a standard log function that always writes to stderr.
  *
 /**
  * Define a standard log function that always writes to stderr.
  *
+ * \param funcname The name of the function to be defined.
+ *
  * \param loglevel_barrier If the loglevel of the current message
  * is less than that, the message is going to be ignored.
  *
  */
  * \param loglevel_barrier If the loglevel of the current message
  * is less than that, the message is going to be ignored.
  *
  */
-#define INIT_STDERR_LOGGING(loglevel_barrier) \
-       __printf_2_3 void para_log(int ll, const char* fmt,...) \
+#define DEFINE_STDERR_LOGGER(funcname, loglevel_barrier) \
+       __printf_2_3 void funcname(int ll, const char* fmt,...) \
        { \
                va_list argp; \
                if (ll < loglevel_barrier) \
        { \
                va_list argp; \
                if (ll < loglevel_barrier) \
@@ -66,6 +70,9 @@
                vfprintf(stderr, fmt, argp); \
                va_end(argp); \
        }
                vfprintf(stderr, fmt, argp); \
                va_end(argp); \
        }
+#define INIT_STDERR_LOGGING(loglevel_barrier) \
+       DEFINE_STDERR_LOGGER(stderr_log, loglevel_barrier); \
+       __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
 
 /** Sent by para_client to initiate the authentication procedure. */
 #define AUTH_REQUEST_MSG "auth rsa "
 
 /** Sent by para_client to initiate the authentication procedure. */
 #define AUTH_REQUEST_MSG "auth rsa "
@@ -105,8 +112,6 @@ extern const char *status_item_list[];
 int for_each_stat_item(char *item_buf, size_t num_bytes,
        int (*item_handler)(int, char *));
 
 int for_each_stat_item(char *item_buf, size_t num_bytes,
        int (*item_handler)(int, char *));
 
-__printf_2_3 void para_log(int, const char*, ...);
-
 /**
  * Write a log message to a dynamically allocated string.
  *
 /**
  * Write a log message to a dynamically allocated string.
  *
index 7f020c8f73f34674a0ff644aa84e73a9096962b1..4650f9947f01ed7fd6f1323610090ae16ef22ac4 100644 (file)
--- a/server.c
+++ b/server.c
@@ -92,6 +92,8 @@
 #include "color.h"
 #include "version.h"
 
 #include "color.h"
 #include "version.h"
 
+__printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
+
 /** Define the array of error lists needed by para_server. */
 INIT_SERVER_ERRLISTS;
 
 /** Define the array of error lists needed by para_server. */
 INIT_SERVER_ERRLISTS;