#ifndef LOG_H
#define LOG_H
/** @file log.h
*
* Local logging functions
*/
#include "error.h"
/**
* Log level definitions
*
* XXX: these names conflict with <syslog.h>
*/
enum log_level {
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
};
/**
* List of log_level values as strings, NULL-terminated
*/
extern const char *log_level_names[];
/**
* The default log level
*/
#define LOG_LEVEL_DEFAULT LOG_INFO
/**
* Maximum length of a log message, including terminating NUL
*/
#define LOG_MSG_MAX 1024
/**
* Log output function, for displaying a line of text, without any trailing newline.
*/
typedef void (*log_output_func) (const char *line, void *arg);
/**
* Set the current log level to filter out messages below the given level
*/
void set_log_level (enum log_level level);
/**
* Set the current log output function, replacing any default or previous value.
*
* Pass in func as NULL to revert to the default handler.
*/
void log_set_func (log_output_func func, void *arg);
/**
* Log a message with the given level
*/
void log_msg (enum log_level level, const char *func, const char *format, ...)
__attribute__ ((format (printf, 3, 4)));
/**
* Shorthand for log_msg
*/
#define log_debug(...) log_msg(LOG_DEBUG, __func__, __VA_ARGS__)
#define log_info(...) log_msg(LOG_INFO, __func__, __VA_ARGS__)
#define log_warn(...) log_msg(LOG_WARN, __func__, __VA_ARGS__)
#define log_error(...) log_msg(LOG_ERROR, __func__, __VA_ARGS__)
#define log_fatal(...) log_msg(LOG_FATAL, __func__, __VA_ARGS__)
/**
* Log a message with the given level, appending the formatted error code name
*/
void _log_err_code (enum log_level level, err_t err, const char *func, const char *format, ...)
__attribute__ ((format (printf, 4, 5)));
/**
* Log a message with the given level, appending the formatted error message
*/
void _log_err (enum log_level level, const error_t *err, const char *func, const char *format, ...)
__attribute__ ((format (printf, 4, 5)));
/**
* Log using errno.
*/
void _log_perr (enum log_level level, const char *func, const char *format, ...)
__attribute__ ((format (printf, 3, 4)));
/**
* Shorthand for _log_*
*
* XXX: kill log_err_info, add log_err_code
*/
#define log_err(err_code, ...) _log_err_code(LOG_ERROR, err_code, __func__, __VA_ARGS__)
#define log_err_info(err_info, ...) _log_err(LOG_ERROR, err_info, __func__, __VA_ARGS__)
#define log_perr(...) _log_perr(LOG_ERROR, __func__, __VA_ARGS__)
#define log_warn_err_code(err_code, ...) _log_err_code(LOG_WARN, err_code, __func__, __VA_ARGS__)
#define log_warn_err(err_info, ...) _log_err(LOG_WARN, err_info, __func__, __VA_ARGS__)
/**
* log_fatal + exit failure
*/
#define FATAL(...) do { log_fatal(__VA_ARGS__); abort(); } while (0)
/**
* log_err + exit failure
*/
#define FATAL_ERR(err_code, ...) do { _log_err_code(LOG_FATAL, err_code, __func__, __VA_ARGS__); abort(); } while (0)
/**
* log_err_info + exit failure
*/
#define FATAL_ERROR(err_info, ...) do { _log_err(LOG_FATAL, err_info, __func__, __VA_ARGS__); abort(); } while (0)
/**
* log_perr + exit failure
*/
#define FATAL_PERROR(...) do { _log_perr(LOG_FATAL, __func__, __VA_ARGS__); abort(); } while (0)
#endif /* LOG_H */