aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/debug.c
blob: c075f13d65bb86a0feb7fe892c168c7f5ad91488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Simple debug functions for level and category filtering
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include "debug.h"
#include "call.h"

const char *debug_level[] = {
	"debug  ",
	"info   ",
	"notice ",
	"error  ",
	NULL,
};

struct debug_cat {
	const char *name;
	const char *color;
} debug_cat[] = {
	{ "sender", "\033[1;33m" },
	{ "sound", "\033[0;35m" },
	{ "dsp", "\033[0;31m" },
	{ "anetz", "\033[1;34m" },
	{ "bnetz", "\033[1;34m" },
	{ "cnetz", "\033[1;34m" },
	{ "nmt", "\033[1;34m" },
	{ "amps", "\033[1;34m" },
	{ "frame", "\033[0;36m" },
	{ "call", "\033[1;37m" },
	{ "mncc", "\033[1;32m" },
	{ "database", "\033[0;33m" },
	{ "transaction", "\033[0;32m" },
	{ "dms", "\033[0;33m" },
	{ "sms", "\033[1;37m" },
	{ 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, ...)
{
	char buffer[4096];
	const char *p;
	va_list args;

	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';
	va_end(args);

	while ((p = strchr(file, '/')))
		file = p + 1;

	clear_console_text();
//	printf("%s%s:%d %s() %s: %s\033[0;39m", debug_cat[cat].color, file, line, function, debug_level[level], buffer);
	printf("%s%s:%d %s: %s\033[0;39m", debug_cat[cat].color, file, line, debug_level[level], buffer);
	fflush(stdout);
}

const char *debug_amplitude(double level)
{
	static char text[42];

	strcpy(text, "                    :                    ");
	if (level > 1.0)
		level = 1.0;
	if (level < -1.0)
		level = -1.0;
	text[20 + (int)(level * 20)] = '*';

	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;
}