src/log.h
author Tero Marttila <terom@fixme.fi>
Sun, 01 Mar 2009 01:48:14 +0200
changeset 21 0911d0b828d4
child 22 c339c020fd33
permissions -rw-r--r--
add basic log.c module
#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,
};

/**
 * 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 LOG_ERROR, appending the formatted error code
 */
void _log_err (err_t err, const char *func, const char *format, ...)
    __attribute__ ((format (printf, 3, 4)));

void _log_err_info (struct error_info *err, const char *func, const char *format, ...)
    __attribute__ ((format (printf, 3, 4)));

#define log_err(err, ...) _log_err(err, __func__, __VA_ARGS__)
#define log_err_info(err_info, ...) _log_err_info(err_info, __func__, __VA_ARGS__)

/*
 * log_fatal + exit failure
 */
#define FATAL(...) do { log_fatal(__VA_ARGS__); exit(EXIT_FAILURE); } while (0)

/*
 * log_err_info + exit failure
 */
#define FATAL_ERROR(err, ...) do { log_err_info(err, __VA_ARGS__); exit(EXIT_FAILURE); } while (0)

#endif /* LOG_H */