From 0b8e638618cdfb5370b2f4525f7a082a621d9312 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Sat, 14 Feb 2015 19:42:17 +0100 Subject: journal: separate set_matches() from query() --- src/gl-eventviewlist.c | 14 +++++---- src/gl-journal.c | 84 ++++++++++++++++++++++++++++++++++---------------- src/gl-journal.h | 5 +-- 3 files changed, 68 insertions(+), 35 deletions(-) diff --git a/src/gl-eventviewlist.c b/src/gl-eventviewlist.c index 21ed1b6..da228ad 100644 --- a/src/gl-eventviewlist.c +++ b/src/gl-eventviewlist.c @@ -446,7 +446,7 @@ on_notify_category (GlCategoryList *list, const gchar * query[] = { "PRIORITY=0", "PRIORITY=1", "PRIORITY=2", "PRIORITY=3", NULL }; priv->current_row_style = GL_EVENT_VIEW_ROW_STYLE_CMDLINE; - gl_journal_query_async (priv->journal, query, NULL, query_ready, view); + gl_journal_set_matches (priv->journal, query); } break; @@ -455,7 +455,7 @@ on_notify_category (GlCategoryList *list, const gchar *query[] = { NULL }; priv->current_row_style = GL_EVENT_VIEW_ROW_STYLE_CMDLINE; - gl_journal_query_async (priv->journal, query, NULL, query_ready, view); + gl_journal_set_matches (priv->journal, query); } break; @@ -473,7 +473,7 @@ on_notify_category (GlCategoryList *list, priv->current_row_style = GL_EVENT_VIEW_ROW_STYLE_CMDLINE; uid_str = create_uid_match_string (); query[3] = uid_str; - gl_journal_query_async (priv->journal, query, NULL, query_ready, view); + gl_journal_set_matches (priv->journal, query); g_free (uid_str); } @@ -484,7 +484,7 @@ on_notify_category (GlCategoryList *list, const gchar *query[] = { "_TRANSPORT=kernel", NULL }; priv->current_row_style = GL_EVENT_VIEW_ROW_STYLE_SIMPLE; - gl_journal_query_async (priv->journal, query, NULL, query_ready, view); + gl_journal_set_matches (priv->journal, query); } break; @@ -493,7 +493,7 @@ on_notify_category (GlCategoryList *list, const gchar *query[] = { "_TRANSPORT=kernel", "_KERNEL_DEVICE", NULL }; priv->current_row_style = GL_EVENT_VIEW_ROW_STYLE_SIMPLE; - gl_journal_query_async (priv->journal, query, NULL, query_ready, view); + gl_journal_set_matches (priv->journal, query); } break; @@ -502,7 +502,7 @@ on_notify_category (GlCategoryList *list, const gchar *query[] = { "_AUDIT_SESSION", NULL }; priv->current_row_style = GL_EVENT_VIEW_ROW_STYLE_CMDLINE; - gl_journal_query_async (priv->journal, query, NULL, query_ready, view); + gl_journal_set_matches (priv->journal, query); } break; @@ -510,6 +510,8 @@ on_notify_category (GlCategoryList *list, g_assert_not_reached (); } + gl_journal_query_async (priv->journal, NULL, query_ready, view); + gtk_widget_show_all (GTK_WIDGET (priv->active_listbox)); settings = g_settings_new (SETTINGS_SCHEMA); diff --git a/src/gl-journal.c b/src/gl-journal.c index 0191e7c..fcba645 100644 --- a/src/gl-journal.c +++ b/src/gl-journal.c @@ -28,6 +28,7 @@ typedef struct sd_journal *journal; gint fd; guint source_id; + gchar **mandatory_fields; } GlJournalPrivate; G_DEFINE_TYPE_WITH_PRIVATE (GlJournal, gl_journal, G_TYPE_OBJECT) @@ -79,6 +80,7 @@ gl_journal_finalize (GObject *object) g_source_remove (priv->source_id); g_clear_pointer (&priv->journal, sd_journal_close); + g_clear_pointer (&priv->mandatory_fields, g_strfreev); } static void @@ -312,21 +314,16 @@ out: void gl_journal_query_async (GlJournal *self, - const gchar * const *query, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { GTask *task; - gchar **data; GList *results; - data = g_strdupv ((gchar **) query); - task = g_task_new (self, cancellable, callback, user_data); - g_task_set_task_data (task, data, (GDestroyNotify) g_strfreev); - results = gl_journal_query (self, query); + results = gl_journal_query (self); g_task_return_pointer (task, results, (GDestroyNotify) gl_journal_results_free); g_object_unref (task); @@ -382,37 +379,18 @@ gl_journal_query_match (sd_journal *journal, } GList * -gl_journal_query (GlJournal *self, - const gchar * const *query) +gl_journal_query (GlJournal *self) { GlJournalPrivate *priv; sd_journal *journal; - gsize i; gint ret; GList *results = NULL; g_return_val_if_fail (GL_JOURNAL (self), NULL); - g_return_val_if_fail (query != NULL, NULL); priv = gl_journal_get_instance_private (self); journal = priv->journal; - - for (i = 0; query[i]; i++) - { - /* don't add fields of which we only want to check existance */ - if (strchr (query[i], '=') == NULL) - continue; - - ret = sd_journal_add_match (journal, query[i], 0); - - if (ret < 0) - { - g_warning ("Error adding match '%s': %s", query[i], - g_strerror (-ret)); - } - } - /* Take events from this boot only. */ sd_id128_t boot_id; gchar boot_string[33]; @@ -466,7 +444,7 @@ gl_journal_query (GlJournal *self, break; } - if (!gl_journal_query_match (journal, query)) + if (!gl_journal_query_match (journal, (const gchar * const *) priv->mandatory_fields)) continue; result = _gl_journal_query_result (self); @@ -479,6 +457,58 @@ gl_journal_query (GlJournal *self, return results; } +/** + * gl_journal_set_matches: + * @journal: a #GlJournal + * @matches: new matches to set + * + * Sets @matches on @journal. Will reset the cursor position to the + * beginning. + */ +void +gl_journal_set_matches (GlJournal *journal, + const gchar * const *matches) +{ + GlJournalPrivate *priv = gl_journal_get_instance_private (journal); + GPtrArray *mandatory_fields; + gint i; + + g_return_if_fail (matches != NULL); + + if (priv->mandatory_fields) + g_clear_pointer (&priv->mandatory_fields, g_strfreev); + + sd_journal_flush_matches (priv->journal); + + mandatory_fields = g_ptr_array_new (); + for (i = 0; matches[i]; i++) + { + int r; + + /* matches without a value should only check for existence. + * systemd doesn't support that, so let's remember them to + * filter out later. + */ + if (strchr (matches[i], '=') == NULL) + { + g_ptr_array_add (mandatory_fields, g_strdup (matches[i])); + continue; + } + + r = sd_journal_add_match (priv->journal, matches[i], 0); + if (r < 0) + { + g_critical ("Failed to add match '%s': %s", matches[i], g_strerror (-r)); + break; + } + } + + /* add sentinel */ + g_ptr_array_add (mandatory_fields, NULL); + + priv->mandatory_fields = (gchar **) g_ptr_array_free (mandatory_fields, FALSE); +} + GlJournalResult * gl_journal_query_cursor (GlJournal *self, const gchar *cursor) diff --git a/src/gl-journal.h b/src/gl-journal.h index e286247..1733dc3 100644 --- a/src/gl-journal.h +++ b/src/gl-journal.h @@ -76,9 +76,10 @@ typedef struct GType gl_journal_result_get_type (void); GType gl_journal_get_type (void); -void gl_journal_query_async (GlJournal *self, const gchar * const *query, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); +void gl_journal_query_async (GlJournal *self, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); GList * gl_journal_query_finish (GlJournal *self, GAsyncResult *res, GError **error); -GList * gl_journal_query (GlJournal *self, const gchar * const *query); +GList * gl_journal_query (GlJournal *self); +void gl_journal_set_matches (GlJournal *journal, const gchar * const *matches); GlJournalResult * gl_journal_result_ref (GlJournalResult *result); void gl_journal_result_unref (GlJournalResult *result); void gl_journal_results_free (GList *results); -- cgit v1.2.1