src/log.h
changeset 21 0911d0b828d4
child 22 c339c020fd33
equal deleted inserted replaced
20:d9c4c2980a0d 21:0911d0b828d4
       
     1 #ifndef LOG_H
       
     2 #define LOG_H
       
     3 
       
     4 /** @file log.h
       
     5  *
       
     6  * Local logging functions
       
     7  */
       
     8 #include "error.h"
       
     9 
       
    10 /**
       
    11  * Log level definitions
       
    12  *
       
    13  * XXX: these names conflict with <syslog.h>
       
    14  */
       
    15 enum log_level {
       
    16     LOG_DEBUG,
       
    17     LOG_INFO,
       
    18     LOG_WARN,
       
    19     LOG_ERROR,
       
    20     LOG_FATAL,
       
    21 };
       
    22 
       
    23 /**
       
    24  * Log a message with the given level
       
    25  */
       
    26 void log_msg (enum log_level level, const char *func, const char *format, ...)
       
    27     __attribute__ ((format (printf, 3, 4)));
       
    28 
       
    29 /**
       
    30  * Shorthand for log_msg
       
    31  */
       
    32 #define log_debug(...) log_msg(LOG_DEBUG, __func__, __VA_ARGS__)
       
    33 #define log_info(...)  log_msg(LOG_INFO,  __func__, __VA_ARGS__)
       
    34 #define log_warn(...)  log_msg(LOG_WARN,  __func__, __VA_ARGS__)
       
    35 #define log_error(...) log_msg(LOG_ERROR, __func__, __VA_ARGS__)
       
    36 #define log_fatal(...) log_msg(LOG_FATAL, __func__, __VA_ARGS__)
       
    37 
       
    38 /**
       
    39  * Log a message with LOG_ERROR, appending the formatted error code
       
    40  */
       
    41 void _log_err (err_t err, const char *func, const char *format, ...)
       
    42     __attribute__ ((format (printf, 3, 4)));
       
    43 
       
    44 void _log_err_info (struct error_info *err, const char *func, const char *format, ...)
       
    45     __attribute__ ((format (printf, 3, 4)));
       
    46 
       
    47 #define log_err(err, ...) _log_err(err, __func__, __VA_ARGS__)
       
    48 #define log_err_info(err_info, ...) _log_err_info(err_info, __func__, __VA_ARGS__)
       
    49 
       
    50 /*
       
    51  * log_fatal + exit failure
       
    52  */
       
    53 #define FATAL(...) do { log_fatal(__VA_ARGS__); exit(EXIT_FAILURE); } while (0)
       
    54 
       
    55 /*
       
    56  * log_err_info + exit failure
       
    57  */
       
    58 #define FATAL_ERROR(err, ...) do { log_err_info(err, __VA_ARGS__); exit(EXIT_FAILURE); } while (0)
       
    59 
       
    60 #endif /* LOG_H */