From 1773b5f77b9c575ab8fc3e3d692e8a7056fe2959 Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Wed, 17 Oct 2007 17:09:03 -0400 Subject: add a basic ck-history command Refactor some event logging code to share with ck-history. At the moment the command only prints the events. --- tools/Makefile.am | 10 ++++ tools/ck-history.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/list-sessions.c | 58 +++++++++---------- 3 files changed, 196 insertions(+), 29 deletions(-) create mode 100644 tools/ck-history.c (limited to 'tools') diff --git a/tools/Makefile.am b/tools/Makefile.am index 5283e18..ad9e589 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -26,6 +26,7 @@ INCLUDES = \ -DLIBEXECDIR=\""$(libexecdir)"\" \ -DDATADIR=\""$(datadir)"\" \ -DSYSCONFDIR=\""$(sysconfdir)"\" \ + -DLOCALSTATEDIR=\""$(localstatedir)"\" \ $(WARN_CFLAGS) \ $(DEBUG_CFLAGS) \ $(DBUS_CFLAGS) \ @@ -33,6 +34,7 @@ INCLUDES = \ bin_PROGRAMS = \ ck-list-sessions \ + ck-history \ $(NULL) ck_list_sessions_SOURCES = \ @@ -43,6 +45,14 @@ ck_list_sessions_LDADD = \ $(CONSOLE_KIT_LIBS) \ $(NULL) +ck_history_SOURCES = \ + ck-history.c \ + $(NULL) + +ck_history_LDADD = \ + $(CONSOLE_KIT_LIBS) \ + $(top_builddir)/src/libck-event-log.la \ + $(NULL) libexec_PROGRAMS = \ ck-collect-session-info \ diff --git a/tools/ck-history.c b/tools/ck-history.c new file mode 100644 index 0000000..9584ee1 --- /dev/null +++ b/tools/ck-history.c @@ -0,0 +1,157 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 William Jon McCann + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "ck-log-event.h" + +typedef enum { + REPORT_TYPE_SUMMARY = 0, +} ReportType; + +#define DEFAULT_LOG_FILENAME LOCALSTATEDIR "/run/ConsoleKit/history" +#define MAX_LINE_LEN 2048 + +static GList *events = NULL; + +static gboolean +process_event_line (const char *line) +{ + GString *str; + CkLogEvent *event; + + str = g_string_new (line); + event = ck_log_event_new_from_string (str); + g_string_free (str, TRUE); + + if (event != NULL) { + events = g_list_prepend (events, event); + } + + return TRUE; +} + +static gboolean +process_log_stream (FILE *fstream) +{ + char line[MAX_LINE_LEN]; + + while (fgets (line, sizeof (line), fstream) != NULL) { + if (strlen (line) == sizeof (line) - 1) { + g_warning ("Log line truncated"); + } + + process_event_line (line); + } + + return TRUE; +} + +static gboolean +process_log_file (const char *filename) +{ + FILE *f; + + f = g_fopen (filename, "r"); + if (f == NULL) { + g_warning ("Error opening %s (%s)\n", + filename, + g_strerror (errno)); + return FALSE; + } + + return process_log_stream (f); +} + +static void +generate_report (void) +{ + GList *l; + + events = g_list_reverse (events); + + for (l = events; l != NULL; l = l->next) { + CkLogEvent *event; + GString *str; + + event = l->data; + str = g_string_new (NULL); + ck_log_event_to_string (event, str); + g_print ("%s\n", str->str); + g_string_free (str, TRUE); + } +} + +static void +free_events (void) +{ + /* FIXME: */ +} + +int +main (int argc, + char **argv) +{ + GOptionContext *context; + gboolean retval; + GError *error = NULL; + static gboolean do_version = FALSE; + static GOptionEntry entries [] = { + { "version", 'V', 0, G_OPTION_ARG_NONE, &do_version, N_("Version of this application"), NULL }, + { NULL } + }; + + context = g_option_context_new (NULL); + g_option_context_add_main_entries (context, entries, NULL); + retval = g_option_context_parse (context, &argc, &argv, &error); + + g_option_context_free (context); + + if (! retval) { + g_warning ("%s", error->message); + g_error_free (error); + exit (1); + } + + if (do_version) { + g_print ("%s %s\n", argv [0], VERSION); + exit (1); + } + + process_log_file (DEFAULT_LOG_FILENAME); + generate_report (); + free_events (); + + return 0; +} diff --git a/tools/list-sessions.c b/tools/list-sessions.c index 75828ed..746c2f7 100644 --- a/tools/list-sessions.c +++ b/tools/list-sessions.c @@ -55,10 +55,10 @@ get_int (DBusGProxy *proxy, gboolean res; error = NULL; - res = dbus_g_proxy_call (proxy, - method, - &error, - G_TYPE_INVALID, + res = dbus_g_proxy_call (proxy, + method, + &error, + G_TYPE_INVALID, G_TYPE_INT, val, G_TYPE_INVALID); if (! res) { @@ -78,10 +78,10 @@ get_path (DBusGProxy *proxy, gboolean res; error = NULL; - res = dbus_g_proxy_call (proxy, - method, - &error, - G_TYPE_INVALID, + res = dbus_g_proxy_call (proxy, + method, + &error, + G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, str, G_TYPE_INVALID); if (! res) { @@ -101,10 +101,10 @@ get_string (DBusGProxy *proxy, gboolean res; error = NULL; - res = dbus_g_proxy_call (proxy, - method, - &error, - G_TYPE_INVALID, + res = dbus_g_proxy_call (proxy, + method, + &error, + G_TYPE_INVALID, G_TYPE_STRING, str, G_TYPE_INVALID); if (! res) { @@ -124,10 +124,10 @@ get_boolean (DBusGProxy *proxy, gboolean res; error = NULL; - res = dbus_g_proxy_call (proxy, - method, - &error, - G_TYPE_INVALID, + res = dbus_g_proxy_call (proxy, + method, + &error, + G_TYPE_INVALID, G_TYPE_BOOLEAN, value, G_TYPE_INVALID); if (! res) { @@ -175,7 +175,7 @@ list_session (DBusGConnection *connection, char *short_sid; const char *short_ssid; - proxy = dbus_g_proxy_new_for_name (connection, + proxy = dbus_g_proxy_new_for_name (connection, CK_NAME, ssid, CK_SESSION_INTERFACE); @@ -256,7 +256,7 @@ list_sessions (DBusGConnection *connection, GPtrArray *sessions; int i; - proxy = dbus_g_proxy_new_for_name (connection, + proxy = dbus_g_proxy_new_for_name (connection, CK_NAME, sid, CK_SEAT_INTERFACE); @@ -267,13 +267,13 @@ list_sessions (DBusGConnection *connection, sessions = NULL; error = NULL; - res = dbus_g_proxy_call (proxy, - "GetSessions", - &error, - G_TYPE_INVALID, + res = dbus_g_proxy_call (proxy, + "GetSessions", + &error, + G_TYPE_INVALID, dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &sessions, - G_TYPE_INVALID); + G_TYPE_INVALID); if (! res) { g_warning ("Failed to get list of sessions for %s: %s", sid, error->message); g_error_free (error); @@ -315,13 +315,13 @@ list_seats (DBusGConnection *connection) seats = NULL; error = NULL; - res = dbus_g_proxy_call (proxy, - "GetSeats", - &error, - G_TYPE_INVALID, + res = dbus_g_proxy_call (proxy, + "GetSeats", + &error, + G_TYPE_INVALID, dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &seats, - G_TYPE_INVALID); + G_TYPE_INVALID); if (! res) { g_warning ("Failed to get list of seats: %s", error->message); g_error_free (error); @@ -387,5 +387,5 @@ main (int argc, list_seats (connection); - return 0; + return 0; } -- cgit