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         GtkTextMark *mark = NULL;
00095 
00096         va_start(args, format);
00097         str = g_strdup_vprintf(format, args);
00098         va_end(args);
00099 
00100         switch (log_level)
00101                 {
00102                 case G_LOG_FLAG_RECURSION:
00103                         display = g_strdup_printf("%s - RECURSION: %s\n%c", log_domain, str, '\0');
00104                         g_print("%s\n", display);
00105                         /* exit(log_level); */
00106                         break;
00107  
00108                 case G_LOG_FLAG_FATAL:
00109                         display = g_strdup_printf("%s - FATAL: %s\n%c", log_domain, str, '\0');
00110                         g_print("%s\n", display);
00111                         /* exit(log_level); */
00112                         break;
00113 
00114                 case G_LOG_LEVEL_ERROR:
00115                         display = g_strdup_printf("%s - ERROR: %s\n%c", log_domain, str, '\0');
00116                         g_print("%s\n", display);
00117                         /* exit(log_level); */
00118                         break;
00119 
00120                 case G_LOG_LEVEL_CRITICAL:
00121                         display = g_strdup_printf("%s - CRITICAL: %s\n%c", log_domain, str, '\0');
00122                         break;
00123 
00124                 case G_LOG_LEVEL_WARNING:
00125                         display = g_strdup_printf("%s - WARNING: %s\n%c", log_domain, str, '\0');
00126                         break;
00127 
00128                 case G_LOG_LEVEL_MESSAGE:
00129                         display = g_strdup_printf("%s - MESSAGE: %s\n%c", log_domain, str, '\0');
00130                         break;
00131 
00132                 case G_LOG_LEVEL_INFO:
00133                         display = g_strdup_printf("%s - INFO: %s\n%c", log_domain, str, '\0');
00134                         break;
00135 
00136                 case G_LOG_LEVEL_DEBUG:
00137                         display = g_strdup_printf("%s - DEBUG: %s\n%c", log_domain, str, '\0');
00138                         break;
00139 
00140                 case G_LOG_LEVEL_MASK: /* To avoid a compilation warning */
00141                         break;
00142                 }
00143 
00144         g_print("%s", display);
00145         tb = GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(logw_textview)));
00146         gtk_text_buffer_get_end_iter(tb, &iStart);
00147         gtk_text_buffer_insert(tb, &iStart, display, -1);
00148         
00149         /* Scrolling down to the new line */
00150         gtk_text_iter_set_line_offset (&iStart, 0);
00151         mark = gtk_text_buffer_get_mark (tb, "scroll");
00152     gtk_text_buffer_move_mark (tb, mark, &iStart);
00153         gtk_text_view_scroll_mark_onscreen (logw_textview, mark);
00154         
00155         g_free(str);
00156         g_free(display);
00157 }
00158 
00159 
00160 /**
00161  * @fn log_message(heraia_window_t *main_window, GLogLevelFlags log_level, const char *format, ...)
00162  *  A function that helps logging a message a the specified level. A wrapper to
00163  *  my_log function log_domain is defined by HERAIA_LOG_DOMAIN
00164  * @param main_window : main structure
00165  * @param log_level : A string that may be either G_LOG_FLAG_RECURSION,
00166  *        G_LOG_FLAG_FATAL, G_LOG_LEVEL_ERROR, G_LOG_LEVEL_CRITICAL,
00167  *        G_LOG_LEVEL_WARNING, G_LOG_LEVEL_MESSAGE, G_LOG_LEVEL_INFO,
00168  *        G_LOG_LEVEL_DEBUG
00169  * @param format : a printf style format
00170  * @param ... : va_list to fill the format.
00171  * @todo may be include the hability to choose a different log domain ?
00172  */
00173 void log_message(heraia_window_t *main_window, GLogLevelFlags log_level, const char *format, ...)
00174 {
00175         va_list args;
00176         gchar *str = NULL;
00177         gchar *str_time = NULL;
00178         gchar *str_time_utf8 = NULL;
00179         gchar *str_utf8 = NULL;
00180         GTimeVal *time = NULL;
00181         GError *err = NULL;
00182 
00183     if (!(main_window->debug == FALSE && log_level == G_LOG_LEVEL_DEBUG))
00184                 {
00185                         g_return_if_fail(format != NULL);
00186 
00187                         va_start(args, format);
00188                         str = g_strdup_vprintf(format, args);
00189                         va_end(args);
00190                         str_utf8 = g_locale_to_utf8(str, -1, NULL, NULL, &err);
00191 
00192                         time = (GTimeVal *) g_malloc0 (sizeof(GTimeVal));
00193                         g_get_current_time(time);
00194                         str_time = g_time_val_to_iso8601(time);
00195                         str_time_utf8 = g_locale_to_utf8(str_time, -1, NULL, NULL, &err);
00196 
00197         
00198                         if (str_utf8)
00199                                 {
00200                                         if (str_time_utf8)
00201                                                 {
00202                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time_utf8, str_utf8, '\0');
00203                                                 }
00204                                         else
00205                                                 {
00206                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time, str_utf8, '\0');
00207                                                 }
00208                                 }
00209                         else
00210                                 {
00211                                         if (str_time_utf8)
00212                                                 {
00213                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time_utf8, str, '\0');
00214                                                 }
00215                                         else
00216                                                 {
00217                                                         my_log(main_window, HERAIA_LOG_DOMAIN, log_level, "%s - %s%c", str_time, str, '\0');
00218                                                 }
00219                                 }
00220 
00221                         g_free(time);
00222                         g_free(str);
00223                         g_free(str_time);
00224                         g_free(str_time_utf8);
00225                         g_free(str_utf8);
00226                 }
00227 }
00228 
00229 /**
00230  * @fn void show_hide_log_window(heraia_window_t *main_window, gboolean show, GtkCheckMenuItem *cmi)
00231  *  Shows and hides the log window
00232  * @param main_window : main structure
00233  * @param show : a boolean to say whether we want to show (TRUE) or hide (FALSE)
00234  *               the window
00235  * @param cmi : the associated check menu item in the menu
00236  */
00237 
00238 void show_hide_log_window(heraia_window_t *main_window, gboolean show, GtkCheckMenuItem *cmi)
00239 {
00240         GtkWidget *log_dialog = NULL;
00241         window_prop_t *log_box_prop = main_window->win_prop->log_box;
00242         
00243         log_dialog = heraia_get_widget(main_window->xmls->main, "log_window");
00244         
00245         if (show == TRUE)
00246            {
00247                         move_and_show_dialog_box(log_dialog, log_box_prop);
00248            }
00249         else
00250           {
00251                   if (log_box_prop->displayed == TRUE)
00252                         {
00253                                 gtk_check_menu_item_set_active(cmi, FALSE);
00254                                 record_and_hide_dialog_box(log_dialog, log_box_prop);
00255                         }
00256           }
00257 }
00258 
00259 /**
00260  * @fn mw_cmi_show_logw_toggle(GtkWidget *widget, gpointer data)
00261  *  The Check menu item for the Log window 
00262  * @param widget : the widget that issued the signal (here the log check menu 
00263  *                 item
00264  * @param data : user data, MUST be main_window main structure
00265  */
00266 void mw_cmi_show_logw_toggle(GtkWidget *widget, gpointer data)
00267 {
00268         heraia_window_t *main_window = (heraia_window_t *) data;
00269         GtkCheckMenuItem *cmi = GTK_CHECK_MENU_ITEM(widget);
00270         gboolean checked = gtk_check_menu_item_get_active(cmi);
00271 
00272         show_hide_log_window(main_window, checked, cmi);
00273 }
00274 
00275 
00276 /**** The Signals ****/
00277 
00278 /** 
00279  * @fn gboolean delete_log_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data )
00280  *  Closing the window 
00281  * @param widget : calling widget 
00282  * @param event : event associated (may be NULL as we don't use this here)
00283  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00284  * @return Always returns TRUE in order to propagate the signal 
00285  */
00286 static gboolean delete_log_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data )
00287 {
00288         logw_close_clicked(widget, data);
00289 
00290         return TRUE;
00291 }
00292 
00293 /**
00294  * @fn void destroy_log_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00295  * When the window is destroyed (Gtk's doc says that we may never get there)
00296  * @param widget : calling widget 
00297  * @param event : event associated (may be NULL as we don't use this here)
00298  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00299 */
00300 static void destroy_log_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00301 {
00302         logw_close_clicked(widget, data);
00303 }
00304 
00305 /**
00306  * @fn  void logw_close_clicked(GtkWidget *widget, gpointer data)
00307  *  Close button is clicked
00308  * @param widget : calling widget 
00309  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00310  */
00311 static void logw_close_clicked(GtkWidget *widget, gpointer data)
00312 {
00313         heraia_window_t *main_window = (heraia_window_t *) data;
00314         GtkWidget *cmi = NULL;
00315         
00316         if (main_window != NULL && main_window->xmls != NULL && main_window->xmls->main != NULL)
00317         {
00318                 cmi = heraia_get_widget(main_window->xmls->main, "mw_cmi_show_logw");
00319                 show_hide_log_window(main_window, FALSE, GTK_CHECK_MENU_ITEM(cmi));
00320         }
00321 }
00322 
00323 
00324 /**
00325  * @fn void log_window_connect_signals(heraia_window_t *main_window)
00326  *  Connecting the window signals to the right functions
00327  * @param main_window : main structure
00328  */
00329 static void log_window_connect_signals(heraia_window_t *main_window)
00330 {
00331 
00332         if (main_window != NULL && main_window->xmls != NULL && main_window->xmls->main != NULL)
00333         {
00334         
00335                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "log_window")), "delete_event", 
00336                                                  G_CALLBACK(delete_log_window_event), main_window);
00337 
00338                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "log_window")), "destroy", 
00339                                                  G_CALLBACK(destroy_log_window), main_window);
00340         
00341                 /* Close Button */
00342                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "logw_close_b")), "clicked", 
00343                                                  G_CALLBACK(logw_close_clicked), main_window);
00344 
00345                 /* the toogle button */
00346                 g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "mw_cmi_show_logw")), "toggled",
00347                                                  G_CALLBACK(mw_cmi_show_logw_toggle), main_window);
00348         }
00349 
00350 }
00351 
00352 /**** End Signals ****/
00353 
00354 /**
00355  * @fn log_window_init_interface(heraia_window_t *main_window)
00356  *  Inits the log window interface 
00357  *  Called once at init time
00358  * @param main_window : main structure 
00359  */
00360 void log_window_init_interface(heraia_window_t *main_window)
00361 {
00362         GtkTextView *logw_textview = NULL;
00363         GtkTextBuffer *tb = NULL;
00364         GtkTextIter iStart;
00365         
00366         
00367         if (main_window != NULL)
00368         {
00369                 /* Connecting signals */
00370                 log_window_connect_signals(main_window);
00371                 
00372                 /* Creating a "scroll" mark on the textview */
00373                 if (main_window->xmls != NULL && main_window->xmls->main != NULL)
00374                 {
00375                         logw_textview = GTK_TEXT_VIEW(heraia_get_widget(main_window->xmls->main, "logw_textview"));
00376                         tb = gtk_text_view_get_buffer(logw_textview);
00377                         gtk_text_buffer_get_end_iter(tb, &iStart);
00378                         gtk_text_buffer_create_mark (tb, "scroll", &iStart, TRUE);
00379                 }
00380         }       
00381 }

Generated on Tue Jun 30 23:18:17 2009 for Heraia by  doxygen 1.5.8