Restructure: Move debug from common code to 'libdebug'

This commit is contained in:
Andreas Eversberg
2017-11-18 08:58:57 +01:00
parent 016e72d6d7
commit 1a341f6b4e
67 changed files with 80 additions and 76 deletions

11
src/libdebug/Makefile.am Normal file
View File

@@ -0,0 +1,11 @@
AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
noinst_LIBRARIES = libdebug.a
libdebug_a_SOURCES = \
debug.c
if HAVE_SDR
AM_CPPFLAGS += -DHAVE_SDR
endif

206
src/libdebug/debug.c Normal file
View File

@@ -0,0 +1,206 @@
/* 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 <math.h>
#include "../libsample/sample.h"
#include "debug.h"
#include "../libdisplay/display.h"
#include "../libmncc/mncc_console.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" },
{ "r2000", "\033[1;34m" },
{ "frame", "\033[0;36m" },
{ "call", "\033[0;37m" },
{ "mncc", "\033[1;32m" },
{ "database", "\033[0;33m" },
{ "transaction", "\033[0;32m" },
{ "dms", "\033[0;33m" },
{ "sms", "\033[1;37m" },
{ "sdr", "\033[1;31m" },
{ "uhd", "\033[1;35m" },
{ "soapy", "\033[1;35m" },
{ NULL, NULL }
};
int debuglevel = DEBUG_INFO;
uint64_t debug_mask = ~0;
extern int num_kanal;
void _printdebug(const char *file, const char __attribute__((unused)) *function, int line, int cat, int level, int chan, const char *fmt, ...)
{
char buffer[4096], *b = buffer;
int s = sizeof(buffer) - 1;
const char *p;
va_list args;
if (debuglevel > level)
return;
buffer[sizeof(buffer) - 1] = '\0';
/* if chan is used, prefix the channel number */
if (num_kanal > 1 && chan >= 0) {
sprintf(buffer, "(chan %d) ", chan);
b = strchr(buffer, '\0');
s -= strlen(buffer);
}
if (!(debug_mask & (1 << cat)))
return;
va_start(args, fmt);
vsnprintf(b, s, fmt, args);
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);
display_wave_limit_scroll(1);
display_status_limit_scroll(1);
display_measurements_limit_scroll(1);
#ifdef HAVE_SDR
display_iq_limit_scroll(1);
display_spectrum_limit_scroll(1);
#endif
printf("%s%s:%d %s: %s\033[0;39m", debug_cat[cat].color, file, line, debug_level[level], buffer);
display_wave_limit_scroll(0);
display_status_limit_scroll(0);
display_measurements_limit_scroll(0);
#ifdef HAVE_SDR
display_iq_limit_scroll(0);
display_spectrum_limit_scroll(0);
#endif
print_console_text();
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;
}
#define level2db(level) (20 * log10(level))
const char *debug_db(double level_db)
{
static char text[128];
int l;
strcpy(text, ": . : . : . : . : . : . : . : . | . : . : . : . : . : . : . : . :");
if (level_db <= 0.0)
return text;
l = (int)round(level2db(level_db));
if (l > 48)
return text;
if (l < -48)
return text;
text[l + 48] = '*';
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;
}

38
src/libdebug/debug.h Normal file
View File

@@ -0,0 +1,38 @@
#define DEBUG_DEBUG 0 /* debug info, not for normal use */
#define DEBUG_INFO 1 /* all info about process */
#define DEBUG_NOTICE 2 /* something unexpected happens */
#define DEBUG_ERROR 3 /* there is an error with this software */
#define DSENDER 0
#define DSOUND 1
#define DDSP 2
#define DANETZ 3
#define DBNETZ 4
#define DCNETZ 5
#define DNMT 6
#define DAMPS 7
#define DR2000 8
#define DFRAME 9
#define DCALL 10
#define DMNCC 11
#define DDB 12
#define DTRANS 13
#define DDMS 14
#define DSMS 15
#define DSDR 16
#define DUHD 17
#define DSOAPY 18
#define PDEBUG(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, -1, fmt, ## arg)
#define PDEBUG_CHAN(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, CHAN, fmt, ## arg)
void _printdebug(const char *file, const char *function, int line, int cat, int level, int chan, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 7, 8)));
const char *debug_amplitude(double level);
const char *debug_db(double level_db);
void debug_list_cat(void);
int parse_debug_opt(const char *opt);
extern int debuglevel;