debug: Infrastructure

Add some debug infrastructure.
Disabled by default, enable with --enable-debug.
When enabled, the environment variable IMLIB2_DEBUG controls the amount
of debug output.
This commit is contained in:
Kim Woelders 2019-11-16 14:51:44 +01:00
parent 5a2490eb7f
commit e47e32a247
10 changed files with 131 additions and 24 deletions

View File

@ -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()

View File

@ -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)

View File

@ -10,6 +10,14 @@
#include <math.h>
#include <time.h>
#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

71
src/lib/debug.c Normal file
View File

@ -0,0 +1,71 @@
#include "common.h"
#include <stdarg.h>
#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 <time.h>
#else
#include <sys/time.h>
#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 */

32
src/lib/debug.h Normal file
View File

@ -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 */

View File

@ -10,12 +10,7 @@
#include "loader_common.h"
#include <sys/stat.h>
#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) */

View File

@ -3,6 +3,7 @@
#include "config.h"
#include "common.h"
#include "debug.h"
#include "image.h"
__EXPORT__ char load(ImlibImage * im, ImlibProgressFunction progress,

View File

@ -8,12 +8,7 @@
#include "loader_common.h"
#include <limits.h>
#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 {

View File

@ -3,12 +3,7 @@
#include <setjmp.h>
#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;

View File

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