common code: Add feature to select debug category rather than showing all
This commit is contained in:
@@ -20,6 +20,9 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <errno.h>
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
const char *debug_level[] = {
|
const char *debug_level[] = {
|
||||||
@@ -27,6 +30,7 @@ const char *debug_level[] = {
|
|||||||
"info ",
|
"info ",
|
||||||
"notice ",
|
"notice ",
|
||||||
"error ",
|
"error ",
|
||||||
|
NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct debug_cat {
|
struct debug_cat {
|
||||||
@@ -44,9 +48,12 @@ struct debug_cat {
|
|||||||
{ "call", "\033[1;37m" },
|
{ "call", "\033[1;37m" },
|
||||||
{ "mncc", "\033[1;32m" },
|
{ "mncc", "\033[1;32m" },
|
||||||
{ "database", "\033[0;33m" },
|
{ "database", "\033[0;33m" },
|
||||||
|
{ "transaction", "\033[0;32m" },
|
||||||
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
int debuglevel = DEBUG_INFO;
|
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, ...)
|
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)
|
if (debuglevel > level)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (!(debug_mask & (1 << cat)))
|
||||||
|
return;
|
||||||
|
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
|
vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
|
||||||
buffer[sizeof(buffer) - 1] = '\0';
|
buffer[sizeof(buffer) - 1] = '\0';
|
||||||
@@ -84,3 +94,56 @@ const char *debug_amplitude(double level)
|
|||||||
return text;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -15,11 +15,15 @@
|
|||||||
#define DCALL 8
|
#define DCALL 8
|
||||||
#define DMNCC 9
|
#define DMNCC 9
|
||||||
#define DDB 10
|
#define DDB 10
|
||||||
|
#define DTRANS 11
|
||||||
|
|
||||||
#define PDEBUG(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, fmt, ## arg)
|
#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, ...);
|
void _printdebug(const char *file, const char *function, int line, int cat, int level, const char *fmt, ...);
|
||||||
|
|
||||||
const char *debug_amplitude(double level);
|
const char *debug_amplitude(double level);
|
||||||
|
|
||||||
|
void debug_list_cat(void);
|
||||||
|
int parse_debug_opt(const char *opt);
|
||||||
|
|
||||||
extern int debuglevel;
|
extern int debuglevel;
|
||||||
|
|
||||||
|
@@ -54,8 +54,11 @@ void print_help_common(const char *arg0, const char *ext_usage)
|
|||||||
/* - - */
|
/* - - */
|
||||||
printf(" -h --help\n");
|
printf(" -h --help\n");
|
||||||
printf(" This help\n");
|
printf(" This help\n");
|
||||||
printf(" -D --debug <level>\n");
|
printf(" -D --debug <level> | <level>,<category>[,<category>[,...]] | list\n");
|
||||||
printf(" Debug level: 0 = debug | 1 = info | 2 = notice (default = '%d')\n", debuglevel);
|
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(" -k --kanal <channel>\n");
|
||||||
printf(" Channel number of \"Sender\"\n");
|
printf(" Channel number of \"Sender\"\n");
|
||||||
printf(" -d --device hw:<card>,<device>\n");
|
printf(" -d --device hw:<card>,<device>\n");
|
||||||
@@ -143,11 +146,14 @@ void opt_switch_common(int c, char *arg0, int *skip_args)
|
|||||||
print_help(arg0);
|
print_help(arg0);
|
||||||
exit(0);
|
exit(0);
|
||||||
case 'D':
|
case 'D':
|
||||||
debuglevel = atoi(optarg);
|
if (!strcasecmp(optarg, "list")) {
|
||||||
if (debuglevel > 2)
|
debug_list_cat();
|
||||||
debuglevel = 2;
|
exit(0);
|
||||||
if (debuglevel < 0)
|
}
|
||||||
debuglevel = 0;
|
if (parse_debug_opt(optarg)) {
|
||||||
|
fprintf(stderr, "Failed to parse debug option, please use -h for help.\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
*skip_args += 2;
|
*skip_args += 2;
|
||||||
break;
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
|
Reference in New Issue
Block a user