1 : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 : /*
3 : log.c
4 : log functions for heraia
5 :
6 : (C) Copyright 2006 - 2008 Olivier Delhomme
7 : e-mail : heraia@delhomme.org
8 : URL : http://heraia.tuxfamily.org
9 :
10 : This program is free software; you can redistribute it and/or modify
11 : it under the terms of the GNU General Public License as published by
12 : the Free Software Foundation; either version 2, or (at your option)
13 : any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program; if not, write to the Free Software
22 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 : */
24 :
25 : #include "heraia_types.h"
26 :
27 : /**
28 : * A function that allow me to printy things on stdout and in th log window
29 : */
30 : static void my_log(heraia_window_t *main_window, gchar *log_domain, GLogLevelFlags log_level, const char *format, ...);
31 :
32 : static void log_window_connect_signals(heraia_window_t *main_window);
33 : static gboolean delete_log_window_event(GtkWidget *widget, GdkEvent *event, gpointer data );
34 : static void destroy_log_window(GtkWidget *widget, GdkEvent *event, gpointer data);
35 : static void logw_close_clicked(GtkWidget *widget, gpointer data);
36 :
37 : /**
38 : * Prints a message to stdout
39 : */
40 : void print_message(const char *format, ...)
41 0 : {
42 : va_list args;
43 0 : gchar *str = NULL;
44 0 : gchar *str_utf8 = NULL;
45 0 : GError *err = NULL;
46 :
47 0 : g_return_if_fail (format != NULL);
48 :
49 0 : va_start(args, format);
50 0 : str = g_strdup_vprintf(format, args);
51 0 : va_end(args);
52 :
53 0 : str_utf8 = g_locale_to_utf8(str, -1, NULL, NULL, &err);
54 :
55 0 : if (str_utf8)
56 : {
57 0 : fputs(str_utf8, stdout);
58 0 : g_free(str_utf8);
59 : }
60 : else
61 : {
62 0 : fprintf(stderr, "Can't convert output to the locale: %s\n", err->message);
63 0 : fputs(str, stderr);
64 0 : g_error_free(err);
65 : }
66 :
67 0 : g_free(str);
68 : }
69 :
70 :
71 : /**
72 : * The log function
73 : */
74 : static void my_log(heraia_window_t *main_window, gchar *log_domain, GLogLevelFlags log_level, const char *format, ...)
75 17 : {
76 : va_list args;
77 17 : gchar *str = NULL;
78 17 : gchar *display = NULL;
79 17 : GtkTextView *logw_textview = GTK_TEXT_VIEW(heraia_get_widget(main_window->xmls->main, "logw_textview"));
80 17 : GtkTextBuffer *tb = NULL;
81 : GtkTextIter iStart;
82 :
83 17 : va_start(args, format);
84 17 : str = g_strdup_vprintf(format, args);
85 17 : va_end(args);
86 :
87 17 : switch (log_level)
88 : {
89 : case G_LOG_FLAG_RECURSION:
90 0 : display = g_strdup_printf("%s - RECURSION: %s\n%c", log_domain, str, '\0');
91 0 : g_print("%s\n", display);
92 : /* exit(log_level); */
93 0 : break;
94 :
95 : case G_LOG_FLAG_FATAL:
96 0 : display = g_strdup_printf("%s - FATAL: %s\n%c", log_domain, str, '\0');
97 0 : g_print("%s\n", display);
98 : /* exit(log_level); */
99 0 : break;
100 :
101 : case G_LOG_LEVEL_ERROR:
102 0 : display = g_strdup_printf("%s - ERROR: %s\n%c", log_domain, str, '\0');
103 0 : g_print("%s\n", display);
104 : /* exit(log_level); */
105 0 : break;
106 :
107 : case G_LOG_LEVEL_CRITICAL:
108 0 : display = g_strdup_printf("%s - CRITICAL: %s\n%c", log_domain, str, '\0');
109 0 : break;
110 :
111 : case G_LOG_LEVEL_WARNING:
112 0 : display = g_strdup_printf("%s - WARNING: %s\n%c", log_domain, str, '\0');
113 0 : break;
114 :
115 : case G_LOG_LEVEL_MESSAGE:
116 0 : display = g_strdup_printf("%s - MESSAGE: %s\n%c", log_domain, str, '\0');
117 0 : break;
118 :
119 : case G_LOG_LEVEL_INFO:
120 7 : display = g_strdup_printf("%s - INFO: %s\n%c", log_domain, str, '\0');
121 7 : break;
122 :
123 : case G_LOG_LEVEL_DEBUG:
124 10 : display = g_strdup_printf("%s - DEBUG: %s\n%c", log_domain, str, '\0');
125 : break;
126 :
127 : case G_LOG_LEVEL_MASK: /* To avoid a compilation warning */
128 : break;
129 : }
130 :
131 17 : g_print("%s", display);
132 17 : tb = GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(logw_textview)));
133 17 : gtk_text_buffer_get_end_iter(tb, &iStart);
134 17 : gtk_text_buffer_insert(tb, &iStart, display, -1);
135 :
136 17 : g_free(str);
137 17 : g_free(display);
138 17 : }
139 :
140 :
141 : /**
142 : * A function that helps logging a message a the specified level
143 : */
144 : void log_message(heraia_window_t *main_window, GLogLevelFlags log_level, const char *format, ...)
145 17 : {
146 : va_list args;
147 17 : gchar *str = NULL;
148 17 : gchar *str_time = NULL;
149 17 : gchar *str_time_utf8 = NULL;
150 17 : gchar *str_utf8 = NULL;
151 17 : GTimeVal *time = NULL;
152 17 : GError *err = NULL;
153 :
154 17 : if (!(main_window->debug == FALSE && log_level == G_LOG_LEVEL_DEBUG))
155 : {
156 17 : g_return_if_fail(format != NULL);
157 :
158 17 : va_start(args, format);
159 17 : str = g_strdup_vprintf(format, args);
160 17 : va_end(args);
161 17 : str_utf8 = g_locale_to_utf8(str, -1, NULL, NULL, &err);
162 :
163 17 : time = (GTimeVal *) g_malloc0 (sizeof(GTimeVal));
164 17 : g_get_current_time(time);
165 17 : str_time = g_time_val_to_iso8601(time);
166 17 : str_time_utf8 = g_locale_to_utf8(str_time, -1, NULL, NULL, &err);
167 :
168 :
169 17 : if (str_utf8)
170 : {
171 17 : if (str_time_utf8)
172 : {
173 17 : my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time_utf8, str_utf8, '\0');
174 : }
175 : else
176 : {
177 0 : my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time, str_utf8, '\0');
178 : }
179 : }
180 : else
181 : {
182 0 : if (str_time_utf8)
183 : {
184 0 : my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time_utf8, str, '\0');
185 : }
186 : else
187 : {
188 0 : my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time, str, '\0');
189 : }
190 : }
191 :
192 17 : g_free(time);
193 17 : g_free(str);
194 17 : g_free(str_time);
195 17 : g_free(str_time_utf8);
196 17 : g_free(str_utf8);
197 : }
198 : }
199 :
200 : /**
201 : * Shows and hides the log window
202 : */
203 :
204 : void show_hide_log_window(heraia_window_t *main_window, gboolean show, GtkCheckMenuItem *cmi)
205 0 : {
206 0 : GtkWidget *log_dialog = NULL;
207 0 : window_prop_t *log_box_prop = main_window->win_prop->log_box;
208 :
209 0 : log_dialog = heraia_get_widget(main_window->xmls->main, "log_window");
210 :
211 0 : if (show == TRUE)
212 : {
213 0 : main_window->win_prop->log_box = move_and_show_dialog_box(log_dialog, log_box_prop);
214 : }
215 : else
216 : {
217 0 : if (log_box_prop->displayed == TRUE)
218 : {
219 0 : gtk_check_menu_item_set_active(cmi, FALSE);
220 0 : main_window->win_prop->log_box = record_and_hide_dialog_box(log_dialog, log_box_prop);
221 : }
222 : }
223 0 : }
224 :
225 : /**
226 : * The Check menu item for the Log window
227 : */
228 : void mw_cmi_affiche_logw_toggle(GtkWidget *widget, gpointer data)
229 0 : {
230 0 : heraia_window_t *main_window = (heraia_window_t *) data;
231 0 : GtkCheckMenuItem *cmi = GTK_CHECK_MENU_ITEM(widget);
232 0 : gboolean checked = gtk_check_menu_item_get_active(cmi);
233 :
234 0 : show_hide_log_window(main_window, checked, cmi);
235 0 : }
236 :
237 :
238 : /**** The Signals ****/
239 :
240 : /**
241 : * Closing the window
242 : */
243 : static gboolean delete_log_window_event(GtkWidget *widget, GdkEvent *event, gpointer data )
244 0 : {
245 0 : logw_close_clicked(widget, data);
246 :
247 0 : return FALSE;
248 : }
249 :
250 : static void destroy_log_window(GtkWidget *widget, GdkEvent *event, gpointer data)
251 0 : {
252 0 : logw_close_clicked(widget, data);
253 0 : }
254 :
255 : /**
256 : * Close button is clicked
257 : */
258 : static void logw_close_clicked(GtkWidget *widget, gpointer data)
259 0 : {
260 0 : heraia_window_t *main_window = (heraia_window_t *) data;
261 0 : GtkCheckMenuItem *cmi = GTK_CHECK_MENU_ITEM(heraia_get_widget(main_window->xmls->main, "mw_cmi_affiche_logw"));
262 :
263 0 : show_hide_log_window(main_window, FALSE, cmi);
264 0 : }
265 :
266 :
267 : /**
268 : * Connecting the window signals to the right functions
269 : */
270 : static void log_window_connect_signals(heraia_window_t *main_window)
271 1 : {
272 :
273 1 : g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "log_window")), "delete_event",
274 : G_CALLBACK(delete_log_window_event), main_window);
275 :
276 1 : g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "log_window")), "destroy",
277 : G_CALLBACK(destroy_log_window), main_window);
278 :
279 : /* Close Button */
280 1 : g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "logw_close_b")), "clicked",
281 : G_CALLBACK(logw_close_clicked), main_window);
282 :
283 : /* the toogle button */
284 1 : g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "mw_cmi_affiche_logw")), "toggled",
285 : G_CALLBACK(mw_cmi_affiche_logw_toggle), main_window);
286 :
287 1 : }
288 :
289 : /**** End Signals ****/
290 :
291 :
292 :
293 : /**
294 : * Inits the log window interface
295 : */
296 : void log_window_init_interface(heraia_window_t *main_window)
297 1 : {
298 1 : log_window_connect_signals(main_window);
299 1 : }
|