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 --- .gitlab-ci.yml | 1 + HACKING.md | 2 + meson.build | 13 ++++++ 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 +++++++++++++++++++++ src/libtracker-data/tracker-db-interface-sqlite.c | 3 +- 8 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/libtracker-common/tracker-debug.c create mode 100644 src/libtracker-common/tracker-debug.h diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8c62a82ad..a2375a1f1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -37,6 +37,7 @@ stages: echo echo "Test suite settings:" echo + echo "TRACKER_DEBUG: ${TRACKER_DEBUG}" echo "TRACKER_VERBOSITY: ${TRACKER_VERBOSITY}" echo "TRACKER_TESTS_VERBOSE: ${TRACKER_TESTS_VERBOSE}" echo "MESON_TEST_EXTRA_ARGS: ${MESON_TEST_EXTRA_ARGS}" diff --git a/HACKING.md b/HACKING.md index f90522a50..671d48195 100644 --- a/HACKING.md +++ b/HACKING.md @@ -31,6 +31,8 @@ OpenSuSE do automated whole-system testing that includes the GNOME desktop for t The following environment variables control logging from Tracker daemons: + * `TRACKER_DEBUG`: takes a comma-separated list of keywords to enable + extra debugging output. Use the keyword 'help' for a list of keywords. * `TRACKER_VERBOSITY`: takes a value of 1, 2 or 3 and causes increasing amounts of log output from Tracker code to be written to stdout. * `G_MESSAGES_DEBUG`: controls log output from GLib-based libraries that diff --git a/meson.build b/meson.build index 407ff80a5..99a1077ef 100644 --- a/meson.build +++ b/meson.build @@ -72,6 +72,19 @@ add_project_arguments(['-D', 'TRACKER_COMPILATION'], ['-D', 'G_LOG_DOMAIN="Tracker"'], language: 'vala') +debug_cflags = [] +buildtype = get_option('buildtype') +if buildtype.startswith('debug') + debug_cflags += '-DG_ENABLE_DEBUG' + if buildtype == 'debug' + debug_cflags += '-DG_ENABLE_CONSISTENCY_CHECKS' + endif +elif buildtype == 'release' + debug_cflags += '-DG_DISABLE_CAST_CHECKS' +endif + +add_project_arguments(debug_cflags, language: 'c') + ################################################################## # Check for libtracker-common, make sure libstemmer exists ################################################################## 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__ */ diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c b/src/libtracker-data/tracker-db-interface-sqlite.c index fb5bcb46d..545f26690 100644 --- a/src/libtracker-data/tracker-db-interface-sqlite.c +++ b/src/libtracker-data/tracker-db-interface-sqlite.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -2512,7 +2513,7 @@ tracker_db_interface_prepare_stmt (TrackerDBInterface *db_interface, sqlite3_stmt *sqlite_stmt; int retval; - g_debug ("Preparing query: '%s'", full_query); + TRACKER_NOTE (SQL_STATEMENTS, g_message ("Preparing query: '%s'", full_query)); retval = sqlite3_prepare_v2 (db_interface->db, full_query, -1, &sqlite_stmt, NULL); if (retval != SQLITE_OK) { -- cgit v1.2.1