Ethereal-dev: [Ethereal-dev] Improvement of logging functions in Ethereal on Win32

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Lars Roland <lars.roland@xxxxxxx>
Date: Fri, 27 Aug 2004 07:01:58 +0200
Hello all,

I patched the logging funtions controling the output to a console window on Win32.
We can get more information about GTK and GLib Messages now.
It is also possible to disable output of non-fatal GTK and GLib Messages completely by choosing GUI-Preference "Open a console window -> never".

Attached are:
-patch for main.c
From patched Ethereal:
-console output of warnings from the about dialog as plain text file
 (I had to open the console window before I opened the about dialog
 to catch the first message)
-console output of warnings and an error when choosing the last font
 in list (unfortunately a screenshot, no copy'n'paste possible with
 this error) The 2nd warning and the error say that they come from
 ethereal, but they come from Pango. Pango has obviously no
 "Logging Domain".

I think this will help a lot.
Please check in.

Regards,
Lars
Index: ethereal-new/gtk/main.c
===================================================================
--- ethereal-new/gtk/main.c	(revision 11842)
+++ ethereal-new/gtk/main.c	(working copy)
@@ -106,6 +106,7 @@
 #endif
 #ifdef WIN32
 #include "capture-wpcap.h"
+#include <process.h>
 #endif
 
 /* GTK related */
@@ -1598,17 +1599,40 @@
      routine is called to log a message, we pop up a console window.
 
      We do that by inserting our own handler for all messages logged
-     to the default domain; that handler pops up a console if necessary,
-     and then calls the default handler. */
+     to the default domain (our domain) and all other domains used by GLib 
+     and GTK; that handler pops up a console if necessary and prints our 
+     messages, unless we suppress all console output. However fatal errors 
+     get displayed anyway. */
+  /* References:
+     http://developer.gnome.org/doc/API/2.0/glib/glib-Message-Logging.html#g-log-set-handler
+     http://mail.gnome.org/archives/gtk-list/2003-June/msg00175.html */
   g_log_set_handler(NULL,
-		    G_LOG_LEVEL_ERROR|
-		    G_LOG_LEVEL_CRITICAL|
-		    G_LOG_LEVEL_WARNING|
-		    G_LOG_LEVEL_MESSAGE|
-		    G_LOG_LEVEL_INFO|
-		    G_LOG_LEVEL_DEBUG|
-		    G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION,
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
 		    console_log_handler, NULL);
+		    
+  g_log_set_handler("Gdk",
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+		    console_log_handler, NULL);
+
+  g_log_set_handler("Gtk",
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+		    console_log_handler, NULL);
+		    
+  g_log_set_handler("GLib",
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+		    console_log_handler, NULL);
+		    
+  g_log_set_handler("GLib-GObject",
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+		    console_log_handler, NULL);
+		    
+  g_log_set_handler("GModule",
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+		    console_log_handler, NULL);
+		    
+  g_log_set_handler("GThread",
+		    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
+		    console_log_handler, NULL);
 #endif
 
   command_name = get_basename(ethereal_path);
@@ -2626,28 +2650,52 @@
   }
 }
 
-/* This routine should not be necessary, at least as I read the GLib
-   source code, as it looks as if GLib is, on Win32, *supposed* to
-   create a console window into which to display its output.
+/* We use our own log handler instead of g_log_default_handler().
+   This gives us the possibility to completely suppress these messages*/
 
-   That doesn't happen, however.  I suspect there's something completely
-   broken about that code in GLib-for-Win32, and that it may be related
-   to the breakage that forces us to just call "printf()" on the message
-   rather than passing the message on to "g_log_default_handler()"
-   (which is the routine that does the aforementioned non-functional
-   console window creation). */
 static void
 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
 		    const char *message, gpointer user_data)
 {
   create_console();
   if (has_console) {
-    /* For some unknown reason, the above doesn't appear to actually cause
-       anything to be sent to the standard output, so we'll just splat the
-       message out directly, just to make sure it gets out. */
-    printf("%s\n", message);
-  } else
-    g_log_default_handler(log_domain, log_level, message, user_data);
+    /* This is never true if "console_open_never" is selected. 
+       Thus everything is suppressed */
+    printf(" ------ \n");
+    if (log_domain==NULL) {
+    	/* Message from Ethereal itself */
+    	printf("From module: Ethereal\n");
+    }
+    else {
+    	printf("From module: %s\n", log_domain);	
+    }
+    	
+    if(log_level & G_LOG_LEVEL_ERROR)
+    	printf("Category: Error\n");
+    else if(log_level & G_LOG_LEVEL_CRITICAL)
+    	printf("Category: Critical\n");
+    else if(log_level & G_LOG_LEVEL_WARNING)
+    	printf("Category: Warning\n");
+    else if(log_level & G_LOG_LEVEL_MESSAGE)
+    	printf("Category: Message\n");
+    else if(log_level & G_LOG_LEVEL_INFO)
+    	printf("Category: Informational\n");
+    else if(log_level & G_LOG_LEVEL_DEBUG)
+    	printf("Category: Debugging\n");
+    else 
+    	printf("Category: Unknown\n");
+       
+    printf("Process ID: %d\n", _getpid());   
+    printf("Message: %s\n ------ \n", message);
+  } 
+  else
+  {
+    if(log_level & G_LOG_FLAG_FATAL) {
+    	/* display fatal errors anyway. They will make ethereal to abort, 
+    	   and we want to have some info in that case */
+	g_log_default_handler(log_domain, log_level, message, user_data);
+    }
+  }
 }
 #endif
 
Invalid utf8 encoding: J÷rg Mayer              <jmayer[AT]loplof.de>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Michael T³xen           <tuexen [AT] fh-muenster.de>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Kent Engstr÷m           <kent[AT]unit.liu.se>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Eduardo PÚrez Ureta     <eperez[AT]dei.inf.uc3m.es>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Ricardo Barroetave±a    <rbarroetavena[AT]veufort.com>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Erik Nordstr÷m          <erik.nordstrom[AT]it.uu.se>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Lo´c Minier             <lool [AT] dooz.org>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Love H÷rnquist +strand  <lha [AT] it.su.se>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: Dominic BÚchaz          <bdo [AT] zhwin.ch>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------
Invalid utf8 encoding: RÚmi Denis-Courmont      <courmisch [AT] via.ecp.fr>

 ------
>From module: Gtk
Category: Critical
Process ID: 3452
Message: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed
 ------


Press any key to exit

PNG image