src/lib/log.h
changeset 16 74fb62022fb3
parent 11 a4e382d4a22a
child 24 82cfdb6680d1
equal deleted inserted replaced
15:a8d183e79ed9 16:74fb62022fb3
     3 
     3 
     4 /*
     4 /*
     5  * error handling
     5  * error handling
     6  */
     6  */
     7 
     7 
     8 void _generic_err ( /*int level, */ int use_stderr, const char *func, int perr, const char *fmt, ...)
     8 enum log_display_flags {
       
     9     LOG_DISPLAY_STDOUT =    0x00,
       
    10     LOG_DISPLAY_STDERR =    0x01,
       
    11 
       
    12     LOG_DISPLAY_PERR =      0x02,
       
    13 
       
    14     LOG_DISPLAY_NONL =      0x04,
       
    15 };
       
    16 
       
    17 
       
    18 void _generic_err (int flags, const char *func, int err, const char *fmt, ...)
     9         __attribute__ ((format (printf, 4, 5)));
    19         __attribute__ ((format (printf, 4, 5)));
    10 
    20 
    11 // needs to be defined as its own function for the noreturn attribute
    21 // needs to be defined as its own function for the noreturn attribute
    12 void _generic_err_exit ( /* int level, */ int used_stderr, const char *func, int perr, const char *fmt, ...)
    22 void _generic_err_exit (int flags, const char *func, int err, const char *fmt, ...)
    13         __attribute__ ((format (printf, 4, 5)))
    23         __attribute__ ((format (printf, 4, 5)))
    14         __attribute__ ((noreturn));
    24         __attribute__ ((noreturn));
    15 
    25 
    16 enum _debug_level {
    26 enum _debug_level {
    17     DEBUG_FATAL,
    27     DEBUG_FATAL,
    23 
    33 
    24 // not currently used
    34 // not currently used
    25 extern enum _debug_level _cur_debug_level;
    35 extern enum _debug_level _cur_debug_level;
    26 
    36 
    27 // various kinds of ways to handle an error, 2**3 of them, *g*
    37 // various kinds of ways to handle an error, 2**3 of them, *g*
    28 #define info(...)                   _generic_err(       0,  NULL,   0,  __VA_ARGS__ )
    38 #define info(...)                   _generic_err(       LOG_DISPLAY_STDOUT,                     NULL, 0,    __VA_ARGS__ )
    29 #define error(...)                  _generic_err(       1,  NULL,   0,  __VA_ARGS__ )
    39 #define error(...)                  _generic_err(       LOG_DISPLAY_STDERR,                     NULL, 0,    __VA_ARGS__ )
    30 #define err_exit(...)               _generic_err_exit(  1,  NULL,   0,  __VA_ARGS__ )
    40 #define err_exit(...)               _generic_err_exit(  LOG_DISPLAY_STDERR,                     NULL, 0,    __VA_ARGS__ )
    31 #define perr(...)                   _generic_err(       1,  NULL,   1,  __VA_ARGS__ )
    41 #define perr(...)                   _generic_err(       LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR,  NULL, 0,    __VA_ARGS__ )
    32 #define perr_exit(...)              _generic_err_exit(  1,  NULL,   1,  __VA_ARGS__ )
    42 #define perr_exit(...)              _generic_err_exit(  LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR,  NULL, 0,    __VA_ARGS__ )
    33 #define err_func(func, ...)         _generic_err(       1,  func,   0,  __VA_ARGS__ )
    43 #define err_func(func, ...)         _generic_err(       LOG_DISPLAY_STDERR,                     func, 0,    __VA_ARGS__ )
    34 #define err_func_exit(func, ...)    _generic_err_exit(  1,  func,   0,  __VA_ARGS__ )
    44 #define err_func_exit(func, ...)    _generic_err_exit(  LOG_DISPLAY_STDERR,                     func, 0,    __VA_ARGS__ )
    35 #define perr_func(func, ...)        _generic_err(       1,  func,   1,  __VA_ARGS__ )
    45 #define perr_func(func, ...)        _generic_err(       LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR,  func, 0,    __VA_ARGS__ )
    36 #define perr_func_exit(func, ...)   _generic_err_exit(  1,  func,   1,  __VA_ARGS__ )
    46 #define perr_func_exit(func, ...)   _generic_err_exit(  LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR,  func, 0,    __VA_ARGS__ )
    37 #define eerr_func(func, err, ...)   _generic_err(       1,  func,   err,__VA_ARGS__ )
    47 #define eerr_func(func, err, ...)   _generic_err(       LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR,  func, err,  __VA_ARGS__ )
       
    48 #define debug(func, ...)            _generic_err(       LOG_DISPLAY_STDERR,                     func, 0,    __VA_ARGS__ )
       
    49 #define debug_nonl(func, ...)       _generic_err(       LOG_DISPLAY_STDERR | LOG_DISPLAY_NONL,  func, 0,    __VA_ARGS__ )
    38 
    50 
    39 /*
    51 // logging includes errors
    40  * Legacy...
       
    41  */
       
    42 #include "error.h"
    52 #include "error.h"
    43 
    53 
    44 #define WARNING(...) err_func(__func__, __VA_ARGS__)
    54 #define WARNING(...) err_func(__func__, __VA_ARGS__)
    45 #define PWARNING(...) perr_func(__func__, __VA_ARGS__)
    55 #define PWARNING(...) perr_func(__func__, __VA_ARGS__)
    46 #define EWARNING(err, ...) eerr_func(__func__, (err), __VA_ARGS__)
    56 #define EWARNING(err, ...) eerr_func(__func__, (err), __VA_ARGS__)
    47 
    57 
    48 #ifdef DEBUG_ENABLED
    58 #ifdef DEBUG_ENABLED
    49 #define DEBUG(...) err_func(__func__, __VA_ARGS__)
    59 #define DEBUG(...) debug(__func__, __VA_ARGS__)
       
    60 #define DEBUGF(...) debug(NULL, __VA_ARGS__)
       
    61 #define DEBUGN(...) debug_nonl(__func__, __VA_ARGS__)
       
    62 #define DEBUGNF(...) debug_nonl(NULL, __VA_ARGS__)
    50 #else
    63 #else
    51 #define DEBUG(...) (void) (0)
    64 #define DEBUG(...) (void) (0)
       
    65 #define DEBUGF(...) (void) (0)
       
    66 #define DEBUGN(...) (void) (0)
       
    67 #define DEBUGNF(...) (void) (0)
    52 #endif
    68 #endif
    53 
    69 
    54 // default is to enable INFO
    70 // default is to enable INFO
    55 #ifdef INFO_DISABLED
    71 #ifdef INFO_DISABLED
    56     #define INFO_ENABLED 0
    72     #define INFO_ENABLED 0
    61 #endif
    77 #endif
    62 
    78 
    63 #if INFO_ENABLED
    79 #if INFO_ENABLED
    64 #define INFO(...) info(__VA_ARGS__)
    80 #define INFO(...) info(__VA_ARGS__)
    65 #else
    81 #else
    66 #define INFO(...) (void) (0)
    82 #define INFO(...) (void) (__VA_ARGS__)
    67 #endif
    83 #endif
    68 
    84 
    69 #endif /* LIB_LOG_H */
    85 #endif /* LIB_LOG_H */