aboutsummaryrefslogtreecommitdiffstats
path: root/dbg.h
diff options
context:
space:
mode:
Diffstat (limited to 'dbg.h')
-rw-r--r--dbg.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/dbg.h b/dbg.h
new file mode 100644
index 0000000..65619b0
--- /dev/null
+++ b/dbg.h
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: LGPL-2.1+
+ *
+ * Copyright (C) 2014-2019 Thomas Gschwantner <tharre3@gmail.com>.
+ */
+
+#ifndef __DBG_H__
+#define __DBG_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+
+#ifndef _FILENAME
+#define _FILENAME __FILE__
+#endif
+
+#ifndef NDEBUG
+#define DEBUG 1
+#else
+#define DEBUG 0
+#endif
+
+extern int DBG_LVL;
+
+#define STRINGIFY(x) #x
+#define PREFIX(...) PREFIX_HELPER(_FILENAME, __LINE__, __VA_ARGS__)
+#define SUFFIX(S, M, ...) M S, __VA_ARGS__
+
+#define log_err(...) fprintf(stderr, PREFIX(__VA_ARGS__))
+#define log_warn(...) do { if (DBG_LVL > 2) log_err(__VA_ARGS__); } while (0)
+#define log_info(...) do { if (DBG_LVL > 1) log_err(__VA_ARGS__); } while (0)
+#define die(...) \
+ do { \
+ log_err(__VA_ARGS__); \
+ exit(EXIT_FAILURE); \
+ } while (0)
+#define fatal(...) die(SUFFIX(": %s\n", __VA_ARGS__, strerror(errno)))
+
+#ifdef NDEBUG
+#define PREFIX_HELPER(f,l,...) __VA_ARGS__
+#else
+#define PREFIX_HELPER(f,l,...) "(" f ":" STRINGIFY(l) "): " __VA_ARGS__
+#endif
+
+#define debug(...) do { if (DEBUG) log_err(__VA_ARGS__); } while (0)
+
+#define assert_str_equal(a,b) ({ \
+ if (strcmp(a, b)) { \
+ log_err("Assertion error: '%s' == '%s'\n", a, b); \
+ abort(); \
+ } \
+})
+
+#define assert_int_equal(a,b) ({ \
+ if (a != b) { \
+ log_err("Assertion error: '%d' == '%d'\n", a, b); \
+ abort(); \
+ } \
+})
+
+/* A neat macro that silences unused parameter warnings compiler independant */
+#define UNUSED(x) (void)(x)
+
+#endif