src/log.h
changeset 21 0911d0b828d4
child 22 c339c020fd33
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/log.h	Sun Mar 01 01:48:14 2009 +0200
@@ -0,0 +1,60 @@
+#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 */