message prefix for g_log



Hi,

We have a configure option --enable-msg-prefix that puts prgname/pid
in the g_log stuff, this patch turns it on by default for all levels
but message/info, and makes it environment-variable-controlled instead
of a configure option. The env variable has two forms, either
G_PREFIX_MESSAGES=no or G_PREFIX_MESSAGES='critical|info' 

G_PREFIX_MESSAGES='' or G_PREFIX_MESSAGES=foo actually ends up being
the same as =no, so maybe just drop the special casing of 'no'

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 19:14:25
@@ -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 19:14:25
@@ -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 19:14:25
@@ -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,64 @@
 #else
 #define ensure_stdout_valid()	/* Define as empty */
 #endif
-	
+
+static void
+g_log_set_msg_prefix_mask (void)
+{
+  static gboolean done = FALSE;
+  
+  g_mutex_lock (g_messages_lock);
+
+  if (done)
+    {
+      g_mutex_unlock (g_messages_lock);
+      return;
+    }
+  
+  done = TRUE;
+
+  {
+    const gchar *val = g_getenv ("G_PREFIX_MESSAGES");
+
+    if (val)
+      {
+        if (strcmp (val, "no") == 0)
+          g_log_msg_prefix = 0;
+        else
+          {
+            gchar **levels;
+            gint i;
+            
+            levels = g_strsplit (val, "|", 10);
+
+            g_log_msg_prefix = 0;
+            i = 0;
+            while (levels[i])
+              {
+                if (strcmp ("error", levels[i]) == 0)
+                  g_log_msg_prefix |= G_LOG_LEVEL_ERROR;
+                else if (strcmp ("critical", levels[i]) == 0)
+                  g_log_msg_prefix |= G_LOG_LEVEL_CRITICAL;
+                else if (strcmp ("warning", levels[i]) == 0)
+                  g_log_msg_prefix |= G_LOG_LEVEL_WARNING;
+                else if (strcmp ("message", levels[i]) == 0)
+                  g_log_msg_prefix |= G_LOG_LEVEL_MESSAGE;
+                else if (strcmp ("info", levels[i]) == 0)
+                  g_log_msg_prefix |= G_LOG_LEVEL_INFO;
+                else if (strcmp ("debug", levels[i]) == 0)
+                  g_log_msg_prefix |= G_LOG_LEVEL_DEBUG;
+                
+                ++i;
+              }
+
+            g_strfreev (levels);
+          }
+      }
+  }
+  
+  g_mutex_unlock (g_messages_lock);
+}
+
 static inline GLogDomain*
 g_log_find_domain (const gchar *log_domain)
 {
@@ -481,6 +538,8 @@
   GPrintFunc local_glib_message_func;
   gchar prg_pid[64], *prg_name = g_get_prgname ();
 
+  g_log_set_msg_prefix_mask ();
+  
   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 +582,12 @@
       /* 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 */
+      if (g_log_msg_prefix & G_LOG_LEVEL_ERROR)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -547,10 +608,12 @@
     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 */
+      if (g_log_msg_prefix & G_LOG_LEVEL_CRITICAL)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -577,10 +640,12 @@
 	}
       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 */
+      if (g_log_msg_prefix & G_LOG_LEVEL_WARNING)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -606,10 +671,13 @@
 	  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 */
+
+      if (g_log_msg_prefix & G_LOG_LEVEL_MESSAGE)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -627,10 +695,13 @@
       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 */
+
+      if (g_log_msg_prefix & G_LOG_LEVEL_INFO)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -648,10 +719,13 @@
       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 */
+
+      if (g_log_msg_prefix & G_LOG_LEVEL_DEBUG)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       if (log_domain)
 	{
 	  write (fd, log_domain, strlen (log_domain));
@@ -672,10 +746,13 @@
        * 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 */
+
+      if (g_log_msg_prefix & log_level)
+        {
+          write (fd, prg_name, strlen (prg_name));
+          write (fd, prg_pid, strlen (prg_pid));
+        }
+
       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 19:14:25
@@ -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]