From c31d324a1171e3c1ab77e56ec510cda450b8a60f Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Fri, 20 Dec 2019 11:40:08 -0500 Subject: [PATCH] Use NDEBUG for debugging; add debug print helper This commit is split up in two parts. First, usage of DEBUG is remapped to NDEBUG, such that the behaviour of assert(3) is as expected with --enable-debug=false (i.e. asserts get disabled, where they previously did not). Second, instances of `printf` guarded by `#ifdef DEBUG` blocks have been refactored into a dedicated `debug` macro to make the code a bit easier to follow. `debug` is a no-op when `NDEBUG` is set. --- cava.c | 32 ++++++++++++-------------------- configure.ac | 4 ++-- debug.h | 7 +++++++ input/alsa.c | 16 ++++------------ 4 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 debug.h diff --git a/cava.c b/cava.c index e84aecd..4dd23c6 100644 --- a/cava.c +++ b/cava.c @@ -31,6 +31,8 @@ #include #include +#include "debug.h" + #ifdef NCURSES #include "output/terminal_ncurses.h" #include "output/terminal_ncurses.c" @@ -361,9 +363,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co // general: main loop while (1) { - #ifdef DEBUG - printf("loading config\n"); - #endif + debug("loading config\n"); //config: load struct error_s error; error.length = 0; @@ -416,9 +416,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co audio.treble_index = 0; - #ifdef DEBUG - printf("starting audio thread\n"); - #endif + debug("starting audio thread\n"); #ifdef ALSA // input_alsa: wait for the input to be ready if (p.im == 1) { @@ -451,9 +449,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co exit(EXIT_FAILURE); } } - #ifdef DEBUG - printf("got format: %d and rate %d\n", audio.format, audio.rate); - #endif + debug("got format: %d and rate %d\n", audio.format, audio.rate); } #endif @@ -540,10 +536,8 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co fftw_plan p_treble_l = fftw_plan_dft_r2c_1d(audio.FFTtreblebufferSize, in_treble_l, out_treble_l, FFTW_MEASURE); fftw_plan p_treble_r = fftw_plan_dft_r2c_1d(audio.FFTtreblebufferSize, in_treble_r, out_treble_r, FFTW_MEASURE); - #ifdef DEBUG - printf("got buffer size: %d, %d, %d", audio.FFTbassbufferSize, audio.FFTmidbufferSize, audio.FFTtreblebufferSize); - printf("zeroing buffers\n"); - #endif + debug("got buffer size: %d, %d, %d", audio.FFTbassbufferSize, audio.FFTmidbufferSize, audio.FFTtreblebufferSize); + debug("zeroing buffers\n"); for (i = 0; i < (audio.FFTbassbufferSize / 2 + 1); i++) { if (i < audio.FFTbassbufferSize) { audio.audio_out_bass_l[i] = 0; @@ -687,7 +681,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co rest = (w - bars * p.bw - bars * p.bs + p.bs) / 2; if (rest < 0)rest = 0; - #ifdef DEBUG + #ifndef NDEBUG printw("height: %d width: %d bars:%d bar width: %d rest: %d\n", w, h, bars, p.bw, rest); @@ -768,7 +762,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co hcf[n - 1] = lcf[n] - 1; } -#ifdef DEBUG +#ifndef NDEBUG if (n != 0) { mvprintw(n,0,"%d: %f -> %f (%d -> %d) bass: %d, treble:%d \n", n, fc[n - 1], fc[n], lcf[n - 1], @@ -851,7 +845,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co //if (cont == 0) break; - #ifdef DEBUG + #ifndef NDEBUG //clear(); refresh(); #endif @@ -1020,13 +1014,11 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co if (p.autosens && !silence && senselow) p.sens = p.sens * 1.001; - #ifdef DEBUG - //printf("%d\n",maxvalue); //checking maxvalue 10000 - #endif + //debug("%d\n",maxvalue); //checking maxvalue 10000 // output: draw processed input - #ifndef DEBUG + #ifdef NDEBUG switch (p.om) { case 1: #ifdef NCURSES diff --git a/configure.ac b/configure.ac index 097c128..ad7e4e7 100644 --- a/configure.ac +++ b/configure.ac @@ -18,9 +18,9 @@ AC_ARG_ENABLE([debug], [enable debug messages and frequency table output]) ) -AS_IF([test "x$enable_debug" = "xyes"], [ +AS_IF([test "x$enable_debug" != "xyes"], [ dnl enabling debug mode - CPPFLAGS="$CPPFLAGS -DDEBUG" + CPPFLAGS="$CPPFLAGS -DNDEBUG" ]) diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..4e9d46f --- /dev/null +++ b/debug.h @@ -0,0 +1,7 @@ +#include + +#ifdef NDEBUG +#define debug(...) do { } while (0) +#else +#define debug(...) fprintf(stderr, __VA_ARGS__) +#endif diff --git a/input/alsa.c b/input/alsa.c index 15b625b..0a852d2 100644 --- a/input/alsa.c +++ b/input/alsa.c @@ -12,9 +12,7 @@ snd_pcm_uframes_t* frames) { fprintf(stderr, "error opening stream: %s\n", snd_strerror(err)); exit(EXIT_FAILURE); } - #ifdef DEBUG - else printf("open stream successful\n"); - #endif + else debug("open stream successful\n"); snd_pcm_hw_params_t* params; snd_pcm_hw_params_alloca(¶ms); // assembling params @@ -145,18 +143,12 @@ void* input_alsa(void* data) { if (err == -EPIPE) { /* EPIPE means overrun */ -#ifdef DEBUG - fprintf(stderr, "overrun occurred\n"); -#endif + debug("overrun occurred\n"); snd_pcm_prepare(handle); } else if (err < 0) { -#ifdef DEBUG - fprintf(stderr, "error from read: %s\n", snd_strerror(err)); -#endif + debug("error from read: %s\n", snd_strerror(err)); } else if (err != (int)frames) { -#ifdef DEBUG - fprintf(stderr, "short read, read %d %d frames\n", err, (int)frames); -#endif + debug("short read, read %d %d frames\n", err, (int)frames); }