gnome-utils r8451 - trunk/logview



Author: cosimoc
Date: Mon Mar  2 15:31:57 2009
New Revision: 8451
URL: http://svn.gnome.org/viewvc/gnome-utils?rev=8451&view=rev

Log:
2009-03-02  Cosimo Cecchi  <cosimoc gnome org>

	* logview-app.c: (parse_syslog), (enumerate_job_finish),
	(enumerate_next_files_async_cb), (enumerate_children_async_cb),
	(logview_app_first_time_initialize), (logview_app_initialize):
	* logview-prefs.c: (logview_prefs_init): when starting the
	application for the first time, gather all the readable files from
	/var/log instead of hardcoding logfile names.
	We'll eventually, and hopefully, support a smarter parsing of
	/etc/logrotate.* and log rotated/compressed log grouping, but right
	now this is the best effort we can do (#567169).


Modified:
   trunk/logview/ChangeLog
   trunk/logview/logview-app.c
   trunk/logview/logview-prefs.c

Modified: trunk/logview/logview-app.c
==============================================================================
--- trunk/logview/logview-app.c	(original)
+++ trunk/logview/logview-app.c	Mon Mar  2 15:31:57 2009
@@ -80,6 +80,198 @@
   return retval;
 }
 
+typedef struct {
+  LogviewApp *app;
+  GSList *logs;
+} EnumerateJob;
+
+/* TODO: ideally we should parse configuration files in /etc/logrotate.conf
+ * and all the files in /etc/logrotate.d/ and group all the logs referring
+ * to the same entry under a category. Right now, we just do some
+ * parsing instead, and fill with quasi-sensible defaults.
+ */
+
+/* adapted from sysklogd sources */
+static GSList*
+parse_syslog ()
+{
+  char *logfile = NULL;
+  char cbuf[BUFSIZ];
+  char *cline, *p;
+  FILE *cf;
+  GSList *logfiles = NULL;
+
+  if ((cf = fopen ("/etc/syslog.conf", "r")) == NULL) {
+    return NULL;
+  }
+
+  cline = cbuf;
+  while (fgets (cline, sizeof (cbuf) - (cline - cbuf), cf) != NULL) {
+    gchar **list;
+    gint i;
+
+    for (p = cline; g_ascii_isspace (*p); ++p);
+    if (*p == '\0' || *p == '#' || *p == '\n')
+      continue;
+
+    list = g_strsplit_set (p, ", -\t()\n", 0);
+
+    for (i = 0; list[i]; ++i) {
+      if (*list[i] == '/' &&
+          g_slist_find_custom (logfiles, list[i],
+                               (GCompareFunc) g_ascii_strcasecmp) == NULL)
+      {
+        logfiles = g_slist_insert (logfiles,
+                                   g_strdup (list[i]), 0);
+      }
+    }
+
+    g_strfreev (list);
+  }
+
+  fclose (cf);
+
+  return logfiles;
+}
+
+static void
+enumerate_job_finish (EnumerateJob *job)
+{
+  GSList *files = job->logs;
+  LogviewApp *app = job->app;
+
+  logview_manager_add_logs_from_name_list (app->priv->manager, files, files->data);
+
+  g_slist_foreach (files, (GFunc) g_free, NULL);
+  g_slist_free (files);
+
+  g_object_unref (job->app);
+  g_slice_free (EnumerateJob, job);
+}
+
+static void
+enumerate_next_files_async_cb (GObject *source,
+                               GAsyncResult *res,
+                               gpointer user_data)
+{
+  EnumerateJob *job = user_data;
+  GList *enumerated_files, *l;
+  GFileInfo *info;
+  GSList *logs;
+  const char *content_type, *name;
+  char *parse_string, *container_path;
+  GFileType type;
+  GFile *container;
+
+  enumerated_files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source),
+                                                          res, NULL);
+  if (!enumerated_files) {
+    enumerate_job_finish (job);
+    return;
+  }
+
+  logs = job->logs;
+  container = g_file_enumerator_get_container (G_FILE_ENUMERATOR (source));
+  container_path = g_file_get_path (container);
+
+  /* TODO: we don't support grouping rotated logs yet, skip gzipped files
+   * and those which name contains a formatted date.
+   */
+  for (l = enumerated_files; l; l = l->next) {
+    info = l->data;
+    type = g_file_info_get_file_type (info);
+    content_type = g_file_info_get_content_type (info);
+    name = g_file_info_get_name (info);
+    
+    if (!g_file_info_get_attribute_boolean (info, "access::can-read")) {
+      g_object_unref (info);
+      continue;
+    }
+
+    if (type != (G_FILE_TYPE_REGULAR || G_FILE_TYPE_SYMBOLIC_LINK) ||
+        !g_content_type_is_a (content_type, "text/plain"))
+    {
+      g_object_unref (info);
+      continue;
+    }
+
+    if (g_content_type_is_a (content_type, "application/x-gzip")) {
+      g_object_unref (info);
+      continue;
+    }
+
+    if (g_regex_match_simple ("\\d{8}$", name, 0, 0)) {
+      g_object_unref (info);
+      continue;
+    }
+
+    parse_string = g_build_filename (container_path, name, NULL);
+
+    if (g_slist_find_custom (logs, parse_string, (GCompareFunc) g_ascii_strcasecmp) == NULL) {
+      logs = g_slist_append (logs, parse_string);
+    } else {
+      g_free (parse_string);
+    }
+
+    g_object_unref (info);
+    parse_string = NULL;
+  }
+
+  g_list_free (enumerated_files);
+  g_object_unref (container);
+  g_free (container_path);
+
+  job->logs = logs;
+
+  enumerate_job_finish (job);
+}
+
+static void
+enumerate_children_async_cb (GObject *source,
+                             GAsyncResult *res,
+                             gpointer user_data)
+{
+  EnumerateJob *job = user_data;
+  GFileEnumerator *enumerator;
+
+  enumerator = g_file_enumerate_children_finish (G_FILE (source),
+                                                 res, NULL);
+  if (!enumerator) {
+    enumerate_job_finish (job);
+    return;
+  }
+
+  g_file_enumerator_next_files_async (enumerator, G_MAXINT,
+                                      G_PRIORITY_DEFAULT,
+                                      NULL, enumerate_next_files_async_cb, job);  
+}
+
+static void
+logview_app_first_time_initialize (LogviewApp *app)
+{
+  GSList *logs;
+  GFile *log_dir;
+  EnumerateJob *job;
+
+  /* let's add all accessible files in /var/log and those mentioned
+   * in /etc/syslog.conf.
+   */
+
+  logs = parse_syslog ();
+
+  job = g_slice_new0 (EnumerateJob);
+  job->app = g_object_ref (app);
+  job->logs = logs;
+
+  log_dir = g_file_new_for_path ("/var/log/");
+  g_file_enumerate_children_async (log_dir,
+                                   "standard::*,access::can-read", 0,
+                                   G_PRIORITY_DEFAULT, NULL,
+                                   enumerate_children_async_cb, job);
+
+  g_object_unref (log_dir);
+}
+
 static void
 do_finalize (GObject *obj)
 {
@@ -152,12 +344,16 @@
     active_log = logview_prefs_get_active_logfile (priv->prefs);
     logs = logview_prefs_get_stored_logfiles (priv->prefs);
 
-    logview_manager_add_logs_from_name_list (priv->manager,
-                                             logs, active_log);
-
-    g_free (active_log);
-    g_slist_foreach (logs, (GFunc) g_free, NULL);
-    g_slist_free (logs);
+    if (!logs) {
+      logview_app_first_time_initialize (app);
+    } else {
+      logview_manager_add_logs_from_name_list (priv->manager,
+                                               logs, active_log);
+
+      g_free (active_log);
+      g_slist_foreach (logs, (GFunc) g_free, NULL);
+      g_slist_free (logs);
+    }
   } else {
     logview_manager_add_logs_from_names (priv->manager, log_files);
   }
@@ -203,4 +399,4 @@
   } else {
     logview_window_add_errors (window, errors);
   }
-}
\ No newline at end of file
+}

Modified: trunk/logview/logview-prefs.c
==============================================================================
--- trunk/logview/logview-prefs.c	(original)
+++ trunk/logview/logview-prefs.c	Mon Mar  2 15:31:57 2009
@@ -57,32 +57,6 @@
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
-static char *default_logfiles[] = {
-  "/var/log/sys.log",
-#ifndef ON_SUN_OS
-  "/var/log/messages",
-  "/var/log/secure",
-  "/var/log/maillog",
-  "/var/log/cron",
-  "/var/log/Xorg.0.log",
-  "/var/log/XFree86.0.log",
-  "/var/log/auth.log",
-  "/var/log/cups/error_log",
-#else
-  "/var/adm/messages",
-  "/var/adm/sulog",
-  "/var/log/authlog",
-  "/var/log/brlog",
-  "/var/log/postrun.log",
-  "/var/log/scrollkeeper.log",
-  "/var/log/snmpd.log",
-  "/var/log/sysidconfig.log",
-  "/var/log/swupas/swupas.log",
-  "/var/log/swupas/swupas.error.log",
-#endif
-  NULL
-};
-
 #define GET_PRIVATE(o) \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), LOGVIEW_TYPE_PREFS, LogviewPrefsPrivate))
 
@@ -196,95 +170,6 @@
   return FALSE;
 }
 
-static gboolean
-check_stored_logfiles (LogviewPrefs *prefs)
-{
-  GConfValue *val;
-  gboolean retval = FALSE;
-
-  val = gconf_client_get (prefs->priv->client,
-                          GCONF_LOGFILES,
-                          NULL);
-  if (val) {
-    gconf_value_free (val);
-    retval = TRUE;
-  }
-
-  return retval;
-}
-
-/* adapted from sysklogd sources */
-static GSList*
-parse_syslog ()
-{
-  char *logfile = NULL;
-  char cbuf[BUFSIZ];
-  char *cline, *p;
-  FILE *cf;
-  GSList *logfiles = NULL;
-
-  if ((cf = fopen ("/etc/syslog.conf", "r")) == NULL) {
-    return NULL;
-  }
-
-  cline = cbuf;
-  while (fgets (cline, sizeof (cbuf) - (cline - cbuf), cf) != NULL) {
-    gchar **list;
-    gint i;
-
-    for (p = cline; g_ascii_isspace (*p); ++p);
-    if (*p == '\0' || *p == '#' || *p == '\n')
-      continue;
-
-    list = g_strsplit_set (p, ", -\t()\n", 0);
-
-    for (i = 0; list[i]; ++i) {
-      if (*list[i] == '/' &&
-          g_slist_find_custom (logfiles, list[i],
-                               (GCompareFunc) g_ascii_strcasecmp) == NULL)
-      {
-        logfiles = g_slist_insert (logfiles,
-                                   g_strdup (list[i]), 0);
-      }
-    }
-
-    g_strfreev (list);
-  }
-
-  fclose (cf);
-
-  return logfiles;
-}
-
-static void
-logview_prefs_fill_defaults (LogviewPrefs *prefs)
-{
-  GSList *logs;
-  int i;
-
-  g_assert (LOGVIEW_IS_PREFS (prefs));
-
-  /* insert in the registry both the default items and the files
-   * specified in syslog.conf.
-   */
-
-  logs = parse_syslog ();
-
-  for (i = 0; default_logfiles[i]; i++) {
-    if (g_slist_find_custom (logs, default_logfiles[i], (GCompareFunc) g_ascii_strcasecmp) == NULL)
-      logs = g_slist_insert (logs, g_strdup (default_logfiles[i]), 0);
-  }
-
-  gconf_client_set_list (prefs->priv->client,
-                         GCONF_LOGFILES,
-                         GCONF_VALUE_STRING,
-                         logs, NULL);
-
-  /* the string list is copied */
-  g_slist_foreach (logs, (GFunc) g_free, NULL);
-  g_slist_free (logs);
-}
-
 static void
 logview_prefs_init (LogviewPrefs *self)
 {
@@ -296,14 +181,6 @@
   priv->client = gconf_client_get_default ();
   priv->size_store_timeout = 0;
 
-  stored_logs = check_stored_logfiles (self);
-  if (!stored_logs) {
-    /* if there's no stored logs, either it's the first start or GConf has
-     * been corrupted. re-fill the registry with sensible defaults anyway.
-     */
-    logview_prefs_fill_defaults (self);
-  }
-
   gconf_client_notify_add (priv->client,
                            GCONF_MONOSPACE_FONT_NAME,
                            (GConfClientNotifyFunc) monospace_font_changed_cb,
@@ -532,4 +409,4 @@
   filename = gconf_client_get_string (prefs->priv->client, GCONF_LOGFILE, NULL);
 
   return filename;
-}
\ No newline at end of file
+}



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