1 /* This file is part of MATE Utils.
2  *
3  * MATE Utils is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * MATE Utils is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with MATE Utils.  If not, see <https://www.gnu.org/licenses/>.
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20 
21 #include "../logview-log.h"
22 #include "../logview-utils.h"
23 
24 #include <glib.h>
25 #include <gio/gio.h>
26 
27 static GMainLoop *loop;
28 
29 static void
new_lines_cb(LogviewLog * log,const char ** lines,GSList * new_days,GError * error,gpointer user_data)30 new_lines_cb (LogviewLog *log,
31               const char **lines,
32               GSList *new_days,
33               GError *error,
34               gpointer user_data)
35 {
36   int i;
37   guint8 day;
38   Day *day_s;
39   GSList *days, *l;
40 
41   for (i = 0; lines[i]; i++) {
42     g_print ("line %d: %s\n", i, lines[i]);
43   }
44   g_print ("outside read, lines no %u\n", logview_log_get_cached_lines_number (log));
45 
46   days = log_read_dates (lines, logview_log_get_timestamp (log));
47   g_print ("\ndays %p\n", days);
48 
49   for (l = days; l; l = l->next) {
50     day_s = l->data;
51     g_print ("\nday %u month %u\n", g_date_get_day (day_s->date), g_date_get_month (day_s->date));
52   }
53 
54   g_object_unref (log);
55 
56   g_main_loop_quit (loop);
57 }
58 
59 static void
callback(LogviewLog * log,GError * error,gpointer user_data)60 callback (LogviewLog *log,
61           GError *error,
62           gpointer user_data)
63 {
64   g_print ("callback! err %p, log %p\n", error, log);
65 
66   logview_log_read_new_lines (log, NULL, new_lines_cb, NULL);
67 }
68 
main(int argc,char ** argv)69 int main (int argc, char **argv)
70 {
71   GError *error = NULL;
72   gchar *log_filename = NULL;
73   gchar *usage;
74   GOptionContext *context;
75   GOptionEntry entries [] =
76     { { "file", 'f', 0, G_OPTION_ARG_FILENAME, &log_filename, "The log file, e.g. /var/log/dpkg.log.2.gz", NULL },
77       { NULL } };
78 
79   context = g_option_context_new (NULL);
80   g_option_context_add_main_entries (context, entries, NULL);
81 
82   if (!g_option_context_parse (context, &argc, &argv, &error)) {
83     if (error) {
84       g_printerr ("%s\n\n", error->message);
85       g_error_free (error);
86     }
87     goto arg_error;
88   }
89 
90   if (!log_filename) {
91     g_printerr ("ERROR: You must specify the log file.\n\n");
92     goto arg_error;
93   }
94 
95   g_option_context_free (context);
96 
97   loop = g_main_loop_new (NULL, FALSE);
98   logview_log_create (log_filename, callback, NULL);
99 
100   g_main_loop_run (loop);
101 
102   g_main_loop_unref (loop);
103 
104   g_free (log_filename);
105 
106   return 0;
107 
108 arg_error:
109   usage = g_option_context_get_help (context, TRUE, NULL);
110   g_printerr ("%s", usage);
111   g_free (usage);
112   g_option_context_free (context);
113 
114   return 1;
115 }
116