summaryrefslogtreecommitdiff
path: root/main.c
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 /main.c
parent18a5dcf9ef6f9bde77326bb363ec61bbf6b5e587 (diff)
Cleanup debugging macros a bit
Diffstat (limited to 'main.c')
-rw-r--r--main.c46
1 files changed, 33 insertions, 13 deletions
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");