log.c

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003   log.c
00004   log functions for heraia
00005  
00006   (C) Copyright 2006 - 2009 Olivier Delhomme
00007   e-mail : heraia@delhomme.org
00008   URL    : http://heraia.tuxfamily.org 
00009  
00010   This program is free software; you can redistribute it and/or modify
00011   it under the terms of the GNU General Public License as published by
00012   the Free Software Foundation; either version 2, or  (at your option) 
00013   any later version.
00014  
00015   This program is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY;  without even the implied warranty of
00017   MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
00018   GNU General Public License for more details.
00019  
00020   You should have received a copy of the GNU General Public License
00021   along with this program; if not, write to the Free Software
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
00023 */
00024 /**
00025  * @file log.c
00026  * Includes everything that deals with the logging system
00027  */
00028 #include <libheraia.h>
00029 
00030 
00031 static void my_log(heraia_window_t *main_window, gchar *log_domain, GLogLevelFlags log_level, const char *format, ...);
00032 static void log_window_connect_signals(heraia_window_t *main_window);
00033 static gboolean delete_log_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data );
00034 static void destroy_log_window(GtkWidget *widget, GdkEvent  *event, gpointer data);
00035 static void logw_close_clicked(GtkWidget *widget, gpointer data);
00036 
00037 /**
00038  * @fn print_message(const char *format, ...)
00039  * Prints a      message to stdout
00040  * @param format : a printf style format
00041  * @param ... : va_list to fill the format. 
00042  */
00043 void print_message(const char *format, ...)
00044 {
00045         va_list args;
00046         gchar *str = NULL;
00047         gchar *str_utf8 = NULL;
00048         GError *err = NULL;
00049 
00050         g_return_if_fail (format != NULL);
00051         
00052         va_start(args, format);
00053         str = g_strdup_vprintf(format, args);
00054         va_end(args);
00055         
00056         str_utf8 = g_locale_to_utf8(str, -1, NULL, NULL, &err);
00057 
00058         if (str_utf8)
00059                 {
00060                         fputs(str_utf8, stdout);
00061                         g_free(str_utf8);
00062                 }
00063         else
00064                 {
00065                         fprintf(stderr, "Can't convert output to the locale: %s\n", err->message);
00066                         fputs(str, stderr);
00067                         g_error_free(err);
00068                 }
00069 
00070         g_free(str);
00071 }
00072 
00073 
00074 /**
00075  * @fn my_log(heraia_window_t *main_window, gchar *log_domain, GLogLevelFlags log_level, const char *format, ...);
00076  *  A function that allow me to printy things on stdout and in th log window
00077  * @param main_window : main structure
00078  * @param log_domain : should be the program's name
00079  * @param log_level : A string that may be either G_LOG_FLAG_RECURSION,
00080  *        G_LOG_FLAG_FATAL, G_LOG_LEVEL_ERROR, G_LOG_LEVEL_CRITICAL,
00081  *        G_LOG_LEVEL_WARNING, G_LOG_LEVEL_MESSAGE, G_LOG_LEVEL_INFO,
00082  *        G_LOG_LEVEL_DEBUG
00083  * @param format : a printf style format
00084  * @param ... : va_list to fill the format.
00085  */
00086 static void my_log(heraia_window_t *main_window, gchar *log_domain, GLogLevelFlags log_level, const char *format, ...)
00087 {
00088         va_list args;
00089         gchar *str = NULL;
00090         gchar *display = NULL;
00091         GtkTextView *logw_textview = GTK_TEXT_VIEW(heraia_get_widget(main_window->xmls->main, "logw_textview"));
00092         GtkTextBuffer *tb = NULL;
00093         GtkTextIter iStart;
00094 
00095         va_start(args, format);
00096         str = g_strdup_vprintf(format, args);
00097         va_end(args);
00098 
00099         switch (log_level)
00100                 {
00101                 case G_LOG_FLAG_RECURSION:
00102                         display = g_strdup_printf("%s - RECURSION: %s\n%c", log_domain, str, '\0');
00103                         g_print("%s\n", display);
00104                         /* exit(log_level); */
00105                         break;
00106  
00107                 case G_LOG_FLAG_FATAL:
00108                         display = g_strdup_printf("%s - FATAL: %s\n%c", log_domain, str, '\0');
00109                         g_print("%s\n", display);
00110                         /* exit(log_level); */
00111                         break;
00112 
00113                 case G_LOG_LEVEL_ERROR:
00114                         display = g_strdup_printf("%s - ERROR: %s\n%c", log_domain, str, '\0');
00115                         g_print("%s\n", display);
00116                         /* exit(log_level); */
00117                         break;
00118 
00119                 case G_LOG_LEVEL_CRITICAL:
00120                         display = g_strdup_printf("%s - CRITICAL: %s\n%c", log_domain, str, '\0');
00121                         break;
00122 
00123                 case G_LOG_LEVEL_WARNING:
00124                         display = g_strdup_printf("%s - WARNING: %s\n%c", log_domain, str, '\0');
00125                         break;
00126 
00127                 case G_LOG_LEVEL_MESSAGE:
00128                         display = g_strdup_printf("%s - MESSAGE: %s\n%c", log_domain, str, '\0');
00129                         break;
00130 
00131                 case G_LOG_LEVEL_INFO:
00132                         display = g_strdup_printf("%s - INFO: %s\n%c", log_domain, str, '\0');
00133                         break;
00134 
00135                 case G_LOG_LEVEL_DEBUG:
00136                         display = g_strdup_printf("%s - DEBUG: %s\n%c", log_domain, str, '\0');
00137                         break;
00138 
00139                 case G_LOG_LEVEL_MASK: /* To avoid a compilation warning */
00140                         break;
00141                 }
00142 
00143         g_print("%s", display);
00144         tb = GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(logw_textview)));
00145         gtk_text_buffer_get_end_iter(tb, &iStart);
00146         gtk_text_buffer_insert(tb, &iStart, display, -1);
00147         
00148         g_free(str);
00149         g_free(display);
00150 }
00151 
00152 
00153 /**
00154  * @fn log_message(heraia_window_t *main_window, GLogLevelFlags log_level, const char *format, ...)
00155  *  A function that helps logging a message a the specified level. A wrapper to
00156  *  my_log function log_domain is defined by HERAIA_LOG_DOMAIN
00157  * @param main_window : main structure
00158  * @param log_level : A string that may be either G_LOG_FLAG_RECURSION,
00159  *        G_LOG_FLAG_FATAL, G_LOG_LEVEL_ERROR, G_LOG_LEVEL_CRITICAL,
00160  *        G_LOG_LEVEL_WARNING, G_LOG_LEVEL_MESSAGE, G_LOG_LEVEL_INFO,
00161  *        G_LOG_LEVEL_DEBUG
00162  * @param format : a printf style format
00163  * @param ... : va_list to fill the format.
00164  * @todo may be include the hability to choose a different log domain ?
00165  */
00166 void log_message(heraia_window_t *main_window, GLogLevelFlags log_level, const char *format, ...)
00167 {
00168         va_list args;
00169         gchar *str = NULL;
00170         gchar *str_time = NULL;
00171         gchar *str_time_utf8 = NULL;
00172         gchar *str_utf8 = NULL;
00173         GTimeVal *time = NULL;
00174         GError *err = NULL;
00175 
00176     if (!(main_window->debug == FALSE && log_level == G_LOG_LEVEL_DEBUG))
00177                 {
00178                         g_return_if_fail(format != NULL);
00179 
00180                         va_start(args, format);
00181                         str = g_strdup_vprintf(format, args);
00182                         va_end(args);
00183                         str_utf8 = g_locale_to_utf8(str, -1, NULL, NULL, &err);
00184 
00185                         time = (GTimeVal *) g_malloc0 (sizeof(GTimeVal));
00186                         g_get_current_time(time);
00187                         str_time = g_time_val_to_iso8601(time);
00188                         str_time_utf8 = g_locale_to_utf8(str_time, -1, NULL, NULL, &err);
00189 
00190         
00191                         if (str_utf8)
00192                                 {
00193                                         if (str_time_utf8)
00194                                                 {
00195                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time_utf8, str_utf8, '\0');
00196                                                 }
00197                                         else
00198                                                 {
00199                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time, str_utf8, '\0');
00200                                                 }
00201                                 }
00202                         else
00203                                 {
00204                                         if (str_time_utf8)
00205                                                 {
00206                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time_utf8, str, '\0');
00207                                                 }
00208                                         else
00209                                                 {
00210                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time, str, '\0');
00211                                                 }
00212                                 }
00213 
00214                         g_free(time);
00215                         g_free(str);
00216                         g_free(str_time);
00217                         g_free(str_time_utf8);
00218                         g_free(str_utf8);
00219                 }
00220 }
00221 
00222 /**
00223  * @fn void show_hide_log_window(heraia_window_t *main_window, gboolean show, GtkCheckMenuItem *cmi)
00224  *  Shows and hides the log window
00225  * @param main_window : main structure
00226  * @param show : a boolean to say whether we want to show (TRUE) or hide (FALSE)
00227  *               the window
00228  * @param cmi : the associated check menu item in the menu
00229  */
00230 
00231 void show_hide_log_window(heraia_window_t *main_window, gboolean show, GtkCheckMenuItem *cmi)
00232 {
00233         GtkWidget *log_dialog = NULL;
00234         window_prop_t *log_box_prop = main_window->win_prop->log_box;
00235         
00236         log_dialog = heraia_get_widget(main_window->xmls->main, "log_window");
00237         
00238         if (show == TRUE)
00239            {
00240                         move_and_show_dialog_box(log_dialog, log_box_prop);
00241            }
00242         else
00243           {
00244                   if (log_box_prop->displayed == TRUE)
00245                         {
00246                                 gtk_check_menu_item_set_active(cmi, FALSE);
00247                                 record_and_hide_dialog_box(log_dialog, log_box_prop);
00248                         }
00249           }
00250 }
00251 
00252 /**
00253  * @fn mw_cmi_show_logw_toggle(GtkWidget *widget, gpointer data)
00254  *  The Check menu item for the Log window 
00255  * @param widget : the widget that issued the signal (here the log check menu 
00256  *                 item
00257  * @param data : user data, MUST be main_window main structure
00258  */
00259 void mw_cmi_show_logw_toggle(GtkWidget *widget, gpointer data)
00260 {
00261         heraia_window_t *main_window = (heraia_window_t *) data;
00262         GtkCheckMenuItem *cmi = GTK_CHECK_MENU_ITEM(widget);
00263         gboolean checked = gtk_check_menu_item_get_active(cmi);
00264 
00265         show_hide_log_window(main_window, checked, cmi);
00266 }
00267 
00268 
00269 /**** The Signals ****/
00270 
00271 /** 
00272  * @fn gboolean delete_log_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data )
00273  *  Closing the window 
00274  * @param widget : calling widget 
00275  * @param event : event associated (may be NULL as we don't use this here)
00276  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00277  * @return Always returns TRUE in order to propagate the signal 
00278  */
00279 static gboolean delete_log_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data )
00280 {
00281         logw_close_clicked(widget, data);
00282 
00283         return TRUE;
00284 }
00285 
00286 /**
00287  * @fn void destroy_log_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00288  * When the window is destroyed (Gtk's doc says that we may never get there)
00289  * @param widget : calling widget 
00290  * @param event : event associated (may be NULL as we don't use this here)
00291  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00292 */
00293 static void destroy_log_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00294 {
00295         logw_close_clicked(widget, data);
00296 }
00297 
00298 /**
00299  * @fn  void logw_close_clicked(GtkWidget *widget, gpointer data)
00300  *  Close button is clicked
00301  * @param widget : calling widget 
00302  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00303  */
00304 static void logw_close_clicked(GtkWidget *widget, gpointer data)
00305 {
00306         heraia_window_t *main_window = (heraia_window_t *) data;
00307         GtkWidget *cmi = NULL;
00308         
00309         if (main_window != NULL && main_window->xmls != NULL && main_window->xmls->main != NULL)
00310         {
00311                 cmi = heraia_get_widget(main_window->xmls->main, "mw_cmi_show_logw");
00312                 show_hide_log_window(main_window, FALSE, GTK_CHECK_MENU_ITEM(cmi));
00313         }
00314 }
00315 
00316 
00317 /**
00318  * @fn void log_window_connect_signals(heraia_window_t *main_window)
00319  *  Connecting the window signals to the right functions
00320  * @param main_window : main structure
00321  */
00322 static void log_window_connect_signals(heraia_window_t *main_window)
00323 {
00324 
00325         if (main_window != NULL && main_window->xmls != NULL && main_window->xmls->main != NULL)
00326         {
00327         
00328                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "log_window")), "delete_event", 
00329                                                  G_CALLBACK(delete_log_window_event), main_window);
00330 
00331                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "log_window")), "destroy", 
00332                                                  G_CALLBACK(destroy_log_window), main_window);
00333         
00334                 /* Close Button */
00335                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "logw_close_b")), "clicked", 
00336                                                  G_CALLBACK(logw_close_clicked), main_window);
00337 
00338                 /* the toogle button */
00339                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "mw_cmi_show_logw")), "toggled",
00340                                                  G_CALLBACK(mw_cmi_show_logw_toggle), main_window);
00341         }
00342 
00343 }
00344 
00345 /**** End Signals ****/
00346 
00347 /**
00348  * @fn log_window_init_interface(heraia_window_t *main_window)
00349  *  Inits the log window interface 
00350  *  Called once at init time
00351  * @param main_window : main structure 
00352  */
00353 void log_window_init_interface(heraia_window_t *main_window)
00354 {
00355         if (main_window != NULL)
00356         {
00357                 /* Connecting signals */
00358                 log_window_connect_signals(main_window);
00359         }       
00360 }

Generated on Sat Mar 14 13:44:29 2009 for Heraia by  doxygen 1.5.6