diff --git a/configure.ac b/configure.ac index 9d8888a..4463a8c 100644 --- a/configure.ac +++ b/configure.ac @@ -379,6 +379,16 @@ AM_CONDITIONAL(BUILD_ID3_LOADER, test "$id3_ok" = yes) AM_CONDITIONAL(BUILD_TEST, false) +AC_ARG_ENABLE([debug], + [AS_HELP_STRING([--enable-debug], [Enable debug features @<:@default=no@:>@])], + [ + if test x$enableval = xyes; then + AC_DEFINE(IMLIB2_DEBUG, 1, [Enable debug features]) + fi + ] +) +AM_CONDITIONAL(BUILD_DEBUG, test "$enable_debug" = "yes") + EC_C_WARNINGS() EC_C_VISIBILITY(yes) EC_C_ASAN() diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index bfff8e1..340249e 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -25,6 +25,8 @@ colormod.h \ common.h \ context.c \ context.h \ +debug.c \ +debug.h \ draw.c \ draw.h \ dynamic_filters.c \ @@ -91,6 +93,9 @@ MY_LIBS = $(FREETYPE_LIBS) $(DLOPEN_LIBS) -lm if BUILD_X11 MY_LIBS += -lXext -lX11 @X_SHM_FD_LIBS@ endif +if BUILD_DEBUG +MY_LIBS += $(CLOCK_LIBS) +endif if BUILD_MMX libImlib2_la_LIBADD = $(MMX_OBJS) $(MY_LIBS) diff --git a/src/lib/common.h b/src/lib/common.h index 0df2338..3b5ec76 100644 --- a/src/lib/common.h +++ b/src/lib/common.h @@ -10,6 +10,14 @@ #include #include +#if __GNUC__ +#define __PRINTF_N__(no) __attribute__((__format__(__printf__, (no), (no)+1))) +#else +#define __PRINTF_N__(no) +#endif +#define __PRINTF__ __PRINTF_N__(1) +#define __PRINTF_2__ __PRINTF_N__(2) + #define DATABIG unsigned long long #define DATA64 unsigned long long #define DATA32 unsigned int diff --git a/src/lib/debug.c b/src/lib/debug.c new file mode 100644 index 0000000..e842249 --- /dev/null +++ b/src/lib/debug.c @@ -0,0 +1,71 @@ +#include "common.h" +#include + +#include "debug.h" + +#if IMLIB2_DEBUG + +unsigned int __imlib_debug = 0; + +static FILE *opt_fout = NULL; + +__attribute__((constructor)) + static void _debug_init(void) +{ + const char *s; + int p1, p2; + + opt_fout = stdout; + + s = getenv("IMLIB2_DEBUG"); + if (!s) + return; + + sscanf(s, "%d:%d", &p1, &p2); + + __imlib_debug = p1; + opt_fout = (p2) ? stderr : stdout; +} + +#if USE_MONOTONIC_CLOCK +#include +#else +#include +#endif + +unsigned int +__imlib_time_us(void) +{ +#if USE_MONOTONIC_CLOCK + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + return (unsigned int)(ts.tv_sec * 1000000 + ts.tv_nsec / 1000); +#else + struct timeval timev; + + gettimeofday(&timev, NULL); + + return (unsigned int)(timev.tv_sec * 1000000 + timev.tv_usec); +#endif +} + +void +__imlib_printf(const char *pfx, const char *fmt, ...) +{ + char fmtx[1024]; + va_list args; + + va_start(args, fmt); + + if (pfx) + { + snprintf(fmtx, sizeof(fmtx), "%-4s: %s", pfx, fmt); + fmt = fmtx; + } + vfprintf(opt_fout, fmt, args); + va_end(args); +} + +#endif /* IMLIB2_DEBUG */ diff --git a/src/lib/debug.h b/src/lib/debug.h new file mode 100644 index 0000000..f6bab08 --- /dev/null +++ b/src/lib/debug.h @@ -0,0 +1,32 @@ +#ifndef IMLIB2_DEDUG_H +#define IMLIB2_DEDUG_H + +#if IMLIB2_DEBUG + +#define D(fmt...) if (__imlib_debug) __imlib_printf(DBG_PFX, fmt) +#define DC(M, fmt...) if (__imlib_debug & M) __imlib_printf(DBG_PFX, fmt) + +#define DBG_FILE 0x0001 +#define DBG_LOAD 0x0002 +#define DBG_LDR 0x0004 + +#if __LOADER_COMMON_H +#undef D +#define D(fmt...) DC(DBG_LDR, fmt) +#endif + +__EXPORT__ extern unsigned int __imlib_debug; + +__EXPORT__ __PRINTF_2__ void __imlib_printf(const char *pfx, + const char *fmt, ...); + +unsigned int __imlib_time_us(void); + +#else + +#define D(fmt...) +#define DC(fmt...) + +#endif /* IMLIB2_DEBUG */ + +#endif /* IMLIB2_DEDUG_H */ diff --git a/src/modules/loaders/loader_bmp.c b/src/modules/loaders/loader_bmp.c index 62a501e..484b5c4 100644 --- a/src/modules/loaders/loader_bmp.c +++ b/src/modules/loaders/loader_bmp.c @@ -10,12 +10,7 @@ #include "loader_common.h" #include -#define DEBUG 0 -#if DEBUG -#define D(fmt...) fprintf(stdout, "BMP loader: " fmt) -#else -#define D(fmt...) -#endif +#define DBG_PFX "LDR-bmp" #define Dx(fmt...) /* The BITMAPFILEHEADER (size 14) */ diff --git a/src/modules/loaders/loader_common.h b/src/modules/loaders/loader_common.h index d1716c9..512ade8 100644 --- a/src/modules/loaders/loader_common.h +++ b/src/modules/loaders/loader_common.h @@ -3,6 +3,7 @@ #include "config.h" #include "common.h" +#include "debug.h" #include "image.h" __EXPORT__ char load(ImlibImage * im, ImlibProgressFunction progress, diff --git a/src/modules/loaders/loader_ico.c b/src/modules/loaders/loader_ico.c index fc69192..0aa739f 100644 --- a/src/modules/loaders/loader_ico.c +++ b/src/modules/loaders/loader_ico.c @@ -8,12 +8,7 @@ #include "loader_common.h" #include -#define DEBUG 0 -#if DEBUG -#define D(fmt...) fprintf(stdout, "ICO loader: " fmt) -#else -#define D(fmt...) -#endif +#define DBG_PFX "LDR-ico" /* The ICONDIR */ typedef struct { diff --git a/src/modules/loaders/loader_jpeg.c b/src/modules/loaders/loader_jpeg.c index 1c9501a..831d6ca 100644 --- a/src/modules/loaders/loader_jpeg.c +++ b/src/modules/loaders/loader_jpeg.c @@ -3,12 +3,7 @@ #include #include "exif.h" -#define DEBUG 0 -#if DEBUG -#define D(fmt...) fprintf(stdout, "JPEG loader: " fmt) -#else -#define D(fmt...) -#endif +#define DBG_PFX "LDR-jpg" typedef struct { struct jpeg_error_mgr jem; diff --git a/src/modules/loaders/loader_xbm.c b/src/modules/loaders/loader_xbm.c index e7c648a..af8b21c 100644 --- a/src/modules/loaders/loader_xbm.c +++ b/src/modules/loaders/loader_xbm.c @@ -3,12 +3,7 @@ */ #include "loader_common.h" -#define DEBUG 0 -#if DEBUG -#define D(fmt...) fprintf(stdout, "XBM loader: " fmt) -#else -#define D(fmt...) -#endif +#define DBG_PFX "LDR-xbm" static const DATA32 _bitmap_colors[2] = { 0xffffffff, 0xff000000 };