Skip to content
Browse files

use debug_printf() for debugging

  • Loading branch information...
1 parent 8033fbf commit 72dc184191062f1bdfafaba0087be10b70e52e8a @kfish committed May 25, 2009
View
1 src/libfishsound/Makefile.am
@@ -13,6 +13,7 @@ EXTRA_DIST = Version_script.in
lib_LTLIBRARIES = libfishsound.la
noinst_HEADERS = \
+ debug.h \
private.h \
convert.h \
fs_compat.h \
View
35 src/libfishsound/comments.c
@@ -43,6 +43,7 @@
#include "private.h"
/*#define DEBUG*/
+#include "debug.h"
/* Ensure comment vector length can be expressed in 32 bits
* including space for the trailing NUL */
@@ -141,9 +142,7 @@ fs_comment_validate_byname (const char * name, const char * value)
for (c = name; *c; c++) {
if (*c < 0x20 || *c > 0x7D || *c == 0x3D) {
-#ifdef DEBUG
- printf ("XXX char %c in %s invalid\n", *c, name);
-#endif
+ debug_printf (1, "XXX char %c in %s invalid", *c, name);
return 0;
}
}
@@ -460,19 +459,15 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments,
/* This value gets checked effectively by the 'for' condition
and the checks within the loop for c running off the end. */
nb_fields=readint(c, 0);
-#ifdef DEBUG
- printf ("fish_sound_comments_decode: %d comments\n", nb_fields);
-#endif
+ debug_printf (1, "%d comments", nb_fields);
c+=4;
for (i=0;i<nb_fields;i++)
{
if (c+4>end) return -1;
len=readint(c, 0);
-#ifdef DEBUG
- printf ("fish_sound_comments_decode: [%d] len %d\n", i, len);
-#endif
+ debug_printf (1, "[%d] len %d\n", i, len);
c+=4;
if (len > (unsigned long) (end-c)) return -1;
@@ -486,10 +481,9 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments,
n = c+len - value;
if ((nvalue = fs_strdup_len (value, n)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY;
-#ifdef DEBUG
- printf ("fish_sound_comments_decode: %s -> %s (length %d)\n",
- name, nvalue, n);
-#endif
+
+ debug_printf (1, "%s -> %s (length %d)", name, nvalue, n);
+
if ((comment = fs_comment_new (name, nvalue)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY;
@@ -498,10 +492,8 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments,
fs_free (nvalue);
} else {
-#ifdef DEBUG
- printf ("fish_sound_comments_decode: [%d] %s (no value)\n",
- i, name, len);
-#endif
+ debug_printf (1, "[%d] %s (no value)", i, name, len);
+
if ((nvalue = fs_strdup_len (name, len)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY;
@@ -517,9 +509,7 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments,
c+=len;
}
-#ifdef DEBUG
- printf ("fish_sound_comments_decode: done\n");
-#endif
+ debug_printf (1, "OUT");
return FISH_SOUND_OK;
}
@@ -575,10 +565,7 @@ fish_sound_comments_encode (FishSound * fsound, unsigned char * buf,
return 0;
}
-#ifdef DEBUG
- printf ("fish_sound_comments_encode: %s = %s\n",
- comment->name, comment->value);
-#endif
+ debug_printf (1, "%s = %s", comment->name, comment->value);
nb_fields++;
}
View
85 src/libfishsound/debug.h
@@ -0,0 +1,85 @@
+/*
+ * A generic debugging printer.
+ *
+ * Conrad Parker <conrad@metadecks.org>, May 2009
+ *
+ * Usage:
+ *
+ * #define DEBUG_LEVEL 3
+ * #include "debug.h"
+ *
+ * ...
+ * debug_print (2, "Something went wrong");
+ * ...
+ *
+ * The macro print_debug(level, fmt) prints a formatted debugging message
+ * of level 'level' to stderr. You should set the maximum tolerable debug
+ * level before including debug.h. If it is 0, or if neither DEBUG_LEVEL
+ * nor DEBUG are defined, then the debug_printf() macro is ignored, and
+ * none of this file is included.
+ */
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+#ifdef DEBUG
+#ifndef DEBUG_LEVEL
+#define DEBUG_LEVEL 1
+#endif
+#endif
+
+#if (DEBUG_LEVEL > 0)
+
+#define DEBUG_MAXLINE 4096
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+
+/*
+ * debug_print_err (func, line, fmt)
+ *
+ * Print a formatted error message to stderr.
+ */
+static void
+debug_print_err (const char * func, int line, const char * fmt, ...)
+{
+ va_list ap;
+ int errno_save;
+ char buf[DEBUG_MAXLINE];
+ int n=0;
+
+ errno_save = errno;
+
+ va_start (ap, fmt);
+
+ if (func) {
+ snprintf (buf+n, DEBUG_MAXLINE-n, "%s():%d: ", func, line);
+ n = strlen (buf);
+ }
+
+ vsnprintf (buf+n, DEBUG_MAXLINE-n, fmt, ap);
+ n = strlen (buf);
+
+ fflush (stdout); /* in case stdout and stderr are the same */
+ fputs (buf, stderr);
+ fputc ('\n', stderr);
+ fflush (NULL);
+
+ va_end (ap);
+}
+
+/*
+ * debug_printf (level, fmt)
+ *
+ * Print a formatted debugging message of level 'level' to stderr
+ */
+#define debug_printf(x,y...) {if (x <= DEBUG_LEVEL) debug_print_err (__func__, __LINE__, y);}
+
+#undef MAXLINE
+
+#else
+#define debug_printf(x,y...)
+#endif
+
+#endif /* __DEBUG_H__ */
View
120 src/libfishsound/flac.c
@@ -45,8 +45,10 @@
#include "private.h"
#include "convert.h"
-/*#define DEBUG*/
-/*#define DEBUG_VERBOSE*/
+#define DEBUG_VERBOSE 2
+/* #define DEBUG */
+/* #define DEBUG_LEVEL DEBUG_VERBOSE */
+#include "debug.h"
#if HAVE_FLAC
@@ -82,9 +84,7 @@ fish_sound_flac_identify (unsigned char * buf, long bytes)
if (bytes < 8) return FISH_SOUND_UNKNOWN;
if (buf[0] != 0x7f) return FISH_SOUND_UNKNOWN;
if (!strncmp ((char *)buf+1, "FLAC", 4)) {
-#ifdef DEBUG
- printf("fish_sound_flac_identify: flac found\n");
-#endif
+ debug_printf(1, "flac found");
/* if only a short buffer was passed, do a weak identify */
if (bytes == 8) return FISH_SOUND_FLAC;
@@ -111,18 +111,12 @@ fs_flac_read_callback(const FLAC__StreamDecoder *decoder,
{
FishSound* fsound = (FishSound*)client_data;
FishSoundFlacInfo* fi = (FishSoundFlacInfo *)fsound->codec_data;
-#ifdef DEBUG_VERBOSE
- printf("fs_flac_read_callback: IN\n");
-#endif
+ debug_printf(DEBUG_VERBOSE, "fs_flac_read_callback: IN");
if (fi->bufferlength > *bytes) {
-#ifdef DEBUG
- printf("fs_flac_read_callback: too much data\n");
-#endif
+ debug_printf(1, "too much data");
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
} else if (fi->bufferlength < 1) {
-#ifdef DEBUG
- printf("fs_flac_read_callback: no data, %ld\n",fi->bufferlength);
-#endif
+ debug_printf(1, "no data, %ld",fi->bufferlength);
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}
@@ -146,9 +140,7 @@ fs_flac_write_callback(const FLAC__StreamDecoder *decoder,
channels = frame->header.channels;
blocksize = frame->header.blocksize;
-#ifdef DEBUG_VERBOSE
- printf("fs_flac_write_callback: IN, blocksize %d\n", blocksize);
-#endif
+ debug_printf(DEBUG_VERBOSE, "IN, blocksize %d", blocksize);
fsound->frameno += blocksize;
@@ -200,23 +192,18 @@ fs_flac_meta_callback(const FLAC__StreamDecoder *decoder,
{
FishSound* fsound = (FishSound*)client_data;
/* FishSoundFlacInfo* fi = (FishSoundFlacInfo *)fsound->codec_data; */
-#ifdef DEBUG
- printf("fs_flac_meta_callback: IN\n");
-#endif
+ debug_printf(1, "IN");
+
switch (metadata->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
-#ifdef DEBUG
- printf("fs_flac_meta_callback: channels %d, samplerate %d\n",
+ debug_printf(1, "channels %d, samplerate %d",
metadata->data.stream_info.channels,
metadata->data.stream_info.sample_rate);
-#endif
fsound->info.channels = metadata->data.stream_info.channels;
fsound->info.samplerate = metadata->data.stream_info.sample_rate;
break;
default:
-#ifdef DEBUG
- printf("fs_flac_meta_callback: not yet implemented type\n");
-#endif
+ debug_printf(1, "not yet implemented type");
break;
}
}
@@ -226,9 +213,7 @@ fs_flac_error_callback(const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
void *client_data)
{
-#ifdef DEBUG
- printf("fs_flac_error_callback: IN\n");
-#endif
+ debug_printf(1, "IN");
fprintf(stderr, "FLAC ERROR: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
}
#endif
@@ -243,20 +228,13 @@ fs_flac_decode_header (FishSound * fsound, unsigned char *buf, long bytes)
if (strncmp((char *)buf+1, "FLAC", 4) != 0) return NULL;
fi->version.major = buf[5];
fi->version.minor = buf[6];
-#ifdef DEBUG
- printf("fs_flac_decode_header : Flac Ogg Mapping Version: %d.%d\n",
+ debug_printf(1, "Flac Ogg Mapping Version: %d.%d",
fi->version.major, fi->version.minor);
-#endif
fi->header_packets = buf[7] << 8 | buf[8];
-#ifdef DEBUG
- printf("fs_flac_decode_header: Number of Header packets: %d\n",
- fi->header_packets);
-#endif
+ debug_printf(1, "Number of Header packets: %d", fi->header_packets);
if ((fi->fsd = FLAC__stream_decoder_new()) == NULL) {
-#ifdef DEBUG
- printf ("fs_flac_decode_header: unable to create new stream_decoder\n");
-#endif
+ debug_printf (1, "unable to create new stream_decoder");
return NULL;
}
@@ -293,15 +271,11 @@ fs_flac_decode (FishSound * fsound, unsigned char * buf, long bytes)
{
FishSoundFlacInfo *fi = fsound->codec_data;
-#ifdef DEBUG_VERBOSE
- printf("fs_flac_decode: IN, fi->packetno = %ld\n", fi->packetno);
-#endif
+ debug_printf(DEBUG_VERBOSE, "IN, fi->packetno = %ld", fi->packetno);
if (fi->packetno == 0) {
if (fs_flac_decode_header (fsound, buf, bytes) == NULL) {
-#ifdef DEBUG
- printf("fs_flac_decode: Error reading header\n");
-#endif
+ debug_printf(1, "Error reading header");
return -1;
}
if ((fi->buffer = fs_malloc(sizeof(unsigned char)*bytes)) == NULL)
@@ -312,20 +286,17 @@ fs_flac_decode (FishSound * fsound, unsigned char * buf, long bytes)
}
else if (fi->packetno <= fi->header_packets){
unsigned char* tmp;
-#ifdef DEBUG
- printf("fs_flac_decode: handling header (fi->header_packets = %d)\n",
- fi->header_packets);
-#endif
+ debug_printf(1, "handling header (fi->header_packets = %d)",
+ fi->header_packets);
#if 0
if (fi->packetno == 1) fish_sound_comments_decode (fsound, buf, bytes);
#endif
if ((buf[0] & 0x7) == 4) {
int len = (buf[1]<<16) + (buf[2]<<8) + buf[3];
-#ifdef DEBUG
- printf ("fs_flac_decode: got vorbiscomments len %d\n", len);
-#endif
+ debug_printf (1, "got vorbiscomments len %d", len);
+
if (fish_sound_comments_decode (fsound, buf+4, len) == FISH_SOUND_ERR_OUT_OF_MEMORY) {
fi->packetno++;
return FISH_SOUND_ERR_OUT_OF_MEMORY;
@@ -381,10 +352,10 @@ fs_flac_enc_write_callback(const FLAC__StreamEncoder *encoder,
{
FishSound* fsound = (FishSound*)client_data;
FishSoundFlacInfo *fi = fsound->codec_data;
-#ifdef DEBUG
- printf("fs_flac_enc_write_callback: IN\n");
- printf("fs_flac_enc_write_callback: bytes: %d, samples: %d\n", bytes, samples);
-#endif
+
+ debug_printf(1, "IN");
+ debug_printf(1, "bytes: %d, samples: %d", bytes, samples);
+
if (fsound->callback.encoded) {
FishSoundEncoded encoded = (FishSoundEncoded) fsound->callback.encoded;
if (fi->packetno == 0 && fi->header <= 1) {
@@ -393,10 +364,9 @@ fs_flac_enc_write_callback(const FLAC__StreamEncoder *encoder,
* and a STREAMINFO block. Prepend the FLAC Ogg mapping header,
* as described in http://flac.sourceforge.net/ogg_mapping.html.
*/
-#ifdef DEBUG
- printf("fs_flac_enc_write_callback: generating FLAC header packet: "
- "%c%c%c%c\n", buffer[0], buffer[1], buffer[2], buffer[3]);
-#endif
+ debug_printf(1, "generating FLAC header packet: %c%c%c%c",
+ buffer[0], buffer[1], buffer[2], buffer[3]);
+
if ((fi->buffer = (unsigned char*)fs_malloc(sizeof(unsigned char)*(bytes+9))) == NULL)
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
@@ -448,25 +418,20 @@ fs_flac_enc_meta_callback(const FLAC__StreamEncoder *encoder,
{
/* FishSound* fsound = (FishSound*)client_data; */
/* FishSoundFlacInfo* fi = (FishSoundFlacInfo *)fsound->codec_data; */
-#ifdef DEBUG
- printf("fs_flac_enc_meta_callback: IN\n");
-#endif
+ debug_printf(1, "IN");
+
switch (metadata->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
-#ifdef DEBUG
- printf("fs_flac_enc_meta_callback: channels %d, samplerate %d\n",
+ debug_printf(1, "channels %d, samplerate %d",
metadata->data.stream_info.channels,
metadata->data.stream_info.sample_rate);
-#endif
/*
fsound->info.channels = metadata->data.stream_info.channels;
fsound->info.samplerate = metadata->data.stream_info.sample_rate;
*/
break;
default:
-#ifdef DEBUG
- printf("fs_flac_enc_meta_callback: metadata type not yet implemented\n");
-#endif
+ debug_printf(1, "metadata type not yet implemented");
break;
}
@@ -658,9 +623,7 @@ fs_flac_encode_f (FishSound * fsound, float * pcm[], long frames)
long i;
int j, channels = fsound->info.channels;
-#ifdef DEBUG
- printf("fs_flac_encode_f: IN, frames = %ld\n", frames);
-#endif
+ debug_printf("IN, frames = %ld", frames);
if ((ipcm = realloc(fi->ipcm, sizeof(FLAC__int32) * channels * frames)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY;
@@ -704,9 +667,7 @@ fs_flac_encode_f_ilv (FishSound * fsound, float ** pcm, long frames)
float * p = (float*)pcm, norm = (1 << (BITS_PER_SAMPLE - 1));
long i, length = frames * fsound->info.channels;
-#ifdef DEBUG
- printf("fs_flac_encode_f_ilv: IN, frames = %ld\n", frames);
-#endif
+ debug_printf(1, "IN, frames = %ld", frames);
if ((ipcm = realloc(fi->ipcm, sizeof(FLAC__int32)*fsound->info.channels*frames)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY;
@@ -749,9 +710,7 @@ fs_flac_delete (FishSound * fsound)
FishSoundFlacInfo * fi = (FishSoundFlacInfo *)fsound->codec_data;
int i;
-#ifdef DEBUG
- printf("fs_flac_delete: IN\n");
-#endif
+ debug_printf("IN");
if (fsound->mode == FISH_SOUND_DECODE) {
if (fi->fsd) {
@@ -810,10 +769,7 @@ fs_flac_flush (FishSound * fsound)
{
FishSoundFlacInfo * fi = (FishSoundFlacInfo *)fsound->codec_data;
-#ifdef DEBUG
- printf("fs_flac_flush: IN (%s)\n",
- fsound->mode == FISH_SOUND_DECODE ? "decode" : "encode");
-#endif
+ debug_printf("IN (%s)", fsound->mode == FISH_SOUND_DECODE ? "decode" : "encode");
if (fsound->mode == FISH_SOUND_DECODE) {
FLAC__stream_decoder_finish(fi->fsd);
View
15 src/libfishsound/speex.c
@@ -46,6 +46,7 @@
#include "convert.h"
/*#define DEBUG*/
+#include "debug.h"
#if HAVE_SPEEX
@@ -207,10 +208,10 @@ process_header(unsigned char * buf, long bytes, int enh_enabled,
if (*channels == -1)
*channels = header->nb_channels;
-#ifdef DEBUG
- fprintf (stderr, "Decoding %d Hz audio using %s mode",
- *rate, mode->modeName);
+ debug_printf (1, "Decoding %d Hz audio using %s mode",
+ *rate, mode->modeName);
+#ifdef DEBUG
if (*channels==1)
fprintf (stderr, " (mono");
else
@@ -287,9 +288,7 @@ fs_speex_decode (FishSound * fsound, unsigned char * buf, long bytes)
return FISH_SOUND_ERR_GENERIC;
}
-#ifdef DEBUG
- printf ("speex: got %d channels, %d Hz\n", channels, rate);
-#endif
+ debug_printf (1, "speex: got %d channels, %d Hz", channels, rate);
fsound->info.samplerate = rate;
fsound->info.channels = channels;
@@ -436,9 +435,7 @@ fs_speex_enc_headers (FishSound * fsound)
speex_encoder_ctl (fss->st, SPEEX_GET_FRAME_SIZE, &fss->frame_size);
-#ifdef DEBUG
- printf ("got frame size %d\n", fss->frame_size);
-#endif
+ debug_printf (1, "got frame size %d", fss->frame_size);
/* XXX: set VBR etc. */
View
27 src/libfishsound/vorbis.c
@@ -42,6 +42,7 @@
#include "convert.h"
/*#define DEBUG*/
+#include "debug.h"
#if HAVE_VORBIS
@@ -87,10 +88,8 @@ fish_sound_vorbis_identify (unsigned char * buf, long bytes)
if ((ret = vorbis_synthesis_headerin (&vi, &vc, &op)) == 0) {
if (vi.rate != 0) id = FISH_SOUND_VORBIS;
-#ifdef DEBUG
} else {
- printf ("vorbis_synthesis_headerin returned %d\n", ret);
-#endif
+ debug_printf (1, "vorbis_synthesis_headerin returned %d", ret);
}
vorbis_info_clear (&vi);
@@ -128,10 +127,8 @@ fs_vorbis_decode (FishSound * fsound, unsigned char * buf, long bytes)
if ((ret = vorbis_synthesis_headerin (&fsv->vi, &fsv->vc, &op)) == 0) {
if (fsv->vi.rate != 0) {
-#ifdef DEBUG
- printf ("Got vorbis info: version %d\tchannels %d\trate %ld\n",
- fsv->vi.version, fsv->vi.channels, fsv->vi.rate);
-#endif
+ debug_printf (1, "Got vorbis info: version %d\tchannels %d\trate %ld",
+ fsv->vi.version, fsv->vi.channels, fsv->vi.rate);
fsound->info.samplerate = fsv->vi.rate;
fsound->info.channels = fsv->vi.channels;
}
@@ -224,10 +221,7 @@ fs_vorbis_enc_headers (FishSound * fsound)
/* Update the comments */
for (comment = fish_sound_comment_first (fsound); comment;
comment = fish_sound_comment_next (fsound, comment)) {
-#ifdef DEBUG
- fprintf (stderr, "fs_vorbis_enc_headers: %s = %s\n",
- comment->name, comment->value);
-#endif
+ debug_printf (1, "%s = %s", comment->name, comment->value);
vorbis_comment_add_tag (&fsv->vc, comment->name, comment->value);
}
@@ -356,9 +350,7 @@ fs_vorbis_encode_f (FishSound * fsound, float * pcm[], long frames)
while (remaining > 0) {
len = MIN (1024, remaining);
-#ifdef DEBUG
- printf ("fs_vorbis_encode: processing %ld frames\n", len);
-#endif
+ debug_printf (1, "processing %ld frames", len);
/* expose the buffer to submit data */
vpcm = vorbis_analysis_buffer (&fsv->vd, 1024);
@@ -387,11 +379,8 @@ fs_vorbis_enc_init (FishSound * fsound)
{
FishSoundVorbisInfo * fsv = (FishSoundVorbisInfo *)fsound->codec_data;
-#ifdef DEBUG
- printf ("Vorbis enc init: %d channels, %d Hz\n", fsound->info.channels,
- fsound->info.samplerate);
-#endif
-
+ debug_printf (1, "Vorbis enc init: %d channels, %d Hz", fsound->info.channels,
+ fsound->info.samplerate);
vorbis_encode_init_vbr (&fsv->vi, fsound->info.channels,
fsound->info.samplerate, (float)0.3 /* quality */);

0 comments on commit 72dc184

Please sign in to comment.
Something went wrong with that request. Please try again.