Navigation


Changeset 941:64088de91f7f in freeDiameter


Ignore:
Timestamp:
Jan 20, 2013, 1:42:12 AM (11 years ago)
Author:
Sebastien Decugis <sdecugis@freediameter.net>
Branch:
default
Phase:
public
Message:

Initial logging rework by Thomas

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/freeDiameter/libfdproto.h

    r907 r941  
    110110
    111111/*
    112  * FUNCTION:    fd_log_debug_fstr
     112 * FUNCTION:    fd_log
    113113 * MACRO:       fd_log_debug
    114114 *
    115115 * PARAMETERS:
    116  *  fstr        : Stream where the text will be sent (default: stdout)
     116 *  loglevel    : Integer, how important the message is
    117117 *  format      : Same format string as in the printf function
    118118 *  ...         : Same list as printf
    119119 *
    120120 * DESCRIPTION:
    121  *  Log internal information for use of developpers only.
     121 * Write information to log.
    122122 * The format and arguments may contain UTF-8 encoded data. The
    123  * output medium (file or console) is expected to support this encoding.
     123 * output medium is expected to support this encoding.
    124124 *
    125125 * RETURN VALUE:
    126126 *  None.
    127127 */
    128 void fd_log_debug_fstr ( FILE * fstr, const char * format, ... );
    129 #define fd_log_debug(format,args...) fd_log_debug_fstr(NULL, format, ## args)
     128void fd_log ( int, const char *, ... );
     129#define fd_log_debug(format,args...) fd_log(FD_LOG_DEBUG, format, ## args)
     130void fd_log_debug_fstr( FILE *, const char *, ... );
    130131
    131132/* these are internal objects of the debug facility,
     
    174175 *
    175176 * PARAMETERS:
    176  *  fstr        : Stream where the text will be sent to (default: stdout)
     177 *  loglevel    : priority of the message
    177178 *  format      : Same format string as in the printf function
    178179 *  va_list     : Argument list
     
    184185 * int          : Success or failure
    185186 */
    186 int fd_log_handler_register ( void (*logger)(const char * format, va_list *args) );
     187int fd_log_handler_register ( void (*logger)(int loglevel, const char * format, va_list args) );
    187188
    188189/*
     
    209210#endif /* ASSERT */
    210211
    211 /* levels definitions */
     212/* log levels definitions */
     213#define FD_LOG_DEBUG 0
     214#define FD_LOG_ERROR 5
     215
     216/* print level definitions */
    212217#define NONE 0  /* Display no debug message */
    213218#define INFO 1  /* Display errors only */
     
    264269                char __buf[25];                                                                                                 \
    265270                const char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed");                                 \
    266                 fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n"                                                                \
    267                           "\t%s|%*s" format "\n",                                                                               \
     271                fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n"  \
     272                          "\t%s|%*s" format,                                                                            \
    268273                                        __thn, fd_log_time(NULL, __buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__,\
    269274                                        (level < FULL)?"@":" ",level, "", ## args);                                             \
     
    277282                        char __buf[25];                                                                                                 \
    278283                        const char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed");                                 \
    279                         fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n"                                                                \
    280                                   "\t%s|%*s" format "\n",                                                                               \
     284                        fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n" \
     285                                  "\t%s|%*s" format,                                                                            \
    281286                                                __thn, fd_log_time(NULL, __buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__,\
    282287                                                (level < FULL)?"@":" ",level, "", ## args);                                             \
    283288                } else {                                                                                                                \
    284                         fd_log_debug(format "\n", ## args);                                                                             \
     289                        fd_log(level, format, ## args);           \
    285290                }                                                                                                                       \
    286291        }                                                                                                                               \
     
    321326                uint8_t * __buf = (uint8_t *)(buf);                                                                             \
    322327                char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed");                                       \
    323                 fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n"                                                                \
     328                fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n"  \
    324329                          "\t%s|%*s" prefix ,                                                                                   \
    325330                                        __thn, fd_log_time(NULL, __ts, sizeof(__ts)), __PRETTY_FUNCTION__, __FILE__, __LINE__,  \
    326331                                        (level < FULL)?"@":" ",level, "");                                                      \
    327332                for (__i = 0; __i < __sz; __i++) {                                                                              \
    328                         fd_log_debug("%02.2hhx", __buf[__i]);                                                                   \
     333                        fd_log(level, "%02.2hhx", __buf[__i]);         \
    329334                }                                                                                                               \
    330                 fd_log_debug(suffix "\n");                                                                                      \
     335                fd_log(level, suffix);                        \
    331336        }                                                                                                                       \
    332337}
     
    357362                        flag);                                  \
    358363          if (__rc)                                             \
    359                 fd_log_debug("%s", (char *)gai_strerror(__rc)); \
     364                fd_log_debug("%s", (char *)gai_strerror(__rc)); \
    360365          else                                                  \
    361                 fd_log_debug("%s", &__addrbuf[0]);              \
     366                fd_log_debug("%s", &__addrbuf[0]);              \
    362367        } else {                                                \
    363                 fd_log_debug("(NULL / ANY)");                   \
     368                fd_log_debug("(NULL / ANY)");                   \
    364369        }                                                       \
    365370}
     
    378383                        flag);                                          \
    379384          if (__rc)                                                     \
    380                 fd_log_debug("%s", (char *)gai_strerror(__rc));         \
     385                fd_log_debug("%s", (char *)gai_strerror(__rc));         \
    381386          else                                                          \
    382                 fd_log_debug("[%s]:%s", &__addrbuf[0],&__servbuf[0]);   \
     387                fd_log_debug("[%s]:%s", &__addrbuf[0],&__servbuf[0]);   \
    383388        } else {                                                        \
    384389                fd_log_debug("(NULL / ANY)");                           \
     
    391396                char __buf[25];                                                                                                 \
    392397                char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed");                                       \
    393                 fd_log_debug("\t | tid:%-20s\t%s\tin %s@%s:%d\n"                                                                \
     398                fd_log(level, "\t | tid:%-20s\t%s\tin %s@%s:%d\n"  \
    394399                          "\t%s|%*s" prefix ,                                                                                   \
    395400                                        __thn, fd_log_time(NULL, __buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__,\
    396401                                        (level < FULL)?"@":" ",level, "");                                                      \
    397402                sSA_DUMP_NODE_SERV( sa, flags );                                                                                \
    398                 fd_log_debug(suffix "\n");                                                                                      \
     403                fd_log(level, suffix);                        \
    399404        }                                                                                                                       \
    400405}
     
    418423#define TRACE_DEBUG_sSA(level, prefix, sa, flags, suffix )
    419424#define TRACE_DEBUG_ERROR(format,args... ) {    \
    420         fd_log_debug(format "\n", ## args);     \
     425        fd_log(FD_LOG_ERROR, format, ## args);  \
    421426}
    422427#endif /* STRIP_DEBUG_CODE */
  • libfdproto/log.c

    r909 r941  
    4444int fd_g_debug_lvl = INFO;
    4545
     46static void fd_internal_logger( int, const char *, va_list );
     47
    4648/* These may be used to pass specific debug requests via the command-line parameters */
    4749char * fd_debug_one_function = NULL;
     
    5355
    5456/* Allow passing of the log and debug information from base stack to extensions */
    55 void (*fd_external_logger)( const char * format, va_list *args ) = NULL;
     57void (*fd_logger)( int loglevel, const char * format, va_list args ) = fd_internal_logger;
    5658
    57 /* Register an dexternal call back for tracing and debug */
    58 int fd_log_handler_register( void (*logger)(const char * format, va_list *args))
     59/* Register an external call back for tracing and debug */
     60int fd_log_handler_register( void (*logger)(int loglevel, const char * format, va_list args) )
    5961{
    6062        CHECK_PARAMS( logger );
    6163
    62         if ( fd_external_logger != NULL )
     64        if ( fd_logger != fd_internal_logger )
    6365        {
    6466               return EALREADY; /* only one registration allowed */
     
    6668        else
    6769        {
    68                fd_external_logger = logger;
     70               fd_logger = logger;
    6971        }
     72
    7073        return 0;
    7174}
     
    7477int fd_log_handler_unregister ( void )
    7578{
    76         fd_external_logger = NULL;
     79        fd_logger = fd_internal_logger;
    7780        return 0; /* Successfull in all cases. */
    7881}
     
    8386}
    8487
     88
     89static void fd_internal_logger( int loglevel, const char *format, va_list ap )
     90{
     91    FILE *fstr = fd_g_debug_fstr ?: stdout;
     92
     93    /* logging has been decided by macros outside already */
     94    vfprintf( fd_g_debug_fstr, format, ap);
     95    fprintf(fd_g_debug_fstr, "\n");
     96    fflush(fd_g_debug_fstr);
     97}
     98
    8599/* Log a debug message */
    86 void fd_log_debug_fstr ( FILE * fstr, const char * format, ... )
     100void fd_log ( int loglevel, const char * format, ... )
    87101{
    88102        va_list ap;
     
    93107       
    94108        va_start(ap, format);
    95         if ( fd_external_logger != NULL )
    96         {
    97                fd_external_logger( format, &ap );
    98         }
    99         else
    100         {
    101                vfprintf( fstr ?: stdout, format, ap);
    102                fflush(fstr ?: stdout);
    103         }
     109        fd_logger(loglevel, format, ap);
    104110        va_end(ap);
    105111
     
    107113       
    108114        (void)pthread_mutex_unlock(&fd_log_lock);
     115}
     116
     117/* Log debug message to file. */
     118void fd_log_debug_fstr( FILE * fstr, const char * format, ... )
     119{
     120        va_list ap;
     121       
     122        va_start(ap, format);
     123        vfprintf(fstr, format, ap);
     124        va_end(ap);
    109125}
    110126
Note: See TracChangeset for help on using the changeset viewer.