[tracker] libtracker-common: Don't log to file by default, only stdout/stderr



commit 5ed0bbc68e356d924198790e19cbf22d6a5ff8f0
Author: Martyn Russell <martyn lanedo com>
Date:   Fri Mar 8 15:42:42 2013 +0000

    libtracker-common: Don't log to file by default, only stdout/stderr
    
    This is actually only useful for debugging anyway and can be switched on again
    by starting tracker processes by setting the environment variable
    TRACKER_USE_LOG_FILES before starting each process.
    
    Log files can still be found in either ~/.xsession-errors,
    ~/.cache/gdm/session.log or systemd journals depending on the system.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=695444

 docs/manpages/tracker-miner-fs.1               |    7 ++
 docs/manpages/tracker-store.1                  |    9 ++
 docs/reference/libtracker-sparql/overview.sgml |   10 +++
 src/libtracker-common/tracker-log.c            |   97 ++++++++++++++++--------
 4 files changed, 92 insertions(+), 31 deletions(-)
---
diff --git a/docs/manpages/tracker-miner-fs.1 b/docs/manpages/tracker-miner-fs.1
index 73b957a..145d8f2 100644
--- a/docs/manpages/tracker-miner-fs.1
+++ b/docs/manpages/tracker-miner-fs.1
@@ -65,6 +65,13 @@ file system).
 
 .SH ENVIRONMENT
 .TP
+.B TRACKER_USE_LOG_FILES
+Don't just log to stdout and stderr, but to log files too which are
+kept in $HOME/.local/share/tracker/. This came into effect in 0.15.3
+and 0.16.0. After this version of Tracker, logging to file (usually
+useful for debugging) can only be done by declaring this environment
+variable.
+.TP
 .B TRACKER_USE_CONFIG_FILES
 Don't use GSettings, instead use a config file similar to how settings
 were saved in 0.10.x. That is, a file which is much like an .ini file.
diff --git a/docs/manpages/tracker-store.1 b/docs/manpages/tracker-store.1
index 3475b06..aca16cb 100644
--- a/docs/manpages/tracker-store.1
+++ b/docs/manpages/tracker-store.1
@@ -93,6 +93,15 @@ If Tracker is compiled with meegotouch locale management support, setting this
 variable in the environment will disable retrieving the locale from GConf, and
 the standard locale settings will be used instead.
 
+.TP
+.B TRACKER_USE_LOG_FILES
+Don't just log to stdout and stderr, but to log files too which are
+kept in $HOME/.local/share/tracker/. This came into effect in 0.15.3
+and 0.16.0. After this version of Tracker, logging to file (usually
+useful for debugging) can only be done by declaring this environment
+variable.
+
+.TP
 .B TRACKER_USE_CONFIG_FILES
 Don't use GSettings, instead use a config file similar to how settings
 were saved in 0.10.x. That is, a file which is much like an .ini file.
diff --git a/docs/reference/libtracker-sparql/overview.sgml b/docs/reference/libtracker-sparql/overview.sgml
index 83d7a67..586c115 100644
--- a/docs/reference/libtracker-sparql/overview.sgml
+++ b/docs/reference/libtracker-sparql/overview.sgml
@@ -102,6 +102,16 @@ $ pkg-config --libs tracker-sparql-0.12
 
       <itemizedlist>
        <listitem>
+         <emphasis>TRACKER_USE_LOG_FILES</emphasis>
+         <para>
+           Don't just log to stdout and stderr, but to log files too
+           which are kept in $HOME/.local/share/tracker/. This came
+           into effect in 0.15.3 and 0.16.0. After this version of
+           Tracker, logging to file (usually useful for debugging)
+           can only be done by declaring this environment variable.
+         </para>
+       </listitem>
+       <listitem>
          <emphasis>TRACKER_USE_CONFIG_FILES</emphasis>
          <para>
            Don't use GSettings, instead use a config file similar to
diff --git a/src/libtracker-common/tracker-log.c b/src/libtracker-common/tracker-log.c
index f83b598..9a4661e 100644
--- a/src/libtracker-common/tracker-log.c
+++ b/src/libtracker-common/tracker-log.c
@@ -38,6 +38,7 @@ static gboolean  initialized;
 static FILE     *fd;
 static gint      verbosity;
 static guint     log_handler_id;
+static gboolean  use_log_files;
 
 #if GLIB_CHECK_VERSION (2,31,0)
 static GMutex    mutex;
@@ -113,8 +114,18 @@ log_output (const gchar    *domain,
                                  message);
 
        if (G_UNLIKELY (fd == NULL)) {
-               g_fprintf (stderr, "%s\n", output);
-               fflush (stderr);
+               FILE *f;
+
+               if (log_level == G_LOG_LEVEL_WARNING ||
+                   log_level == G_LOG_LEVEL_CRITICAL ||
+                   log_level == G_LOG_LEVEL_ERROR) {
+                       f = stderr;
+               } else {
+                       f = stdout;
+               }
+
+               g_fprintf (f, "%s\n", output);
+               fflush (f);
        } else {
                size += g_fprintf (fd, "%s\n", output);
                fflush (fd);
@@ -135,7 +146,10 @@ tracker_log_handler (const gchar    *domain,
                      const gchar    *message,
                      gpointer        user_data)
 {
-       log_output (domain, log_level, message);
+       /* Unless enabled, we don't log to file by default */
+       if (use_log_files) {
+               log_output (domain, log_level, message);
+       }
 
        /* Now show the message through stdout/stderr as usual */
        g_log_default_handler (domain, log_level, message, user_data);
@@ -154,8 +168,7 @@ gboolean
 tracker_log_init (gint    this_verbosity,
                   gchar **used_filename)
 {
-       gchar *filename;
-       gchar *basename;
+       const gchar *env_use_log_files;
        const gchar *env_verbosity;
        GLogLevelFlags hide_levels = 0;
 
@@ -163,6 +176,19 @@ tracker_log_init (gint    this_verbosity,
                return TRUE;
        }
 
+       env_use_log_files = g_getenv ("TRACKER_USE_LOG_FILES");
+       if (env_use_log_files != NULL) {
+               /* When set we use:
+                *   ~/.local/share/Tracker/
+                * Otherwise, we either of the following:
+                *   ~/.xsession-errors
+                *   ~/.cache/gdm/session.log
+                *   systemd journal
+                * Depending on the system.
+                */
+               use_log_files = TRUE;
+       }
+
        env_verbosity = g_getenv ("TRACKER_VERBOSITY");
        if (env_verbosity != NULL) {
                this_verbosity = atoi (env_verbosity);
@@ -183,25 +209,40 @@ tracker_log_init (gint    this_verbosity,
                g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
        }
 
-       basename = g_strdup_printf ("%s.log", g_get_application_name ());
-       filename = g_build_filename (g_get_user_data_dir (),
-                                    "tracker",
-                                    basename,
-                                    NULL);
-       g_free (basename);
-
-       /* Open file */
-       fd = g_fopen (filename, "a");
-       if (!fd) {
-               const gchar *error_string;
-
-               error_string = g_strerror (errno);
-               g_fprintf (stderr,
-                          "Could not open log:'%s', %s\n",
-                          filename,
-                          error_string);
-               g_fprintf (stderr,
-                          "All logging will go to stderr\n");
+       if (use_log_files) {
+               gchar *basename;
+               gchar *filename;
+
+               basename = g_strdup_printf ("%s.log", g_get_application_name ());
+               filename = g_build_filename (g_get_user_data_dir (),
+                                            "tracker",
+                                            basename,
+                                            NULL);
+               g_free (basename);
+
+               /* Open file */
+               fd = g_fopen (filename, "a");
+               if (!fd) {
+                       const gchar *error_string;
+
+                       error_string = g_strerror (errno);
+                       g_fprintf (stderr,
+                                  "Could not open log:'%s', %s\n",
+                                  filename,
+                                  error_string);
+                       g_fprintf (stderr,
+                                  "All logging will go to stderr\n");
+
+                       use_log_files = TRUE;
+               }
+
+               if (used_filename) {
+                       *used_filename = filename;
+               } else {
+                       g_free (filename);
+               }
+       } else {
+               *used_filename = NULL;
        }
 
        verbosity = CLAMP (this_verbosity, 0, 3);
@@ -248,12 +289,6 @@ tracker_log_init (gint    this_verbosity,
        /* Set log handler function for the rest */
        g_log_set_default_handler (tracker_log_handler, NULL);
 
-       if (used_filename) {
-               *used_filename = filename;
-       } else {
-               g_free (filename);
-       }
-
        initialized = TRUE;
 
        /* log binary name and version */
@@ -277,7 +312,7 @@ tracker_log_shutdown (void)
                log_handler_id = 0;
        }
 
-       if (fd) {
+       if (use_log_files && fd != NULL) {
                fclose (fd);
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]