common code: Add feature to select debug category rather than showing all

This commit is contained in:
Andreas Eversberg 2016-06-12 10:50:55 +02:00
parent 051cc81b13
commit 7cfd11eb17
3 changed files with 81 additions and 8 deletions

View File

@ -20,6 +20,9 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include "debug.h"
const char *debug_level[] = {
@ -27,6 +30,7 @@ const char *debug_level[] = {
"info ",
"notice ",
"error ",
NULL,
};
struct debug_cat {
@ -44,9 +48,12 @@ struct debug_cat {
{ "call", "\033[1;37m" },
{ "mncc", "\033[1;32m" },
{ "database", "\033[0;33m" },
{ "transaction", "\033[0;32m" },
{ NULL, NULL }
};
int debuglevel = DEBUG_INFO;
uint64_t debug_mask = ~0;
void _printdebug(const char *file, const char *function, int line, int cat, int level, const char *fmt, ...)
{
@ -57,6 +64,9 @@ void _printdebug(const char *file, const char *function, int line, int cat, int
if (debuglevel > level)
return;
if (!(debug_mask & (1 << cat)))
return;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
buffer[sizeof(buffer) - 1] = '\0';
@ -84,3 +94,56 @@ const char *debug_amplitude(double level)
return text;
}
void debug_list_cat(void)
{
int i;
printf("Give number of debug level:\n");
for (i = 0; debug_level[i]; i++)
printf(" %d = %s\n", i, debug_level[i]);
printf("\n");
printf("Give name(s) of debug category:\n");
for (i = 0; debug_cat[i].name; i++)
printf(" %s%s\033[0;39m\n", debug_cat[i].color, debug_cat[i].name);
printf("\n");
}
int parse_debug_opt(const char *optarg)
{
int i, max_level = 0;
char *dstring, *p;
for (i = 0; debug_level[i]; i++)
max_level = i;
dstring = strdup(optarg);
p = strsep(&dstring, ",");
for (i = 0; i < p[i]; i++) {
if (p[i] < '0' || p[i] > '9') {
fprintf(stderr, "Only digits are allowed for debug level!\n");
return -EINVAL;
}
}
debuglevel = atoi(p);
if (debuglevel > max_level) {
fprintf(stderr, "Debug level too high, use 'list' to show available levels!\n");
return -EINVAL;
}
if (dstring)
debug_mask = 0;
while((p = strsep(&dstring, ","))) {
for (i = 0; debug_cat[i].name; i++) {
if (!strcasecmp(p, debug_cat[i].name))
break;
}
if (!debug_cat[i].name) {
fprintf(stderr, "Given debug category '%s' unknown, use 'list' to show available categories!\n", p);
return -EINVAL;
}
debug_mask |= (1 << i);
}
return 0;
}

View File

@ -15,11 +15,15 @@
#define DCALL 8
#define DMNCC 9
#define DDB 10
#define DTRANS 11
#define PDEBUG(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, fmt, ## arg)
void _printdebug(const char *file, const char *function, int line, int cat, int level, const char *fmt, ...);
const char *debug_amplitude(double level);
void debug_list_cat(void);
int parse_debug_opt(const char *opt);
extern int debuglevel;

View File

@ -54,8 +54,11 @@ void print_help_common(const char *arg0, const char *ext_usage)
/* - - */
printf(" -h --help\n");
printf(" This help\n");
printf(" -D --debug <level>\n");
printf(" Debug level: 0 = debug | 1 = info | 2 = notice (default = '%d')\n", debuglevel);
printf(" -D --debug <level> | <level>,<category>[,<category>[,...]] | list\n");
printf(" Use 'list' to get a list of all levels and categories\n");
printf(" Debug level: digit of debug level (default = '%d')\n", debuglevel);
printf(" Debug level+category: level digit followed by one or more categories\n");
printf(" -> If no category is specified, all categories are selected\n");
printf(" -k --kanal <channel>\n");
printf(" Channel number of \"Sender\"\n");
printf(" -d --device hw:<card>,<device>\n");
@ -76,7 +79,7 @@ void print_help_common(const char *arg0, const char *ext_usage)
printf(" -G --rx-gain <dB>\n");
printf(" Raise receiver RX level by given gain in dB. This is useful if input\n");
printf(" level of the sound device is too low, even after setting maximum level\n");
printf(" with the mixer settings.\n");
printf(" with the mixer settings.\n");
printf(" -m --mncc-sock\n");
printf(" Disable built-in call contol and offer socket (to LCR)\n");
printf(" -c --call-device hw:<card>,<device>\n");
@ -143,11 +146,14 @@ void opt_switch_common(int c, char *arg0, int *skip_args)
print_help(arg0);
exit(0);
case 'D':
debuglevel = atoi(optarg);
if (debuglevel > 2)
debuglevel = 2;
if (debuglevel < 0)
debuglevel = 0;
if (!strcasecmp(optarg, "list")) {
debug_list_cat();
exit(0);
}
if (parse_debug_opt(optarg)) {
fprintf(stderr, "Failed to parse debug option, please use -h for help.\n");
exit(0);
}
*skip_args += 2;
break;
case 'k':