Skip to content

Commit

Permalink
#2287, move debug messages in --enable-debug builds to the DEBUG PgSQ…
Browse files Browse the repository at this point in the history
…L logging channel

git-svn-id: http://svn.osgeo.org/postgis/trunk@13617 b70326c6-7e19-0410-871a-916f4a2858ee
  • Loading branch information
pramsey committed Jun 4, 2015
1 parent be80083 commit 75e4f97
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 29 deletions.
2 changes: 1 addition & 1 deletion liblwgeom/cunit/cu_tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int main(int argc, char *argv[])
PG_SuiteSetup *setupfunc = setupfuncs;

/* Install the custom error handler */
lwgeom_set_handlers(0, 0, 0, cu_errorreporter, 0);
lwgeom_set_handlers(0, 0, 0, cu_errorreporter, 0, 0);

/* Initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
Expand Down
12 changes: 11 additions & 1 deletion liblwgeom/liblwgeom.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ typedef void* (*lwallocator)(size_t size);
typedef void* (*lwreallocator)(void *mem, size_t size);
typedef void (*lwfreeor)(void* mem);
typedef void (*lwreporter)(const char* fmt, va_list ap);
typedef void (*lwdebuglogger)(int level, const char* fmt, va_list ap);

/**
* Install custom memory management and error handling functions you want your
Expand All @@ -202,7 +203,7 @@ typedef void (*lwreporter)(const char* fmt, va_list ap);
*/
extern void lwgeom_set_handlers(lwallocator allocator,
lwreallocator reallocator, lwfreeor freeor, lwreporter errorreporter,
lwreporter noticereporter);
lwreporter noticereporter, lwdebuglogger debuglogger);

/**
* Request interruption of any running code
Expand Down Expand Up @@ -256,6 +257,15 @@ void lwnotice(const char *fmt, ...);
*/
void lwerror(const char *fmt, ...);

/**
* Write a debug message out.
* Don't call this function directly, use the
* macros, LWDEBUG() or LWDEBUGF(), for
* efficiency.
* @ingroup logging
*/
void lwdebug(int level, const char *fmt, ...);


/* TODO: move these elsewhere */
extern int lw_vasprintf (char **result, const char *format, va_list args);
Expand Down
4 changes: 2 additions & 2 deletions liblwgeom/lwgeom_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
#define LWDEBUG(level, msg) \
do { \
if (POSTGIS_DEBUG_LEVEL >= level) \
lwnotice("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__); \
lwdebug(level, "[%s:%s:%d] " msg, __FILE__, __func__, __LINE__); \
} while (0);

/* Display a formatted notice at the given debug level
* (like printf, with variadic arguments) */
#define LWDEBUGF(level, msg, ...) \
do { \
if (POSTGIS_DEBUG_LEVEL >= level) \
lwnotice("[%s:%s:%d] " msg, \
lwdebug(level, "[%s:%s:%d] " msg, \
__FILE__, __func__, __LINE__, __VA_ARGS__); \
} while (0);

Expand Down
36 changes: 35 additions & 1 deletion liblwgeom/lwutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ static void default_errorreporter(const char *fmt, va_list ap);
lwreporter lwnotice_var = default_noticereporter;
lwreporter lwerror_var = default_errorreporter;

/* Default logger */
static void default_debuglogger(int level, const char *fmt, va_list ap);
lwdebuglogger lwdebug_var = default_debuglogger;

#define LW_MSG_MAXLEN 256

static char *lwgeomTypeName[] =
Expand Down Expand Up @@ -79,6 +83,19 @@ lwerror(const char *fmt, ...)
va_end(ap);
}

void
lwdebug(int level, const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);

/* Call the supplied function */
(*lwdebug_var)(level, fmt, ap);

va_end(ap);
}

/*
* Default allocators
*
Expand Down Expand Up @@ -116,6 +133,22 @@ default_noticereporter(const char *fmt, va_list ap)
printf("%s\n", msg);
}

static void
default_debuglogger(int level, const char *fmt, va_list ap)
{
char msg[LW_MSG_MAXLEN+1];
if ( POSTGIS_DEBUG_LEVEL >= level )
{
/* Space pad the debug output */
int i;
for ( i = 0; i < level; i++ )
msg[i] = ' ';
vsnprintf(msg+i, LW_MSG_MAXLEN-i, fmt, ap);
msg[LW_MSG_MAXLEN]='\0';
printf("%s\n", msg);
}
}

static void
default_errorreporter(const char *fmt, va_list ap)
{
Expand All @@ -135,14 +168,15 @@ default_errorreporter(const char *fmt, va_list ap)
void
lwgeom_set_handlers(lwallocator allocator, lwreallocator reallocator,
lwfreeor freeor, lwreporter errorreporter,
lwreporter noticereporter) {
lwreporter noticereporter, lwdebuglogger debuglogger) {

if ( allocator ) lwalloc_var = allocator;
if ( reallocator ) lwrealloc_var = reallocator;
if ( freeor ) lwfree_var = freeor;

if ( errorreporter ) lwerror_var = errorreporter;
if ( noticereporter ) lwnotice_var = noticereporter;
if ( debuglogger ) lwdebug_var = debuglogger;
}

const char*
Expand Down
16 changes: 15 additions & 1 deletion libpgcommon/lwgeom_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,25 @@ pg_notice(const char *fmt, va_list ap)
ereport(NOTICE, (errmsg_internal("%s", errmsg)));
}

static void
pg_debug(int level, const char *fmt, va_list ap)
{
char errmsg[PGC_ERRMSG_MAXLEN+1];
vsnprintf (errmsg, PGC_ERRMSG_MAXLEN, fmt, ap);
errmsg[PGC_ERRMSG_MAXLEN]='\0';
int pglevel[6] = {NOTICE, DEBUG1, DEBUG2, DEBUG3, DEBUG4, DEBUG5};

if ( level >= 0 && level <= 5 )
ereport(pglevel[level], (errmsg_internal("%s", errmsg)));
else
ereport(DEBUG5, (errmsg_internal("%s", errmsg)));
}

void
pg_install_lwgeom_handlers(void)
{
/* install PostgreSQL handlers */
lwgeom_set_handlers(pg_alloc, pg_realloc, pg_free, pg_error, pg_notice);
lwgeom_set_handlers(pg_alloc, pg_realloc, pg_free, pg_error, pg_notice, pg_debug);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions libpgcommon/lwgeom_pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ void pg_install_lwgeom_handlers(void);
#if POSTGIS_DEBUG_LEVEL > 0

/* Display a simple message at NOTICE level */
/* from PgSQL utils/elog.h, LOG is 15, and DEBUG5 is 10 and everything else is in between */
#define POSTGIS_DEBUG(level, msg) \
do { \
if (POSTGIS_DEBUG_LEVEL >= level) \
ereport(NOTICE, (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__))); \
ereport((level < 1 || level > 5) ? DEBUG5 : (LOG - level), (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__))); \
} while (0);

/* Display a formatted message at NOTICE level (like printf, with variadic arguments) */
#define POSTGIS_DEBUGF(level, msg, ...) \
do { \
if (POSTGIS_DEBUG_LEVEL >= level) \
ereport(NOTICE, (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__))); \
ereport((level < 1 || level > 5) ? DEBUG5 : (LOG - level), (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__))); \
} while (0);

#else

#else /* POSTGIS_DEBUG_LEVEL */
/* Empty prototype that can be optimised away by the compiler for non-debug builds */
#define POSTGIS_DEBUG(level, msg) \
((void) 0)
Expand All @@ -57,7 +58,7 @@ void pg_install_lwgeom_handlers(void);
#define POSTGIS_DEBUGF(level, msg, ...) \
((void) 0)

#endif
#endif /* POSTGIS_DEBUG_LEVEL */


/*
Expand Down
14 changes: 7 additions & 7 deletions postgis/lwgeom_functions_lrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ ptarray_locate_between_m(POINTARRAY *ipa, double m0, double m1)
*/
ret.ptarrays=lwalloc(sizeof(POINTARRAY *)*ipa->npoints-1);

POSTGIS_DEBUGF(2, "ptarray_locate...: called for pointarray %x, m0:%g, m1:%g",
POSTGIS_DEBUGF(2, "ptarray_locate...: called for pointarray %p, m0:%g, m1:%g",
ipa, m0, m1);


Expand All @@ -464,7 +464,7 @@ ptarray_locate_between_m(POINTARRAY *ipa, double m0, double m1)
/* segment completely outside, nothing to do */
if (! clipval ) continue;

POSTGIS_DEBUGF(3, " clipped to: [ %g %g %g %g - %g %g %g %g ] clipval: %x", p1.x, p1.y, p1.z, p1.m,
POSTGIS_DEBUGF(3, " clipped to: [ %g %g %g %g - %g %g %g %g ] clipval: %d", p1.x, p1.y, p1.z, p1.m,
p2.x, p2.y, p2.z, p2.m, clipval);

/* If no points have been accumulated so far, then if clipval != 0 the first point must be the
Expand All @@ -486,7 +486,7 @@ ptarray_locate_between_m(POINTARRAY *ipa, double m0, double m1)
*/
if ( clipval & 0x0100 || i == ipa->npoints-1 )
{
POSTGIS_DEBUGF(3, " closing pointarray %x with %d points", dpa, dpa->npoints);
POSTGIS_DEBUGF(3, " closing pointarray %p with %d points", dpa, dpa->npoints);

ret.ptarrays[ret.nptarrays++] = dpa;
dpa = NULL;
Expand All @@ -512,7 +512,7 @@ lwpoint_locate_between_m(LWPOINT *lwpoint, double m0, double m1)
{
POINT3DM p3dm;

POSTGIS_DEBUGF(2, "lwpoint_locate_between called for lwpoint %x", lwpoint);
POSTGIS_DEBUGF(2, "lwpoint_locate_between called for lwpoint %p", lwpoint);

lwpoint_getPoint3dm_p(lwpoint, &p3dm);
if ( p3dm.m >= m0 && p3dm.m <= m1)
Expand Down Expand Up @@ -553,7 +553,7 @@ lwline_locate_between_m(LWLINE *lwline_in, double m0, double m1)
const int lineflag=0x10;
POINTARRAYSET paset=ptarray_locate_between_m(ipa, m0, m1);

POSTGIS_DEBUGF(2, "lwline_locate_between called for lwline %x", lwline_in);
POSTGIS_DEBUGF(2, "lwline_locate_between called for lwline %p", lwline_in);

POSTGIS_DEBUGF(3, " ptarray_locate... returned %d pointarrays",
paset.nptarrays);
Expand Down Expand Up @@ -624,7 +624,7 @@ lwcollection_locate_between_m(LWCOLLECTION *lwcoll, double m0, double m1)
int ngeoms=0;
LWGEOM **geoms;

POSTGIS_DEBUGF(2, "lwcollection_locate_between_m called for lwcoll %x", lwcoll);
POSTGIS_DEBUGF(2, "lwcollection_locate_between_m called for lwcoll %p", lwcoll);

geoms=lwalloc(sizeof(LWGEOM *)*lwcoll->ngeoms);
for (i=0; i<lwcoll->ngeoms; i++)
Expand Down Expand Up @@ -653,7 +653,7 @@ lwcollection_locate_between_m(LWCOLLECTION *lwcoll, double m0, double m1)
static LWGEOM *
lwgeom_locate_between_m(LWGEOM *lwin, double m0, double m1)
{
POSTGIS_DEBUGF(2, "lwgeom_locate_between called for lwgeom %x", lwin);
POSTGIS_DEBUGF(2, "lwgeom_locate_between called for lwgeom %p", lwin);

switch (lwin->type)
{
Expand Down
7 changes: 0 additions & 7 deletions postgis/lwgeom_geos.c
Original file line number Diff line number Diff line change
Expand Up @@ -3261,23 +3261,16 @@ Datum GEOSnoop(PG_FUNCTION_ARGS)
GSERIALIZED *geom;
GEOSGeometry *geosgeom;
GSERIALIZED *lwgeom_result;
#if POSTGIS_DEBUG_LEVEL > 0
int result;
LWGEOM_UNPARSER_RESULT lwg_unparser_result;
#endif

initGEOS(lwnotice, lwgeom_geos_error);

geom = PG_GETARG_GSERIALIZED_P(0);


geosgeom = (GEOSGeometry *)POSTGIS2GEOS(geom);
if ( ! geosgeom ) PG_RETURN_NULL();

lwgeom_result = GEOS2POSTGIS(geosgeom, gserialized_has_z(geom));
GEOSGeom_destroy(geosgeom);


PG_FREE_IF_COPY(geom, 0);

PG_RETURN_POINTER(lwgeom_result);
Expand Down
13 changes: 12 additions & 1 deletion raster/rt_pg/rtpostgis.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,22 @@ rt_pg_notice(const char *fmt, va_list ap)
ereport(NOTICE, (errmsg_internal("%s", msg)));
}

static void
rt_pg_debug(const char *fmt, va_list ap)
{
char msg[RT_MSG_MAXLEN+1];

vsnprintf (msg, RT_MSG_MAXLEN, fmt, ap);

msg[RT_MSG_MAXLEN]='\0';
ereport(DEBUG1, (errmsg_internal("%s", msg)));
}


void
rt_init_allocators(void)
{
/* raster callback - install raster handlers */
rt_set_handlers(rt_pg_alloc, rt_pg_realloc, rt_pg_free, rt_pg_error,
rt_pg_notice, rt_pg_notice);
rt_pg_notice, rt_pg_debug);
}
4 changes: 2 additions & 2 deletions raster/rt_pg/rtpostgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
#define POSTGIS_RT_DEBUG(level, msg) \
do { \
if (POSTGIS_DEBUG_LEVEL >= level) \
ereport(NOTICE, (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__))); \
ereport((level < 1 || level > 5) ? DEBUG5 : (LOG - level), (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__))); \
} while (0);

/* Display a formatted message at NOTICE level (like printf, with variadic arguments) */
#define POSTGIS_RT_DEBUGF(level, msg, ...) \
do { \
if (POSTGIS_DEBUG_LEVEL >= level) \
ereport(NOTICE, (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__))); \
ereport((level < 1 || level > 5) ? DEBUG5 : (LOG - level), (errmsg_internal("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__))); \
} while (0);

#else
Expand Down
2 changes: 1 addition & 1 deletion raster/test/cunit/cu_tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main(int argc, char *argv[])
PG_SuiteSetup *setupfunc = setupfuncs;

/* install the custom error handler */
lwgeom_set_handlers(0, 0, 0, cu_error_reporter, 0);
lwgeom_set_handlers(0, 0, 0, cu_error_reporter, 0, 0);

/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
Expand Down

0 comments on commit 75e4f97

Please sign in to comment.