From ca1a0f3c28fdbc3dae2c7bd6019fdcacb021e610 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Mon, 20 Apr 2020 00:29:47 +0200 Subject: Add TRACKER_DEBUG environment variable, use for SQL debug This works the same as GTK's GTK_DEBUG variable. It will allow us to include more types of optional debugging info, and will make the default debug output more readlable. See https://gitlab.gnome.org/GNOME/tracker/issues/178 --- src/libtracker-common/meson.build | 1 + src/libtracker-common/tracker-common.h | 1 + src/libtracker-common/tracker-debug.c | 57 ++++++++++++++++++++++++++++++++++ src/libtracker-common/tracker-debug.h | 54 ++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 src/libtracker-common/tracker-debug.c create mode 100644 src/libtracker-common/tracker-debug.h (limited to 'src/libtracker-common') diff --git a/src/libtracker-common/meson.build b/src/libtracker-common/meson.build index 3ec6caec0..11e610d09 100644 --- a/src/libtracker-common/meson.build +++ b/src/libtracker-common/meson.build @@ -12,6 +12,7 @@ tracker_common_enum_header = enums[1] tracker_common_sources = [ 'tracker-date-time.c', + 'tracker-debug.c', 'tracker-file-utils.c', 'tracker-log.c', 'tracker-type-utils.c', diff --git a/src/libtracker-common/tracker-common.h b/src/libtracker-common/tracker-common.h index 8d5df2be9..0a9267ed9 100644 --- a/src/libtracker-common/tracker-common.h +++ b/src/libtracker-common/tracker-common.h @@ -29,6 +29,7 @@ #define __LIBTRACKER_COMMON_INSIDE__ #include "tracker-date-time.h" +#include "tracker-debug.h" #include "tracker-file-utils.h" #include "tracker-language.h" #include "tracker-log.h" diff --git a/src/libtracker-common/tracker-debug.c b/src/libtracker-common/tracker-debug.c new file mode 100644 index 000000000..647b78b09 --- /dev/null +++ b/src/libtracker-common/tracker-debug.c @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2020, Sam Thursfield + * + * 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 Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" + +#include "tracker-debug.h" + +#ifdef G_ENABLE_DEBUG +static const GDebugKey tracker_debug_keys[] = { + { "sql-statements", TRACKER_DEBUG_SQL_STATEMENTS }, +}; +#endif /* G_ENABLE_DEBUG */ + +static gpointer +parse_debug_flags () +{ + const gchar *env_string; + guint flags = 0; + + env_string = g_getenv ("TRACKER_DEBUG"); + if (env_string != NULL) { +#ifdef G_ENABLE_DEBUG + flags = g_parse_debug_string (env_string, tracker_debug_keys, G_N_ELEMENTS (tracker_debug_keys)); +#else + g_warning ("TRACKER_DEBUG set but ignored because tracker isn't built with G_ENABLE_DEBUG"); +#endif /* G_ENABLE_DEBUG */ + env_string = NULL; + } + + return GINT_TO_POINTER (flags); +} + +guint +tracker_get_debug_flags (void) +{ + static GOnce once = G_ONCE_INIT; + + g_once (&once, parse_debug_flags, NULL); + + return GPOINTER_TO_INT (once.retval); +} diff --git a/src/libtracker-common/tracker-debug.h b/src/libtracker-common/tracker-debug.h new file mode 100644 index 000000000..aaca4fc07 --- /dev/null +++ b/src/libtracker-common/tracker-debug.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2020, Sam Thursfield + * + * 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 Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __TRACKER_DEBUG_H__ +#define __TRACKER_DEBUG_H__ + +#if !defined (__LIBTRACKER_COMMON_INSIDE__) && !defined (TRACKER_COMPILATION) +#error "only must be included directly." +#endif + +#include + +G_BEGIN_DECLS + +typedef enum { + TRACKER_DEBUG_SQL_STATEMENTS = 1 << 1, +} TrackerDebugFlag; + +#ifdef G_ENABLE_DEBUG + +#define TRACKER_DEBUG_CHECK(type) G_UNLIKELY (tracker_get_debug_flags () & TRACKER_DEBUG_##type) + +#define TRACKER_NOTE(type,action) G_STMT_START { \ + if (TRACKER_DEBUG_CHECK (type)) \ + { action; }; } G_STMT_END + +#else /* !G_ENABLE_DEBUG */ + +#define TRACKER_DEBUG_CHECK(type) 0 +#define TRACKER_NOTE(type, action) + +#endif /* G_ENABLE_DEBUG */ + +guint tracker_get_debug_flags (void); + +G_END_DECLS + +#endif /* __TRACKER_DEBUG_H__ */ -- cgit v1.2.1