summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-16 11:22:46 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-16 11:22:46 +0200
commitcab7429706aa6fce9d217ba6c5e3b6e5557914d5 (patch)
tree5f718ffb09cabf720ea19fedccac9e3ba087f8e9
parent18a5dcf9ef6f9bde77326bb363ec61bbf6b5e587 (diff)
Cleanup debugging macros a bit
-rw-r--r--cfgdir.c2
-rw-r--r--main.c46
-rw-r--r--main.h26
-rw-r--r--server.c4
-rw-r--r--uring.c17
-rw-r--r--utils.c8
6 files changed, 70 insertions, 33 deletions
diff --git a/cfgdir.c b/cfgdir.c
index 7e5022b..85646bc 100644
--- a/cfgdir.c
+++ b/cfgdir.c
@@ -360,7 +360,7 @@ inotify_cb(struct cfg *cfg, struct uring_task *task, int res)
for (ptr = iev->buf; ptr < iev->buf + res; ptr += sizeof(struct inotify_event) + event->len) {
event = (const struct inotify_event *)ptr;
- if (debuglvl > 0)
+ if (debug_enabled(DBG_CFG))
inotify_event_dump(event);
if (event->mask & (IN_IGNORED | IN_MOVE_SELF | IN_DELETE_SELF | IN_UNMOUNT))
diff --git a/main.c b/main.c
index 8f690bf..51ae12a 100644
--- a/main.c
+++ b/main.c
@@ -23,28 +23,22 @@
#include "systemd.h"
#include "igmp.h"
-int debuglvl = 0;
+unsigned debug_mask = DBG_ERROR | DBG_INFO;
bool exiting = false;
struct cfg *cfghack = NULL;
void
-debug(unsigned lvl, const char *fmt, ...)
+do_debug(enum debug_category category, const char *fmt, ...)
{
va_list ap;
- if (lvl > debuglvl)
- return;
-
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
}
-#define info(...) fprintf(stderr, __VA_ARGS__)
-#define error(...) fprintf(stderr, __VA_ARGS__)
-
__attribute__((noreturn))
void
die(const char *fmt, ...)
@@ -57,8 +51,6 @@ die(const char *fmt, ...)
exit(EXIT_FAILURE);
};
-#define perrordie(msg) die("%s: %m\n", msg)
-
static void
cfg_free(struct uring_task *task)
{
@@ -88,11 +80,31 @@ cfg_read(struct cfg *cfg)
fclose(cfgfile);
}
+const struct {
+ const char *name;
+ unsigned val;
+} debug_category_str[] = {
+ {
+ .name = "config",
+ .val = DBG_CFG
+ },{
+ .name = "refcount",
+ .val = DBG_REF
+ },{
+ .name = "malloc",
+ .val = DBG_MALLOC
+ },{
+ .name = NULL,
+ .val = 0
+ }
+};
+
static struct cfg *
cfg_init(int argc, char **argv)
{
struct cfg *cfg;
int c;
+ unsigned i;
cfg = zmalloc(sizeof(*cfg));
if (!cfg) {
@@ -106,11 +118,11 @@ cfg_init(int argc, char **argv)
int option_index = 0;
static struct option long_options[] = {
{ "homedir", required_argument, 0, 'h' },
- { "debug", no_argument, 0, 'd' },
+ { "debug", required_argument, 0, 'd' },
{ 0, 0, 0, 0 }
};
- c = getopt_long(argc, argv, ":h:d",
+ c = getopt_long(argc, argv, ":h:d:",
long_options, &option_index);
if (c == -1)
break;
@@ -120,7 +132,15 @@ cfg_init(int argc, char **argv)
cfg->homedir = optarg;
break;
case 'd':
- debuglvl++;
+ for (i = 0; debug_category_str[i].name; i++) {
+ if (!strcasecmp(optarg, debug_category_str[i].name))
+ break;
+ }
+
+ if (!debug_category_str[i].name)
+ die("Invalid debug category\n");
+
+ debug_mask |= debug_category_str[i].val;
break;
default:
die("Invalid arguments\n");
diff --git a/main.h b/main.h
index 23be852..255a252 100644
--- a/main.h
+++ b/main.h
@@ -10,12 +10,30 @@ struct cfg;
extern bool exiting;
-extern int debuglvl;
+extern unsigned debug_mask;
-void debug(unsigned lvl, const char *fmt, ...);
+enum debug_category {
+ DBG_ERROR = (0x1 << 1),
+ DBG_INFO = (0x1 << 2),
+ DBG_VERBOSE = (0x1 << 3),
+ DBG_CFG = (0x1 << 4),
+ DBG_REF = (0x1 << 5),
+ DBG_MALLOC = (0x1 << 6),
+};
+
+static inline bool
+debug_enabled(enum debug_category category)
+{
+ return !!(category & debug_mask);
+}
+
+void do_debug(enum debug_category category, const char *fmt, ...);
-#define info(...) fprintf(stderr, __VA_ARGS__)
-#define error(...) fprintf(stderr, __VA_ARGS__)
+#define __debug(c, ...) do { if (debug_enabled((c))) do_debug((c), __VA_ARGS__); } while (0)
+#define debug(c, fmt, ...) __debug(c, "%s:" fmt, __func__, __VA_ARGS__)
+#define verbose(...) __debug(DBG_VERBOSE, __VA_ARGS__)
+#define info(...) __debug(DBG_ERROR, __VA_ARGS__)
+#define error(...) __debug(DBG_ERROR, __VA_ARGS__)
void die(const char *fmt, ...);
diff --git a/server.c b/server.c
index cdd8202..377945f 100644
--- a/server.c
+++ b/server.c
@@ -718,11 +718,11 @@ server_new(struct cfg *cfg, const char *name)
list_for_each_entry(scfg, &cfg->servers, list) {
if (strcmp(name, scfg->name))
continue;
- debug(2, "Server already exists: %s\n", name);
+ fprintf(stderr, "Server already exists: %s\n", name);
return scfg;
}
- debug(2, "Would add server cfg: %s\n", name);
+ fprintf(stderr, "Would add server cfg: %s\n", name);
scfg = zmalloc(sizeof(*scfg));
if (!scfg) {
error("malloc");
diff --git a/uring.c b/uring.c
index 188b101..ea5934d 100644
--- a/uring.c
+++ b/uring.c
@@ -113,10 +113,9 @@ uring_task_put(struct cfg *cfg, struct uring_task *task)
{
struct uring_task *parent = task->parent;
- /*
- fprintf(stderr, "%s: called with task %s (0x%p) and refcount %u\n",
- __func__, task->name, task, task->refcount);
- */
+ debug(DBG_REF, "task %s (%p), refcount %u\n",
+ task->name, task, task->refcount);
+
task->refcount--;
if (task->refcount > 0)
@@ -132,8 +131,8 @@ uring_task_put(struct cfg *cfg, struct uring_task *task)
}
if (parent)
- fprintf(stderr, "%s: task %s (%p) putting parent %s (%p)\n",
- __func__, task->name, task, task->parent->name, task->parent);
+ debug(DBG_REF, "putting parent %s (%p)\n",
+ task->parent->name, task->parent);
if (task->free)
task->free(task);
@@ -145,10 +144,8 @@ uring_task_put(struct cfg *cfg, struct uring_task *task)
void
uring_task_get(struct cfg *cfg, struct uring_task *task)
{
- /*
- fprintf(stderr, "%s: called with task %s (0x%p) and refcount %u\n",
- __func__, task->name, task, task->refcount);
- */
+ debug(DBG_REF, "task %s (%p), refcount %u\n",
+ task->name, task, task->refcount);
if (task->refcount < 0)
error("Negative refcount!\n");
diff --git a/utils.c b/utils.c
index 56b75a5..20c4802 100644
--- a/utils.c
+++ b/utils.c
@@ -36,8 +36,9 @@ add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, s
{
struct allocation *a = malloc(sizeof(*a));
- fprintf(stderr, "Allocation: %s:%i %s(%zu) = %p\n",
- callerfn, line, allocfn, size, ptr);
+ debug(DBG_MALLOC, "%s:%i %s(%zu) = %p\n",
+ callerfn, line, allocfn, size, ptr);
+
a->allocfn = allocfn;
a->callerfn = callerfn;
a->line = line;
@@ -98,7 +99,8 @@ __xfree(const char *fn, int line, void *ptr)
free(ptr);
malloc_count--;
- fprintf(stderr, "Deallocation: %s:%i %p\n", fn, line, ptr);
+ debug(DBG_MALLOC, "%s:%i %p\n", fn, line, ptr);
+
list_for_each_entry_safe(a, tmp, &malloc_list, list) {
if (a->ptr == ptr) {
list_del(&a->list);