[gnome-logs/wip/test] Add category to log entries in important category



commit a9590889e177f2a5647242647084118e10eedd41
Author: Jonathan Kang <jonathan121537 gmail com>
Date:   Tue Jun 9 20:24:43 2015 +0800

    Add category to log entries in important category
    
    Use a label to indicate to which category entries from the important
    category belong.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=750795

 src/gl-eventviewlist.c |   28 +++++++++++++-
 src/gl-eventviewrow.c  |   99 ++++++++++++++++++++++++++++++++++++++++++++++--
 src/gl-eventviewrow.h  |   17 ++++++++-
 src/gl-journal.c       |   41 ++++++++++++++++++++
 src/gl-journal.h       |    2 +
 src/gl-util.c          |   14 +++++++
 src/gl-util.h          |    1 +
 7 files changed, 196 insertions(+), 6 deletions(-)
---
diff --git a/src/gl-eventviewlist.c b/src/gl-eventviewlist.c
index 14ed803..9dff73b 100644
--- a/src/gl-eventviewlist.c
+++ b/src/gl-eventviewlist.c
@@ -43,6 +43,7 @@ typedef struct
     GlJournalEntry *entry;
     GlUtilClockFormat clock_format;
     GtkListBox *entries_box;
+    GtkSizeGroup *category_sizegroup;
     GtkSizeGroup *message_sizegroup;
     GtkSizeGroup *time_sizegroup;
     GtkWidget *categories;
@@ -331,11 +332,34 @@ gl_event_list_view_create_row_widget (gpointer item,
     GtkWidget *rtn;
     GtkWidget *message_label;
     GtkWidget *time_label;
+    GlCategoryList *list;
+    GlCategoryListFilter filter;
     GlEventViewList *view = user_data;
 
     GlEventViewListPrivate *priv = gl_event_view_list_get_instance_private (view);
 
-    rtn = gl_event_view_row_new (item, priv->clock_format);
+    list = GL_CATEGORY_LIST (priv->categories);
+    filter = gl_category_list_get_category (list);
+
+    if (filter == GL_CATEGORY_LIST_FILTER_IMPORTANT)
+    {
+        GtkWidget *category_label;
+
+        rtn = gl_event_view_row_new (item,
+                                     priv->clock_format,
+                                     GL_EVENT_VIEW_ROW_CATEGORY_IMPORTANT);
+
+        category_label = gl_event_view_row_get_category_label (GL_EVENT_VIEW_ROW (rtn));
+        gtk_size_group_add_widget (GTK_SIZE_GROUP (priv->category_sizegroup),
+                                   category_label);
+    }
+    else
+    {
+        rtn = gl_event_view_row_new (item,
+                                     priv->clock_format,
+                                     GL_EVENT_VIEW_ROW_CATEGORY_NONE);
+    }
+
     message_label = gl_event_view_row_get_message_label (GL_EVENT_VIEW_ROW (rtn));
     time_label = gl_event_view_row_get_time_label (GL_EVENT_VIEW_ROW (rtn));
 
@@ -596,6 +620,7 @@ gl_event_view_list_finalize (GObject *object)
 
     g_clear_object (&priv->journal_model);
     g_clear_pointer (&priv->search_text, g_free);
+    g_object_unref (priv->category_sizegroup);
     g_object_unref (priv->message_sizegroup);
     g_object_unref (priv->time_sizegroup);
 }
@@ -638,6 +663,7 @@ gl_event_view_list_init (GlEventViewList *view)
 
     priv = gl_event_view_list_get_instance_private (view);
     priv->search_text = NULL;
+    priv->category_sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
     priv->message_sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
     priv->time_sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
     categories = GL_CATEGORY_LIST (priv->categories);
diff --git a/src/gl-eventviewrow.c b/src/gl-eventviewrow.c
index 4b883de..21fb5aa 100644
--- a/src/gl-eventviewrow.c
+++ b/src/gl-eventviewrow.c
@@ -27,6 +27,7 @@
 enum
 {
     PROP_0,
+    PROP_CATEGORY,
     PROP_CLOCK_FORMAT,
     PROP_ENTRY,
     N_PROPERTIES
@@ -40,8 +41,10 @@ struct _GlEventViewRow
 
 typedef struct
 {
+    GlEventViewRowCategory category;
     GlUtilClockFormat clock_format;
     GlJournalEntry *entry;
+    GtkWidget *category_label;
     GtkWidget *message_label;
     GtkWidget *time_label;
 } GlEventViewRowPrivate;
@@ -51,6 +54,16 @@ G_DEFINE_TYPE_WITH_PRIVATE (GlEventViewRow, gl_event_view_row, GTK_TYPE_LIST_BOX
 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
 
 GtkWidget *
+gl_event_view_row_get_category_label (GlEventViewRow *row)
+{
+    GlEventViewRowPrivate *priv;
+
+    priv = gl_event_view_row_get_instance_private (row);
+
+    return priv->category_label;
+}
+
+GtkWidget *
 gl_event_view_row_get_message_label (GlEventViewRow *row)
 {
     GlEventViewRowPrivate *priv;
@@ -90,6 +103,9 @@ gl_event_view_row_get_property (GObject *object,
 
     switch (prop_id)
     {
+        case PROP_CATEGORY:
+            g_value_set_enum (value, priv->category);
+            break;
         case PROP_CLOCK_FORMAT:
             g_value_set_enum (value, priv->clock_format);
             break;
@@ -113,6 +129,9 @@ gl_event_view_row_set_property (GObject *object,
 
     switch (prop_id)
     {
+        case PROP_CATEGORY:
+            priv->category = g_value_get_enum (value);
+            break;
         case PROP_CLOCK_FORMAT:
             priv->clock_format = g_value_get_enum (value);
             break;
@@ -126,12 +145,56 @@ gl_event_view_row_set_property (GObject *object,
 }
 
 static void
+gl_event_view_row_construct_category_label (GlEventViewRow *row,
+                                            GlJournalEntry *entry)
+{
+    gint uid;
+    GlEventViewRowPrivate *priv;
+
+    uid = gl_util_get_uid ();
+    priv = gl_event_view_row_get_instance_private (row);
+
+    /* The priority given to the categories should be determined by how
+     * specific the checks are. The applications category is the most
+     * specific, followed by the hardware category, then kernel, security
+     * and finally the least-specific other category. So we check the category
+     * in the order of applications, hardware, system, security and other. */
+    if ((g_strcmp0 (gl_journal_entry_get_transport (entry), "kernel") == 0
+         || g_strcmp0 (gl_journal_entry_get_transport (entry), "stdout") == 0
+         || g_strcmp0 (gl_journal_entry_get_transport (entry), "syslog") == 0)
+        && gl_journal_entry_get_uid (entry) == uid)
+    {
+        priv->category_label = gtk_label_new (_("Applications"));
+    }
+    else if (g_strcmp0 (gl_journal_entry_get_transport (entry), "kernel") == 0
+             && gl_journal_entry_get_kernel_device (entry) != NULL)
+    {
+        priv->category_label = gtk_label_new (_("Hardware"));
+    }
+    else if (g_strcmp0 (gl_journal_entry_get_transport (entry), "kernel") == 0)
+    {
+        priv->category_label = gtk_label_new (_("System"));
+    }
+    else if (gl_journal_entry_get_audit_session (entry) != NULL)
+    {
+        priv->category_label = gtk_label_new (_("Security"));
+    }
+    else
+    {
+        priv->category_label = gtk_label_new (_("Other"));
+    }
+}
+
+static void
 gl_event_view_row_constructed (GObject *object)
 {
     GtkStyleContext *context;
     GtkWidget *grid;
     gchar *time;
     gboolean rtl;
+    GlEventViewRowCategory category;
+    GlUtilClockFormat tmp_clock_format;
+    GlJournalEntry *tmp_entry;
     GlJournalEntry *entry;
     GDateTime *now;
     GlEventViewRow *row = GL_EVENT_VIEW_ROW (object);
@@ -148,6 +211,23 @@ gl_event_view_row_constructed (GObject *object)
     gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
     gtk_container_add (GTK_CONTAINER (row), grid);
 
+    g_object_get (object,
+                  "category", &category,
+                  "clock-format", &tmp_clock_format,
+                  "entry", &tmp_entry,
+                  NULL);
+
+    if (category == GL_EVENT_VIEW_ROW_CATEGORY_IMPORTANT)
+    {
+        gl_event_view_row_construct_category_label (row, entry);
+
+        context = gtk_widget_get_style_context (GTK_WIDGET (priv->category_label));
+        gtk_style_context_add_class (context, "dim-label");
+        gtk_label_set_xalign (GTK_LABEL (priv->category_label), 0);
+        gtk_grid_attach (GTK_GRID (grid), priv->category_label,
+                         rtl ? 2 : 0, 0, 1, 1);
+    }
+
     priv->message_label = gtk_label_new (gl_journal_entry_get_message (entry));
     gtk_widget_set_direction (priv->message_label, GTK_TEXT_DIR_LTR);
     context = gtk_widget_get_style_context (GTK_WIDGET (priv->message_label));
@@ -157,7 +237,7 @@ gl_event_view_row_constructed (GObject *object)
                              PANGO_ELLIPSIZE_END);
     gtk_label_set_xalign (GTK_LABEL (priv->message_label), 0);
     gtk_grid_attach (GTK_GRID (grid), priv->message_label,
-                     rtl ? 1 : 0, 0, 1, 1);
+                     1, 0, 1, 1);
 
     now = g_date_time_new_now_local ();
     time = gl_util_timestamp_to_display (gl_journal_entry_get_timestamp (entry),
@@ -169,9 +249,10 @@ gl_event_view_row_constructed (GObject *object)
     gtk_style_context_add_class (context, "event-time");
     gtk_widget_set_halign (priv->time_label, GTK_ALIGN_END);
     gtk_label_set_xalign (GTK_LABEL (priv->time_label), 1);
-    gtk_grid_attach (GTK_GRID (grid), priv->time_label, rtl ? 0 : 1, 0, 1, 1);
+    gtk_grid_attach (GTK_GRID (grid), priv->time_label, rtl ? 0 : 2, 0, 1, 1);
 
     g_free (time);
+    g_object_unref (tmp_entry);
 
     G_OBJECT_CLASS (gl_event_view_row_parent_class)->constructed (object);
 }
@@ -186,6 +267,14 @@ gl_event_view_row_class_init (GlEventViewRowClass *klass)
     gobject_class->get_property = gl_event_view_row_get_property;
     gobject_class->set_property = gl_event_view_row_set_property;
 
+    obj_properties[PROP_CATEGORY] = g_param_spec_enum ("category", "Category",
+                                                       "Filter rows from important category",
+                                                       GL_TYPE_EVENT_VIEW_ROW_CATEGORY,
+                                                       GL_EVENT_VIEW_ROW_CATEGORY_NONE,
+                                                       G_PARAM_READWRITE |
+                                                       G_PARAM_CONSTRUCT_ONLY |
+                                                       G_PARAM_STATIC_STRINGS);
+
     obj_properties[PROP_CLOCK_FORMAT] = g_param_spec_enum ("clock-format", "Clock format",
                                                            "Format of the clock in which to show timestamps",
                                                            GL_TYPE_UTIL_CLOCK_FORMAT,
@@ -226,8 +315,10 @@ gl_event_view_row_get_entry (GlEventViewRow *row)
 
 GtkWidget *
 gl_event_view_row_new (GlJournalEntry *entry,
-                       GlUtilClockFormat clock_format)
+                       GlUtilClockFormat clock_format,
+                       GlEventViewRowCategory category)
 {
     return g_object_new (GL_TYPE_EVENT_VIEW_ROW, "entry", entry,
-                         "clock-format", clock_format, NULL);
+                         "clock-format", clock_format,
+                         "category", category, NULL);
 }
diff --git a/src/gl-eventviewrow.h b/src/gl-eventviewrow.h
index 3e33814..4fbba00 100644
--- a/src/gl-eventviewrow.h
+++ b/src/gl-eventviewrow.h
@@ -26,11 +26,26 @@ G_BEGIN_DECLS
 #include "gl-journal.h"
 #include "gl-util.h"
 
+/*
+ * GlEventViewRowCategory:
+ * @GL_EVENT_VIEW_ROW_CATEGORY_NONE:
+ * @GL_EVENT_VIEW_ROW_CATEGORY_IMPORTANT:
+ *
+ * The category, a property of GlEventViewRow, to filter events from
+ * "important" category.
+ */
+typedef enum
+{
+    GL_EVENT_VIEW_ROW_CATEGORY_NONE,
+    GL_EVENT_VIEW_ROW_CATEGORY_IMPORTANT
+} GlEventViewRowCategory;
+
 #define GL_TYPE_EVENT_VIEW_ROW (gl_event_view_row_get_type ())
 G_DECLARE_FINAL_TYPE (GlEventViewRow, gl_event_view_row, GL, EVENT_VIEW_ROW, GtkListBoxRow)
 
-GtkWidget * gl_event_view_row_new (GlJournalEntry *entry, GlUtilClockFormat clock_format);
+GtkWidget * gl_event_view_row_new (GlJournalEntry *entry, GlUtilClockFormat clock_format, 
GlEventViewRowCategory category);
 GlJournalEntry * gl_event_view_row_get_entry (GlEventViewRow *row);
+GtkWidget * gl_event_view_row_get_category_label (GlEventViewRow *row);
 GtkWidget * gl_event_view_row_get_message_label (GlEventViewRow *row);
 GtkWidget * gl_event_view_row_get_time_label (GlEventViewRow *row);
 
diff --git a/src/gl-journal.c b/src/gl-journal.c
index 11f3e41..6147572 100644
--- a/src/gl-journal.c
+++ b/src/gl-journal.c
@@ -34,8 +34,10 @@ struct _GlJournalEntry
   gchar *comm;
   gchar *kernel_device;
   gchar *audit_session;
+  gchar *transport;
   gchar *catalog;
   guint priority;
+  gint uid;
 };
 
 G_DEFINE_TYPE (GlJournalEntry, gl_journal_entry, G_TYPE_OBJECT);
@@ -217,6 +219,7 @@ _gl_journal_query_entry (GlJournal *self)
     sd_journal *journal;
     GError *error = NULL;
     gchar *priority;
+    gchar *uid;
 
     priv = gl_journal_get_instance_private (self);
     journal = priv->journal;
@@ -316,6 +319,27 @@ _gl_journal_query_entry (GlJournal *self)
 
     entry->audit_session = gl_journal_get_data (self, "_AUDIT_SESSION", NULL);
 
+    entry->transport = gl_journal_get_data (self, "_TRANSPORT", &error);
+
+    if (error != NULL)
+    {
+        g_debug ("Error while getting transport from journal: %s",
+                 error->message);
+        g_clear_error (&error);
+    }
+
+    uid = gl_journal_get_data (self, "_UID", &error);
+
+    if (error != NULL)
+    {
+        g_debug ("Error while getting uid from journal: %s", error->message);
+        g_clear_error (&error);
+    }
+
+    /* We store an invalid or non-existent UID as -1 */
+    entry->uid = uid ? atoi (uid) : -1;
+    g_free (uid);
+
     return entry;
 
 out:
@@ -501,6 +525,7 @@ gl_journal_entry_finalize (GObject *object)
   g_free (entry->comm);
   g_free (entry->kernel_device);
   g_free (entry->audit_session);
+  g_free (entry->transport);
 
   G_OBJECT_CLASS (gl_journal_entry_parent_class)->finalize (object);
 }
@@ -554,6 +579,14 @@ gl_journal_entry_get_audit_session (GlJournalEntry *entry)
 }
 
 const gchar *
+gl_journal_entry_get_transport (GlJournalEntry *entry)
+{
+  g_return_val_if_fail (GL_IS_JOURNAL_ENTRY (entry), NULL);
+
+  return entry->transport;
+}
+
+const gchar *
 gl_journal_entry_get_catalog (GlJournalEntry *entry)
 {
   g_return_val_if_fail (GL_IS_JOURNAL_ENTRY (entry), NULL);
@@ -568,3 +601,11 @@ gl_journal_entry_get_priority (GlJournalEntry *entry)
 
   return entry->priority;
 }
+
+gint
+gl_journal_entry_get_uid (GlJournalEntry *entry)
+{
+  g_return_val_if_fail (GL_IS_JOURNAL_ENTRY (entry), -1);
+
+  return entry->uid;
+}
diff --git a/src/gl-journal.h b/src/gl-journal.h
index 198a8ee..2d78cf7 100644
--- a/src/gl-journal.h
+++ b/src/gl-journal.h
@@ -72,8 +72,10 @@ const gchar *           gl_journal_entry_get_message                    (GlJourn
 const gchar *           gl_journal_entry_get_command_line               (GlJournalEntry *entry);
 const gchar *           gl_journal_entry_get_kernel_device              (GlJournalEntry *entry);
 const gchar *           gl_journal_entry_get_audit_session              (GlJournalEntry *entry);
+const gchar *           gl_journal_entry_get_transport                  (GlJournalEntry *entry);
 const gchar *           gl_journal_entry_get_catalog                    (GlJournalEntry *entry);
 guint                   gl_journal_entry_get_priority                   (GlJournalEntry *entry);
+gint                    gl_journal_entry_get_uid                        (GlJournalEntry *entry);
 
 G_END_DECLS
 
diff --git a/src/gl-util.c b/src/gl-util.c
index 6c470f8..40cea30 100644
--- a/src/gl-util.c
+++ b/src/gl-util.c
@@ -194,3 +194,17 @@ gl_util_timestamp_to_display (guint64 microsecs,
 out:
     return time;
 }
+
+gint
+gl_util_get_uid (void)
+{
+    GCredentials *creds;
+    uid_t uid;
+
+    creds = g_credentials_new ();
+    uid = g_credentials_get_unix_user (creds, NULL);
+
+    g_object_unref (creds);
+
+    return uid;
+}
diff --git a/src/gl-util.h b/src/gl-util.h
index b6cf53c..3590128 100644
--- a/src/gl-util.h
+++ b/src/gl-util.h
@@ -42,6 +42,7 @@ void gl_util_on_css_provider_parsing_error (GtkCssProvider *provider,
 gchar * gl_util_timestamp_to_display (guint64 microsecs,
                                       GDateTime *now,
                                       GlUtilClockFormat format);
+gint gl_util_get_uid (void);
 
 G_END_DECLS
 


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