changeset 784:e87d083d0342

Added ability to register another log function; thanks to Zack for the code
author Sebastien Decugis <sdecugis@nict.go.jp>
date Thu, 29 Mar 2012 21:19:33 +0200
parents 229d7dee6a27
children e780452c6b55
files include/freeDiameter/libfdproto.h libfdproto/log.c
diffstat 2 files changed, 66 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/include/freeDiameter/libfdproto.h	Fri Mar 02 08:13:34 2012 +0100
+++ b/include/freeDiameter/libfdproto.h	Thu Mar 29 21:19:33 2012 +0200
@@ -154,6 +154,37 @@
  */
 char * fd_log_time ( struct timespec * ts, char * buf, size_t len );
 
+/*
+ * FUNCTION:    fd_log_handler_register
+ * MACRO:
+ *
+ * PARAMETERS:
+ *  fstr        : Stream where the text will be sent (default: stdout)
+ *  format      : Same format string as in the printf function
+ *  va_list     : Argument list
+ *
+ * DESCRIPTION:
+ * Register an external method for logging purposes.
+ *
+ * RETURN VALUE:
+ * int          : Success or failure
+ */
+int fd_log_handler_register ( void (*logger)(const char * format, va_list *args) );
+
+/*
+ * FUNCTION:    fd_log_handler_unregister
+ * MACRO:
+ *
+ * PARAMETERS:
+ *
+ * DESCRIPTION:
+ * Unregister the external logging function.
+ *
+ * RETURN VALUE:
+ * int          : Success or failure
+ */
+int fd_log_handler_unregister ( void );
+
 
 /*============================================================*/
 /*                    DEBUG MACROS                            */
--- a/libfdproto/log.c	Fri Mar 02 08:13:34 2012 +0100
+++ b/libfdproto/log.c	Thu Mar 29 21:19:33 2012 +0200
@@ -49,6 +49,32 @@
 int fd_breaks = 0;
 int fd_breakhere(void) { return ++fd_breaks; }
 
+/* Allow passing of the log and debug information from base stack to extensions */
+void (*fd_external_logger)( const char * format, va_list *args ) = NULL;
+
+/* Register an dexternal call back for tracing and debug */
+int fd_log_handler_register( void (*logger)(const char * format, va_list *args))
+{
+        CHECK_PARAMS( logger );
+
+        if ( fd_external_logger != NULL )
+        {
+               return EALREADY; /* only one registeration allowed */
+        }
+        else
+        {
+               fd_external_logger = logger;
+        }
+        return 0;
+}
+
+/* Implement a simple reset function here */
+int fd_log_handler_unregister ( void )
+{
+        fd_external_logger = NULL;
+        return 0; /* Successfull in all cases. */
+}
+
 static void fd_cleanup_mutex_silent( void * mutex )
 {
 	(void)pthread_mutex_unlock((pthread_mutex_t *)mutex);
@@ -64,9 +90,16 @@
 	pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock);
 	
 	va_start(ap, format);
-	vfprintf( fstr ?: stdout, format, ap);
+        if ( fd_external_logger != NULL )
+        {
+               fd_external_logger( format, &ap );
+        }
+        else
+        {
+               vfprintf( fstr ?: stdout, format, ap);
+               fflush(fstr ?: stdout);
+        }
 	va_end(ap);
-	fflush(fstr ?: stdout);
 
 	pthread_cleanup_pop(0);
 	
"Welcome to our mercurial repository"