# HG changeset patch # User Sebastien Decugis # Date 1358613732 -3600 # Node ID 64088de91f7fa1f1a4181e5e248e333cffaf9b4f # Parent 8500947421cb412b5f4f2dfffdf1a35b6fb369e0 Initial logging rework by Thomas diff -r 8500947421cb -r 64088de91f7f include/freeDiameter/libfdproto.h --- a/include/freeDiameter/libfdproto.h Sat Jan 19 17:12:25 2013 +0100 +++ b/include/freeDiameter/libfdproto.h Sat Jan 19 17:42:12 2013 +0100 @@ -109,24 +109,25 @@ /* - * FUNCTION: fd_log_debug_fstr + * FUNCTION: fd_log * MACRO: fd_log_debug * * PARAMETERS: - * fstr : Stream where the text will be sent (default: stdout) + * loglevel : Integer, how important the message is * format : Same format string as in the printf function * ... : Same list as printf * * DESCRIPTION: - * Log internal information for use of developpers only. + * Write information to log. * The format and arguments may contain UTF-8 encoded data. The - * output medium (file or console) is expected to support this encoding. + * output medium is expected to support this encoding. * * RETURN VALUE: * None. */ -void fd_log_debug_fstr ( FILE * fstr, const char * format, ... ); -#define fd_log_debug(format,args...) fd_log_debug_fstr(NULL, format, ## args) +void fd_log ( int, const char *, ... ); +#define fd_log_debug(format,args...) fd_log(FD_LOG_DEBUG, format, ## args) +void fd_log_debug_fstr( FILE *, const char *, ... ); /* these are internal objects of the debug facility, might be useful to control the behavior from outside */ @@ -173,7 +174,7 @@ * MACRO: * * PARAMETERS: - * fstr : Stream where the text will be sent to (default: stdout) + * loglevel : priority of the message * format : Same format string as in the printf function * va_list : Argument list * @@ -183,7 +184,7 @@ * RETURN VALUE: * int : Success or failure */ -int fd_log_handler_register ( void (*logger)(const char * format, va_list *args) ); +int fd_log_handler_register ( void (*logger)(int loglevel, const char * format, va_list args) ); /* * FUNCTION: fd_log_handler_unregister @@ -208,7 +209,11 @@ #define ASSERT(x) assert(x) #endif /* ASSERT */ -/* levels definitions */ +/* log levels definitions */ +#define FD_LOG_DEBUG 0 +#define FD_LOG_ERROR 5 + +/* print level definitions */ #define NONE 0 /* Display no debug message */ #define INFO 1 /* Display errors only */ #define FULL 2 /* Display additional information to follow code execution */ @@ -263,8 +268,8 @@ if ( TRACE_BOOL(level) ) { \ char __buf[25]; \ const char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed"); \ - fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ - "\t%s|%*s" format "\n", \ + fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ + "\t%s|%*s" format, \ __thn, fd_log_time(NULL, __buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__,\ (level < FULL)?"@":" ",level, "", ## args); \ } \ @@ -276,12 +281,12 @@ if (fd_g_debug_lvl > FULL) { \ char __buf[25]; \ const char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed"); \ - fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ - "\t%s|%*s" format "\n", \ + fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ + "\t%s|%*s" format, \ __thn, fd_log_time(NULL, __buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__,\ (level < FULL)?"@":" ",level, "", ## args); \ } else { \ - fd_log_debug(format "\n", ## args); \ + fd_log(level, format, ## args); \ } \ } \ } @@ -320,14 +325,14 @@ size_t __sz = (size_t)(bufsz); \ uint8_t * __buf = (uint8_t *)(buf); \ char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed"); \ - fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ + fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ "\t%s|%*s" prefix , \ __thn, fd_log_time(NULL, __ts, sizeof(__ts)), __PRETTY_FUNCTION__, __FILE__, __LINE__, \ (level < FULL)?"@":" ",level, ""); \ for (__i = 0; __i < __sz; __i++) { \ - fd_log_debug("%02.2hhx", __buf[__i]); \ + fd_log(level, "%02.2hhx", __buf[__i]); \ } \ - fd_log_debug(suffix "\n"); \ + fd_log(level, suffix); \ } \ } @@ -356,11 +361,11 @@ 0, \ flag); \ if (__rc) \ - fd_log_debug("%s", (char *)gai_strerror(__rc)); \ + fd_log_debug("%s", (char *)gai_strerror(__rc)); \ else \ - fd_log_debug("%s", &__addrbuf[0]); \ + fd_log_debug("%s", &__addrbuf[0]); \ } else { \ - fd_log_debug("(NULL / ANY)"); \ + fd_log_debug("(NULL / ANY)"); \ } \ } /* Same but with the port (service) also */ @@ -377,9 +382,9 @@ sizeof(__servbuf), \ flag); \ if (__rc) \ - fd_log_debug("%s", (char *)gai_strerror(__rc)); \ + fd_log_debug("%s", (char *)gai_strerror(__rc)); \ else \ - fd_log_debug("[%s]:%s", &__addrbuf[0],&__servbuf[0]); \ + fd_log_debug("[%s]:%s", &__addrbuf[0],&__servbuf[0]); \ } else { \ fd_log_debug("(NULL / ANY)"); \ } \ @@ -390,12 +395,12 @@ if ( TRACE_BOOL(level) ) { \ char __buf[25]; \ char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed"); \ - fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ + fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n" \ "\t%s|%*s" prefix , \ __thn, fd_log_time(NULL, __buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__,\ (level < FULL)?"@":" ",level, ""); \ sSA_DUMP_NODE_SERV( sa, flags ); \ - fd_log_debug(suffix "\n"); \ + fd_log(level, suffix); \ } \ } @@ -417,7 +422,7 @@ #define TRACE_DEBUG_BUFFER(level, prefix, buf, bufsz, suffix ) #define TRACE_DEBUG_sSA(level, prefix, sa, flags, suffix ) #define TRACE_DEBUG_ERROR(format,args... ) { \ - fd_log_debug(format "\n", ## args); \ + fd_log(FD_LOG_ERROR, format, ## args); \ } #endif /* STRIP_DEBUG_CODE */ diff -r 8500947421cb -r 64088de91f7f libfdproto/log.c --- a/libfdproto/log.c Sat Jan 19 17:12:25 2013 +0100 +++ b/libfdproto/log.c Sat Jan 19 17:42:12 2013 +0100 @@ -43,6 +43,8 @@ pthread_key_t fd_log_thname; int fd_g_debug_lvl = INFO; +static void fd_internal_logger( int, const char *, va_list ); + /* These may be used to pass specific debug requests via the command-line parameters */ char * fd_debug_one_function = NULL; char * fd_debug_one_file = NULL; @@ -52,28 +54,29 @@ 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; +void (*fd_logger)( int loglevel, const char * format, va_list args ) = fd_internal_logger; -/* Register an dexternal call back for tracing and debug */ -int fd_log_handler_register( void (*logger)(const char * format, va_list *args)) +/* Register an external call back for tracing and debug */ +int fd_log_handler_register( void (*logger)(int loglevel, const char * format, va_list args) ) { CHECK_PARAMS( logger ); - if ( fd_external_logger != NULL ) + if ( fd_logger != fd_internal_logger ) { return EALREADY; /* only one registration allowed */ } else { - fd_external_logger = logger; + fd_logger = logger; } + return 0; } /* Implement a simple reset function here */ int fd_log_handler_unregister ( void ) { - fd_external_logger = NULL; + fd_logger = fd_internal_logger; return 0; /* Successfull in all cases. */ } @@ -82,8 +85,19 @@ (void)pthread_mutex_unlock((pthread_mutex_t *)mutex); } + +static void fd_internal_logger( int loglevel, const char *format, va_list ap ) +{ + FILE *fstr = fd_g_debug_fstr ?: stdout; + + /* logging has been decided by macros outside already */ + vfprintf( fd_g_debug_fstr, format, ap); + fprintf(fd_g_debug_fstr, "\n"); + fflush(fd_g_debug_fstr); +} + /* Log a debug message */ -void fd_log_debug_fstr ( FILE * fstr, const char * format, ... ) +void fd_log ( int loglevel, const char * format, ... ) { va_list ap; @@ -92,15 +106,7 @@ pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock); va_start(ap, format); - if ( fd_external_logger != NULL ) - { - fd_external_logger( format, &ap ); - } - else - { - vfprintf( fstr ?: stdout, format, ap); - fflush(fstr ?: stdout); - } + fd_logger(loglevel, format, ap); va_end(ap); pthread_cleanup_pop(0); @@ -108,6 +114,16 @@ (void)pthread_mutex_unlock(&fd_log_lock); } +/* Log debug message to file. */ +void fd_log_debug_fstr( FILE * fstr, const char * format, ... ) +{ + va_list ap; + + va_start(ap, format); + vfprintf(fstr, format, ap); + va_end(ap); +} + /* Function to set the thread's friendly name */ void fd_log_threadname ( const char * name ) {