Re: message prefix for g_log



Hi,

Modified to use g_parse_debug_string() for the env variable contents.

Havoc


Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/glib/ChangeLog,v
retrieving revision 1.677
diff -u -u -r1.677 ChangeLog
--- ChangeLog	2001/04/20 17:08:56	1.677
+++ ChangeLog	2001/04/26 20:02:01
@@ -1,3 +1,10 @@
+2001-04-26  Havoc Pennington  <hp redhat com>
+
+	* configure.in: Get rid of --enable-msg-prefix
+
+	* gmessages.c: make whether to prefix the messages with
+	appname/pid a runtime setting, not a compile-time setting.
+
 2001-04-20  Dan Winship  <danw ximian com>
 
 	* configure.in: Add a check for the Darwin dynamic linker. Use
Index: configure.in
===================================================================
RCS file: /cvs/gnome/glib/configure.in,v
retrieving revision 1.192
diff -u -u -r1.192 configure.in
--- configure.in	2001/04/20 17:08:56	1.192
+++ configure.in	2001/04/26 20:02:02
@@ -101,7 +101,6 @@
 
 dnl declare --enable-* args and collect ac_help strings
 AC_ARG_ENABLE(debug, [  --enable-debug=[no/minimum/yes] turn on debugging [default=$debug_default]],,enable_debug=$debug_default)
-AC_ARG_ENABLE(msg-prefix, [  --enable-msg-prefix     turn on program name and PID prefixing of messages and warnings],,enable_msg_prefix=no)
 AC_ARG_ENABLE(gc_friendly, [  --enable-gc-friendly    turn on garbage collector friendliness [default=no]],,enable_gc_friendly=no)
 AC_ARG_ENABLE(mem_pools, [  --disable-mem-pools     disable all glib memory pools],,disable_mem_pools=no)
 AC_ARG_ENABLE(ansi, [  --enable-ansi           turn on strict ansi [default=no]],
@@ -133,11 +132,6 @@
   AC_DEFINE(DISABLE_MEM_POOLS, 1, [Whether to disable memory pools])
   AC_SUBST(DISABLE_MEM_POOLS)
   AC_MSG_RESULT(yes)
-fi
-
-if test "x$enable_msg_prefix" = "xyes"; then
-  AC_DEFINE_UNQUOTED(G_ENABLE_MSG_PREFIX, 1, 
-		     [Enable prefixing of error messages with program names])
 fi
 
 dnl Checks for programs.
Index: gmessages.c
===================================================================
RCS file: /cvs/gnome/glib/gmessages.c,v
retrieving revision 1.25
diff -u -u -r1.25 gmessages.c
--- gmessages.c	2001/03/09 21:23:33	1.25
+++ gmessages.c	2001/04/26 20:02:02
@@ -78,6 +78,7 @@
 const gchar	     *g_log_domain_glib = "GLib";
 static GLogDomain    *g_log_domains = NULL;
 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
+static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
 static GPrintFunc     glib_print_func = NULL;
 static GPrintFunc     glib_printerr_func = NULL;
 static GErrorFunc     glib_error_func = NULL;
@@ -86,7 +87,6 @@
 
 static GPrivate* g_log_depth = NULL;
 
-
 /* --- functions --- */
 #ifdef G_OS_WIN32
 #  define STRICT
@@ -150,7 +150,57 @@
 #else
 #define ensure_stdout_valid()	/* Define as empty */
 #endif
-	
+
+static void
+g_log_write_prefix (gint           fd,
+                    GLogLevelFlags mask)
+{
+  static gboolean initted = FALSE;
+  gchar prg_pid[64], *prg_name;
+  
+  g_mutex_lock (g_messages_lock);
+
+  if (!initted)
+    {
+      const gchar *val;
+      initted = TRUE;
+
+      val = g_getenv ("G_PREFIX_MESSAGES");
+      
+      if (val)
+        {
+          static const GDebugKey keys[] = {
+            { "error", G_LOG_LEVEL_ERROR },
+            { "critical", G_LOG_LEVEL_CRITICAL },
+            { "warning", G_LOG_LEVEL_WARNING },
+            { "message", G_LOG_LEVEL_MESSAGE },
+            { "info", G_LOG_LEVEL_INFO },
+            { "debug", G_LOG_LEVEL_DEBUG }
+          };
+          
+          g_log_msg_prefix = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
+        }
+    }
+  
+  g_mutex_unlock (g_messages_lock);
+
+  prg_name = g_get_prgname ();
+  
+  if (!prg_name)
+    {
+      prg_name = "(process";
+      sprintf (prg_pid, ":%u): ", getpid ());
+    }
+  else
+    sprintf (prg_pid, " (pid:%u): ", getpid ());
+  
+  if ((g_log_msg_prefix & mask) == mask)
+    {
+      write (fd, prg_name, strlen (prg_name));
+      write (fd, prg_pid, strlen (prg_pid));
+    }
+}
+
 static inline GLogDomain*
 g_log_find_domain (const gchar *log_domain)
 {
@@ -480,7 +530,7 @@
   GWarningFunc local_glib_warning_func;
   GPrintFunc local_glib_message_func;
   gchar prg_pid[64], *prg_name = g_get_prgname ();
-
+  
   in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
   is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
   log_level &= G_LOG_LEVEL_MASK;
@@ -523,10 +573,8 @@
       /* use write(2) for output, in case we are out of memeory */
       ensure_stdout_valid ();
       write (fd, "\n", 1);
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -547,10 +595,8 @@
     case G_LOG_LEVEL_CRITICAL:
       ensure_stdout_valid ();
       write (fd, "\n", 1);
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -577,10 +623,8 @@
 	}
       ensure_stdout_valid ();
       write (fd, "\n", 1);
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -606,10 +650,9 @@
 	  return;
 	}
       ensure_stdout_valid ();
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -627,10 +670,9 @@
       break;
     case G_LOG_LEVEL_INFO:
       ensure_stdout_valid ();
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -648,10 +690,9 @@
       break;
     case G_LOG_LEVEL_DEBUG:
       ensure_stdout_valid ();
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -672,10 +713,9 @@
        * try to make the best out of it.
        */
       ensure_stdout_valid ();
-#ifdef G_ENABLE_MSG_PREFIX
-      write (fd, prg_name, strlen (prg_name));
-      write (fd, prg_pid, strlen (prg_pid));
-#endif /* G_ENABLE_MSG_PREFIX */
+
+      g_log_write_prefix (fd, log_level);
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
Index: docs/reference/glib/tmpl/macros_misc.sgml
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/glib/tmpl/macros_misc.sgml,v
retrieving revision 1.19
diff -u -u -r1.19 macros_misc.sgml
--- docs/reference/glib/tmpl/macros_misc.sgml	2001/04/16 16:34:14	1.19
+++ docs/reference/glib/tmpl/macros_misc.sgml	2001/04/26 20:02:02
@@ -66,6 +66,7 @@
 Portable way to copy <type>va_list</type> variables.
 </para>
 
+<!-- # Unused Parameters # -->
 @ap1: the <type>va_list</type> variable to place a copy of @ap2 in.
 @ap2: a <type>va_list</type>.
 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]