# HG changeset patch # User Sebastien Decugis # Date 1333048773 -7200 # Node ID e87d083d0342b2761655e0e574a25236d2198f9e # Parent 229d7dee6a27b7b05fe09784d6971351764dda4b Added ability to register another log function; thanks to Zack for the code diff -r 229d7dee6a27 -r e87d083d0342 include/freeDiameter/libfdproto.h --- 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 */ diff -r 229d7dee6a27 -r e87d083d0342 libfdproto/log.c --- 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);