From: Pierre-Luc Beaudoin Date: Fri, 30 Jan 2009 17:42:17 +0000 (+0200) Subject: Add DEBUG macros X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66b39d5c5f8d75196fbcde016829e2b4376c4c25;p=libchamplain Add DEBUG macros --- diff --git a/champlain/Makefile.am b/champlain/Makefile.am index 19208c2..462c5a0 100644 --- a/champlain/Makefile.am +++ b/champlain/Makefile.am @@ -13,6 +13,7 @@ lib_LTLIBRARIES = libchamplain-0.2.la libchamplain_0_2_la_SOURCES = \ $(CHAMPLAIN_MARSHAL_LIST) \ $(BUILT_SOURCES) \ + champlain-debug.c \ champlain-view.c \ champlain-layer.c \ champlain-marker.c \ @@ -28,6 +29,7 @@ libchamplain_0_2_la_SOURCES = \ sources/debugmap.c noinst_HEADERS = \ + champlain-debug.h \ champlain-view.h \ champlain-marker.c \ champlain-private.h \ diff --git a/champlain/champlain-debug.c b/champlain/champlain-debug.c new file mode 100644 index 0000000..3c097ef --- /dev/null +++ b/champlain/champlain-debug.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2007 Collabora Ltd. + * Copyright (C) 2007 Nokia Corporation + * Copyright (C) 2008-2009 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "champlain-debug.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_DEBUG + +static ChamplainDebugFlags flags = 0; + +static GDebugKey keys[] = { + { "Loading", CHAMPLAIN_DEBUG_LOADING }, + { "Engine", CHAMPLAIN_DEBUG_ENGINE }, + { "View", CHAMPLAIN_DEBUG_VIEW }, + { "Other", CHAMPLAIN_DEBUG_OTHER }, + { 0, } +}; + +static void +debug_set_flags (ChamplainDebugFlags new_flags) +{ + flags |= new_flags; +} + +void +champlain_debug_set_flags (const gchar *flags_string) +{ + guint nkeys; + + for (nkeys = 0; keys[nkeys].value; nkeys++); + + if (flags_string) + debug_set_flags (g_parse_debug_string (flags_string, keys, nkeys)); +} + +gboolean +champlain_debug_flag_is_set (ChamplainDebugFlags flag) +{ + return (flag & flags) != 0; +} + +void +champlain_debug (ChamplainDebugFlags flag, + const gchar *format, + ...) +{ + if (flag & flags) + { + va_list args; + va_start (args, format); + g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args); + va_end (args); + } +} + +#else + +gboolean +champlain_debug_flag_is_set (ChamplainDebugFlags flag) +{ + return FALSE; +} + +void +champlain_debug (ChamplainDebugFlags flag, const gchar *format, ...) +{ +} + +void +champlain_debug_set_flags (const gchar *flags_string) +{ +} + +#endif /* ENABLE_DEBUG */ + diff --git a/champlain/champlain-debug.h b/champlain/champlain-debug.h new file mode 100644 index 0000000..d60f3ba --- /dev/null +++ b/champlain/champlain-debug.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2007 Collabora Ltd. + * Copyright (C) 2007 Nokia Corporation + * Copyright (C) 2008-2009 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __CHAMPLAIN_DEBUG_H__ +#define __CHAMPLAIN_DEBUG_H__ + +#include "config.h" + +#include + +G_BEGIN_DECLS + +/* Please keep this enum in sync with #keys in champlain-debug.c */ +typedef enum +{ + CHAMPLAIN_DEBUG_LOADING = 1 << 1, + CHAMPLAIN_DEBUG_ENGINE = 1 << 2, + CHAMPLAIN_DEBUG_VIEW = 1 << 3, + CHAMPLAIN_DEBUG_OTHER = 1 << 4, +} ChamplainDebugFlags; + +gboolean champlain_debug_flag_is_set (ChamplainDebugFlags flag); +void champlain_debug (ChamplainDebugFlags flag, const gchar *format, ...) + G_GNUC_PRINTF (2, 3); +void champlain_debug_set_flags (const gchar *flags_string); +G_END_DECLS + +#endif /* __CHAMPLAIN_DEBUG_H__ */ + +/* ------------------------------------ */ + +/* Below this point is outside the __DEBUG_H__ guard - so it can take effect + * more than once. So you can do: + * + * #define DEBUG_FLAG CHAMPLAIN_DEBUG_ONE_THING + * #include "debug.h" + * ... + * DEBUG ("if we're debugging one thing"); + * ... + * #undef DEBUG_FLAG + * #define DEBUG_FLAG CHAMPLAIN_DEBUG_OTHER_THING + * #include "debug.h" + * ... + * DEBUG ("if we're debugging the other thing"); + * ... + */ + +#ifdef DEBUG_FLAG +#ifdef ENABLE_DEBUG + +#undef DEBUG +#define DEBUG(format, ...) \ + champlain_debug (DEBUG_FLAG, "%s: " format, G_STRFUNC, ##__VA_ARGS__) + +#undef DEBUGGING +#define DEBUGGING champlain_debug_flag_is_set (DEBUG_FLAG) + +#else /* !defined (ENABLE_DEBUG) */ + +#undef DEBUG +#define DEBUG(format, ...) do {} while (0) + +#undef DEBUGGING +#define DEBUGGING 0 + +#endif /* !defined (ENABLE_DEBUG) */ +#endif /* defined (DEBUG_FLAG) */ diff --git a/configure.ac b/configure.ac index c1d0c45..8ce6619 100644 --- a/configure.ac +++ b/configure.ac @@ -39,9 +39,22 @@ PKG_CHECK_MODULES(DEPS, # check for gtk-doc GTK_DOC_CHECK(1.9) +# ----------------------------------------------------------- +# Enable debug +# ----------------------------------------------------------- + +AC_ARG_ENABLE(debug, + AC_HELP_STRING([--disable-debug],[compile without debug code]), + enable_debug=$enableval, enable_debug=yes ) + +if test x$enable_debug = xyes; then + AC_DEFINE(ENABLE_DEBUG, [], [Enable debug code]) +fi +# ----------------------------------------------------------- + AC_CONFIG_FILES([Makefile champlain/Makefile - demos/Makefile + demos/Makefile tidy/Makefile docs/reference/Makefile docs/reference/version.xml @@ -53,4 +66,5 @@ echo " libchamplain $VERSION" echo "" echo " Prefix: ${prefix}" echo " Compiler flags: ${CPPFLAGS}" +echo " Debug: ${enable_debug}" echo ""