glib-unix: add function to ensure an fd is sealed
[platform/upstream/glib.git] / glib / gmessages.c
index 38298bf..c1b5622 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-/* 
+/*
  * MT safe
  */
 
+/**
+ * SECTION:warnings
+ * @Title: Message Output and Debugging Functions
+ * @Short_description: functions to output messages and help debug applications
+ *
+ * These functions provide support for outputting messages.
+ *
+ * The g_return family of macros (g_return_if_fail(),
+ * g_return_val_if_fail(), g_return_if_reached(),
+ * g_return_val_if_reached()) should only be used for programming
+ * errors, a typical use case is checking for invalid parameters at
+ * the beginning of a public function. They should not be used if
+ * you just mean "if (error) return", they should only be used if
+ * you mean "if (bug in program) return". The program behavior is
+ * generally considered undefined after one of these checks fails.
+ * They are not intended for normal control flow, only to give a
+ * perhaps-helpful warning before giving up.
+ */
+
 #include "config.h"
 
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #include <signal.h>
 #include <locale.h>
 #include <errno.h>
 
-#include "glib.h"
-#include "gdebug.h"
+#include "glib-init.h"
+#include "gbacktrace.h"
+#include "gcharset.h"
+#include "gconvert.h"
+#include "genviron.h"
+#include "gmem.h"
 #include "gprintfint.h"
-#include "gthreadinit.h"
-#include "galias.h"
+#include "gtestutils.h"
+#include "gthread.h"
+#include "gstrfuncs.h"
+#include "gstring.h"
+#include "gpattern.h"
+
+#ifdef G_OS_UNIX
+#include <unistd.h>
+#endif
 
 #ifdef G_OS_WIN32
 #include <process.h>           /* For getpid() */
 #include <io.h>
-#  define STRICT               /* Strict typing, please */
 #  define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
 #  include <windows.h>
-#  undef STRICT
 #endif
 
+
+/**
+ * SECTION:messages
+ * @title: Message Logging
+ * @short_description: versatile support for logging messages
+ *     with different levels of importance
+ *
+ * These functions provide support for logging error messages
+ * or messages used for debugging.
+ *
+ * There are several built-in levels of messages, defined in
+ * #GLogLevelFlags. These can be extended with user-defined levels.
+ */
+
+/**
+ * G_LOG_DOMAIN:
+ *
+ * Defines the log domain.
+ *
+ * For applications, this is typically left as the default %NULL
+ * (or "") domain. Libraries should define this so that any messages
+ * which they log can be differentiated from messages from other
+ * libraries and application code. But be careful not to define
+ * it in any public header files.
+ *
+ * For example, GTK+ uses this in its Makefile.am:
+ * |[
+ * INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
+ * ]|
+ */
+
+/**
+ * G_LOG_FATAL_MASK:
+ *
+ * GLib log levels that are considered fatal by default.
+ */
+
+/**
+ * GLogFunc:
+ * @log_domain: the log domain of the message
+ * @log_level: the log level of the message (including the
+ *     fatal and recursion flags)
+ * @message: the message to process
+ * @user_data: user data, set in g_log_set_handler()
+ *
+ * Specifies the prototype of log handler functions.
+ *
+ * The default log handler, g_log_default_handler(), automatically appends a
+ * new-line character to @message when printing it. It is advised that any
+ * custom log handler functions behave similarly, so that logging calls in user
+ * code do not need modifying to add a new-line character to the message if the
+ * log handler is changed.
+ */
+
+/**
+ * GLogLevelFlags:
+ * @G_LOG_FLAG_RECURSION: internal flag
+ * @G_LOG_FLAG_FATAL: internal flag
+ * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
+ *     This level is also used for messages produced by g_assert().
+ * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
+ *     This level is also used for messages produced by g_return_if_fail()
+ *     and g_return_val_if_fail().
+ * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
+ * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
+ * @G_LOG_LEVEL_INFO: log level for informational messages, see g_info()
+ * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
+ * @G_LOG_LEVEL_MASK: a mask including all log levels
+ *
+ * Flags specifying the level of log messages.
+ *
+ * It is possible to change how GLib treats messages of the various
+ * levels using g_log_set_handler() and g_log_set_fatal_mask().
+ */
+
+/**
+ * G_LOG_LEVEL_USER_SHIFT:
+ *
+ * Log levels below 1<<G_LOG_LEVEL_USER_SHIFT are used by GLib.
+ * Higher bits can be used for user-defined log levels.
+ */
+
+/**
+ * g_message:
+ * @...: format string, followed by parameters to insert
+ *     into the format string (as with printf())
+ *
+ * A convenience function/macro to log a normal message.
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ */
+
+/**
+ * g_warning:
+ * @...: format string, followed by parameters to insert
+ *     into the format string (as with printf())
+ *
+ * A convenience function/macro to log a warning message.
+ *
+ * You can make warnings fatal at runtime by setting the `G_DEBUG`
+ * environment variable (see
+ * [Running GLib Applications](glib-running.html)).
+ *
+ * If g_log_default_handler() is used as the log handler function,
+ * a newline character will automatically be appended to @..., and
+ * need not be entered manually.
+ */
+
+/**
+ * g_critical:
+ * @...: format string, followed by parameters to insert
+ *     into the format string (as with printf())
+ *
+ * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
+ * It's more or less application-defined what constitutes
+ * a critical vs. a regular warning. You could call
+ * g_log_set_always_fatal() to make critical warnings exit
+ * the program, then use g_critical() for fatal errors, for
+ * example.
+ *
+ * You can also make critical warnings fatal at runtime by
+ * setting the `G_DEBUG` environment variable (see
+ * [Running GLib Applications](glib-running.html)).
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ */
+
+/**
+ * g_error:
+ * @...: format string, followed by parameters to insert
+ *     into the format string (as with printf())
+ *
+ * A convenience function/macro to log an error message.
+ *
+ * Error messages are always fatal, resulting in a call to
+ * abort() to terminate the application. This function will
+ * result in a core dump; don't use it for errors you expect.
+ * Using this function indicates a bug in your program, i.e.
+ * an assertion failure.
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ *
+ */
+
+/**
+ * g_info:
+ * @...: format string, followed by parameters to insert
+ *     into the format string (as with printf())
+ *
+ * A convenience function/macro to log an informational message. Seldom used.
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ *
+ * Such messages are suppressed by the g_log_default_handler() unless
+ * the G_MESSAGES_DEBUG environment variable is set appropriately.
+ *
+ * Since: 2.40
+ */
+
+/**
+ * g_debug:
+ * @...: format string, followed by parameters to insert
+ *     into the format string (as with printf())
+ *
+ * A convenience function/macro to log a debug message.
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ *
+ * Such messages are suppressed by the g_log_default_handler() unless
+ * the G_MESSAGES_DEBUG environment variable is set appropriately.
+ *
+ * Since: 2.6
+ */
+
 /* --- structures --- */
 typedef struct _GLogDomain     GLogDomain;
 typedef struct _GLogHandler    GLogHandler;
@@ -77,21 +284,41 @@ struct _GLogHandler
 
 
 /* --- variables --- */
-static GMutex        *g_messages_lock = NULL;
+static GMutex         g_messages_lock;
 static GLogDomain    *g_log_domains = NULL;
-static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
 static GPrintFunc     glib_print_func = NULL;
 static GPrintFunc     glib_printerr_func = NULL;
-static GPrivate             *g_log_depth = NULL;
-static GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
+static GPrivate       g_log_depth;
 static GLogFunc       default_log_func = g_log_default_handler;
 static gpointer       default_log_data = NULL;
+static GTestLogFatalFunc fatal_log_func = NULL;
+static gpointer          fatal_log_data;
 
 /* --- functions --- */
+
+static void _g_log_abort (gboolean breakpoint);
+
+static void
+_g_log_abort (gboolean breakpoint)
+{
+  if (g_test_subprocess ())
+    {
+      /* If this is a test case subprocess then it probably caused
+       * this error message on purpose, so just exit() rather than
+       * abort()ing, to avoid triggering any system crash-reporting
+       * daemon.
+       */
+      _exit (1);
+    }
+
+  if (breakpoint)
+    G_BREAKPOINT ();
+  else
+    abort ();
+}
+
 #ifdef G_OS_WIN32
-#  define STRICT
 #  include <windows.h>
-#  undef STRICT
 static gboolean win32_keep_fatal_message = FALSE;
 
 /* This default message will usually be overwritten. */
@@ -127,41 +354,16 @@ static void
 write_string (int          fd,
              const gchar *string)
 {
-  write (fd, string, strlen (string));
-}
-
-static void
-g_messages_prefixed_init (void)
-{
-  static gboolean initialized = FALSE;
-
-  if (!initialized)
-    {
-      const gchar *val;
-
-      initialized = TRUE;
-      val = g_getenv ("G_MESSAGES_PREFIXED");
-      
-      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));
-       }
-    }
+  int res;
+  do 
+    res = write (fd, string, strlen (string));
+  while (G_UNLIKELY (res == -1 && errno == EINTR));
 }
 
 static GLogDomain*
 g_log_find_domain_L (const gchar *log_domain)
 {
-  register GLogDomain *domain;
+  GLogDomain *domain;
   
   domain = g_log_domains;
   while (domain)
@@ -176,7 +378,7 @@ g_log_find_domain_L (const gchar *log_domain)
 static GLogDomain*
 g_log_domain_new_L (const gchar *log_domain)
 {
-  register GLogDomain *domain;
+  GLogDomain *domain;
 
   domain = g_new (GLogDomain, 1);
   domain->log_domain = g_strdup (log_domain);
@@ -195,7 +397,7 @@ g_log_domain_check_free_L (GLogDomain *domain)
   if (domain->fatal_mask == G_LOG_FATAL_MASK &&
       domain->handlers == NULL)
     {
-      register GLogDomain *last, *work;
+      GLogDomain *last, *work;
       
       last = NULL;  
 
@@ -225,7 +427,7 @@ g_log_domain_get_handler_L (GLogDomain      *domain,
 {
   if (domain && log_level)
     {
-      register GLogHandler *handler;
+      GLogHandler *handler;
       
       handler = domain->handlers;
       while (handler)
@@ -243,6 +445,22 @@ g_log_domain_get_handler_L (GLogDomain     *domain,
   return default_log_func;
 }
 
+/**
+ * g_log_set_always_fatal:
+ * @fatal_mask: the mask containing bits set for each level
+ *     of error which is to be fatal
+ *
+ * Sets the message levels which are always fatal, in any log domain.
+ * When a message with any of these levels is logged the program terminates.
+ * You can only set the levels defined by GLib to be fatal.
+ * %G_LOG_LEVEL_ERROR is always fatal.
+ *
+ * You can also make some message levels fatal at runtime by setting
+ * the `G_DEBUG` environment variable (see
+ * [Running GLib Applications](glib-running.html)).
+ *
+ * Returns: the old fatal mask
+ */
 GLogLevelFlags
 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
 {
@@ -257,20 +475,30 @@ g_log_set_always_fatal (GLogLevelFlags fatal_mask)
   /* remove bogus flag */
   fatal_mask &= ~G_LOG_FLAG_FATAL;
 
-  g_mutex_lock (g_messages_lock);
+  g_mutex_lock (&g_messages_lock);
   old_mask = g_log_always_fatal;
   g_log_always_fatal = fatal_mask;
-  g_mutex_unlock (g_messages_lock);
+  g_mutex_unlock (&g_messages_lock);
 
   return old_mask;
 }
 
+/**
+ * g_log_set_fatal_mask:
+ * @log_domain: the log domain
+ * @fatal_mask: the new fatal mask
+ *
+ * Sets the log levels which are fatal in the given domain.
+ * %G_LOG_LEVEL_ERROR is always fatal.
+ *
+ * Returns: the old fatal mask for the log domain
+ */
 GLogLevelFlags
 g_log_set_fatal_mask (const gchar   *log_domain,
                      GLogLevelFlags fatal_mask)
 {
   GLogLevelFlags old_flags;
-  register GLogDomain *domain;
+  GLogDomain *domain;
   
   if (!log_domain)
     log_domain = "";
@@ -280,7 +508,7 @@ g_log_set_fatal_mask (const gchar   *log_domain,
   /* remove bogus flag */
   fatal_mask &= ~G_LOG_FLAG_FATAL;
   
-  g_mutex_lock (g_messages_lock);
+  g_mutex_lock (&g_messages_lock);
 
   domain = g_log_find_domain_L (log_domain);
   if (!domain)
@@ -290,11 +518,52 @@ g_log_set_fatal_mask (const gchar   *log_domain,
   domain->fatal_mask = fatal_mask;
   g_log_domain_check_free_L (domain);
 
-  g_mutex_unlock (g_messages_lock);
+  g_mutex_unlock (&g_messages_lock);
 
   return old_flags;
 }
 
+/**
+ * g_log_set_handler:
+ * @log_domain: (allow-none): the log domain, or %NULL for the default ""
+ *     application domain
+ * @log_levels: the log levels to apply the log handler for.
+ *     To handle fatal and recursive messages as well, combine
+ *     the log levels with the #G_LOG_FLAG_FATAL and
+ *     #G_LOG_FLAG_RECURSION bit flags.
+ * @log_func: the log handler function
+ * @user_data: data passed to the log handler
+ *
+ * Sets the log handler for a domain and a set of log levels.
+ * To handle fatal and recursive messages the @log_levels parameter
+ * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
+ * bit flags.
+ *
+ * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
+ * you want to set a handler for this log level you must combine it with
+ * #G_LOG_FLAG_FATAL.
+ *
+ * Here is an example for adding a log handler for all warning messages
+ * in the default domain:
+ * |[<!-- language="C" --> 
+ * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
+ *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
+ * ]|
+ *
+ * This example adds a log handler for all critical messages from GTK+:
+ * |[<!-- language="C" --> 
+ * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
+ *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
+ * ]|
+ *
+ * This example adds a log handler for all messages from GLib:
+ * |[<!-- language="C" --> 
+ * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
+ *                    | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
+ * ]|
+ *
+ * Returns: the id of the new handler
+ */
 guint
 g_log_set_handler (const gchar  *log_domain,
                   GLogLevelFlags log_levels,
@@ -313,7 +582,7 @@ g_log_set_handler (const gchar       *log_domain,
 
   handler = g_new (GLogHandler, 1);
 
-  g_mutex_lock (g_messages_lock);
+  g_mutex_lock (&g_messages_lock);
 
   domain = g_log_find_domain_L (log_domain);
   if (!domain)
@@ -326,38 +595,93 @@ g_log_set_handler (const gchar     *log_domain,
   handler->next = domain->handlers;
   domain->handlers = handler;
 
-  g_mutex_unlock (g_messages_lock);
+  g_mutex_unlock (&g_messages_lock);
   
   return handler_id;
 }
 
+/**
+ * g_log_set_default_handler:
+ * @log_func: the log handler function
+ * @user_data: data passed to the log handler
+ *
+ * Installs a default log handler which is used if no
+ * log handler has been set for the particular log domain
+ * and log level combination. By default, GLib uses
+ * g_log_default_handler() as default log handler.
+ *
+ * Returns: the previous default log handler
+ *
+ * Since: 2.6
+ */
 GLogFunc
 g_log_set_default_handler (GLogFunc log_func,
                           gpointer user_data)
 {
   GLogFunc old_log_func;
   
-  g_mutex_lock (g_messages_lock);
+  g_mutex_lock (&g_messages_lock);
   old_log_func = default_log_func;
   default_log_func = log_func;
   default_log_data = user_data;
-  g_mutex_unlock (g_messages_lock);
+  g_mutex_unlock (&g_messages_lock);
   
   return old_log_func;
 }
 
+/**
+ * g_test_log_set_fatal_handler:
+ * @log_func: the log handler function.
+ * @user_data: data passed to the log handler.
+ *
+ * Installs a non-error fatal log handler which can be
+ * used to decide whether log messages which are counted
+ * as fatal abort the program.
+ *
+ * The use case here is that you are running a test case
+ * that depends on particular libraries or circumstances
+ * and cannot prevent certain known critical or warning
+ * messages. So you install a handler that compares the
+ * domain and message to precisely not abort in such a case.
+ *
+ * Note that the handler is reset at the beginning of
+ * any test case, so you have to set it inside each test
+ * function which needs the special behavior.
+ *
+ * This handler has no effect on g_error messages.
+ *
+ * Since: 2.22
+ **/
+void
+g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
+                              gpointer          user_data)
+{
+  g_mutex_lock (&g_messages_lock);
+  fatal_log_func = log_func;
+  fatal_log_data = user_data;
+  g_mutex_unlock (&g_messages_lock);
+}
+
+/**
+ * g_log_remove_handler:
+ * @log_domain: the log domain
+ * @handler_id: the id of the handler, which was returned
+ *     in g_log_set_handler()
+ *
+ * Removes the log handler.
+ */
 void
 g_log_remove_handler (const gchar *log_domain,
                      guint        handler_id)
 {
-  register GLogDomain *domain;
+  GLogDomain *domain;
   
   g_return_if_fail (handler_id > 0);
   
   if (!log_domain)
     log_domain = "";
   
-  g_mutex_lock (g_messages_lock);
+  g_mutex_lock (&g_messages_lock);
   domain = g_log_find_domain_L (log_domain);
   if (domain)
     {
@@ -374,7 +698,7 @@ g_log_remove_handler (const gchar *log_domain,
              else
                domain->handlers = work->next;
              g_log_domain_check_free_L (domain); 
-             g_mutex_unlock (g_messages_lock);
+             g_mutex_unlock (&g_messages_lock);
              g_free (work);
              return;
            }
@@ -382,185 +706,11 @@ g_log_remove_handler (const gchar *log_domain,
          work = last->next;
        }
     } 
-  g_mutex_unlock (g_messages_lock);
-  g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
+  g_mutex_unlock (&g_messages_lock);
+  g_warning ("%s: could not find handler with id '%d' for domain \"%s\"",
             G_STRLOC, handler_id, log_domain);
 }
 
-void
-g_logv (const gchar   *log_domain,
-       GLogLevelFlags log_level,
-       const gchar   *format,
-       va_list        args1)
-{
-  gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
-  gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
-  gint i;
-
-  log_level &= G_LOG_LEVEL_MASK;
-  if (!log_level)
-    return;
-  
-  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
-    {
-      register GLogLevelFlags test_level;
-      
-      test_level = 1 << i;
-      if (log_level & test_level)
-       {
-         guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
-         GLogDomain *domain;
-         GLogFunc log_func;
-         GLogLevelFlags domain_fatal_mask;
-         gpointer data = NULL;
-
-         if (was_fatal)
-           test_level |= G_LOG_FLAG_FATAL;
-         if (was_recursion)
-           test_level |= G_LOG_FLAG_RECURSION;
-
-         /* check recursion and lookup handler */
-         g_mutex_lock (g_messages_lock);
-         domain = g_log_find_domain_L (log_domain ? log_domain : "");
-         if (depth)
-           test_level |= G_LOG_FLAG_RECURSION;
-         depth++;
-         domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
-         if ((domain_fatal_mask | g_log_always_fatal) & test_level)
-           test_level |= G_LOG_FLAG_FATAL;
-         if (test_level & G_LOG_FLAG_RECURSION)
-           log_func = _g_log_fallback_handler;
-         else
-           log_func = g_log_domain_get_handler_L (domain, test_level, &data);
-         domain = NULL;
-         g_mutex_unlock (g_messages_lock);
-
-         g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
-
-         /* had to defer debug initialization until we can keep track of recursion */
-         if (!(test_level & G_LOG_FLAG_RECURSION) && !_g_debug_initialized)
-           {
-             GLogLevelFlags orig_test_level = test_level;
-
-             _g_debug_init ();
-             if ((domain_fatal_mask | g_log_always_fatal) & test_level)
-               test_level |= G_LOG_FLAG_FATAL;
-             if (test_level != orig_test_level)
-               {
-                 /* need a relookup, not nice, but not too bad either */
-                 g_mutex_lock (g_messages_lock);
-                 domain = g_log_find_domain_L (log_domain ? log_domain : "");
-                 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
-                 domain = NULL;
-                 g_mutex_unlock (g_messages_lock);
-               }
-           }
-
-         if (test_level & G_LOG_FLAG_RECURSION)
-           {
-             /* we use a stack buffer of fixed size, since we're likely
-              * in an out-of-memory situation
-              */
-             gchar buffer[1025];
-             gint size;
-             size = _g_vsnprintf (buffer, 1024, format, args1);
-
-             log_func (log_domain, test_level, buffer, data);
-           }
-         else
-           {
-             gchar *msg = g_strdup_vprintf (format, args1);
-
-             log_func (log_domain, test_level, msg, data);
-
-             g_free (msg);
-           }
-
-         if (test_level & G_LOG_FLAG_FATAL)
-           {
-#ifdef G_OS_WIN32
-             gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
-             
-             MessageBox (NULL, locale_msg, NULL,
-                         MB_ICONERROR|MB_SETFOREGROUND);
-             if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
-               G_BREAKPOINT ();
-
-             abort ();
-#else
-#if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
-             if (!(test_level & G_LOG_FLAG_RECURSION))
-               G_BREAKPOINT ();
-             else
-               abort ();
-#else /* !G_ENABLE_DEBUG || !SIGTRAP */
-             abort ();
-#endif /* !G_ENABLE_DEBUG || !SIGTRAP */
-#endif /* !G_OS_WIN32 */
-           }
-         
-         depth--;
-         g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
-       }
-    }
-}
-
-void
-g_log (const gchar   *log_domain,
-       GLogLevelFlags log_level,
-       const gchar   *format,
-       ...)
-{
-  va_list args;
-  
-  va_start (args, format);
-  g_logv (log_domain, log_level, format, args);
-  va_end (args);
-}
-
-void
-g_return_if_fail_warning (const char *log_domain,
-                         const char *pretty_function,
-                         const char *expression)
-{
-  /*
-   * Omit the prefix used by the PLT-reduction
-   * technique used in GTK+. 
-   */
-  if (g_str_has_prefix (pretty_function, "IA__"))
-    pretty_function += 4;
-  g_log (log_domain,
-        G_LOG_LEVEL_CRITICAL,
-        "%s: assertion `%s' failed",
-        pretty_function,
-        expression);
-}
-
-void
-g_assert_warning (const char *log_domain,
-                 const char *file,
-                 const int   line,
-                 const char *pretty_function,
-                 const char *expression)
-{
-  /*
-   * Omit the prefix used by the PLT-reduction
-   * technique used in GTK+. 
-   */
-  if (g_str_has_prefix (pretty_function, "IA__"))
-    pretty_function += 4;
-  g_log (log_domain,
-        G_LOG_LEVEL_ERROR,
-        expression 
-        ? "file %s: line %d (%s): assertion failed: (%s)"
-        : "file %s: line %d (%s): should not be reached",
-        file, 
-        line, 
-        pretty_function,
-        expression);
-  abort ();
-}
-
 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
                            (wc == 0x7f) || \
                            (wc >= 0x80 && wc < 0xa0)))
@@ -686,6 +836,11 @@ format_unsigned (gchar  *buf,
 
 #define        ALERT_LEVELS            (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
 
+/* these are emitted by the default log handler */
+#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
+/* these are filtered by G_MESSAGES_DEBUG by the default log handler */
+#define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
+
 static int
 mklevel_prefix (gchar          level_prefix[STRING_BUFFER_SIZE],
                GLogLevelFlags log_level)
@@ -734,11 +889,349 @@ mklevel_prefix (gchar          level_prefix[STRING_BUFFER_SIZE],
     strcat (level_prefix, " **");
 
 #ifdef G_OS_WIN32
-  win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
+  if ((log_level & G_LOG_FLAG_FATAL) != 0 && !g_test_initialized ())
+    win32_keep_fatal_message = TRUE;
 #endif
   return to_stdout ? 1 : 2;
 }
 
+typedef struct {
+  gchar          *log_domain;
+  GLogLevelFlags  log_level;
+  gchar          *pattern;
+} GTestExpectedMessage;
+
+static GSList *expected_messages = NULL;
+
+/**
+ * g_logv:
+ * @log_domain: the log domain
+ * @log_level: the log level
+ * @format: the message format. See the printf() documentation
+ * @args: the parameters to insert into the format string
+ *
+ * Logs an error or debugging message.
+ *
+ * If the log level has been set as fatal, the abort()
+ * function is called to terminate the program.
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ */
+void
+g_logv (const gchar   *log_domain,
+       GLogLevelFlags log_level,
+       const gchar   *format,
+       va_list        args)
+{
+  gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
+  gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
+  gchar buffer[1025], *msg, *msg_alloc = NULL;
+  gint i;
+
+  log_level &= G_LOG_LEVEL_MASK;
+  if (!log_level)
+    return;
+
+  if (log_level & G_LOG_FLAG_RECURSION)
+    {
+      /* we use a stack buffer of fixed size, since we're likely
+       * in an out-of-memory situation
+       */
+      gsize size G_GNUC_UNUSED;
+
+      size = _g_vsnprintf (buffer, 1024, format, args);
+      msg = buffer;
+    }
+  else
+    msg = msg_alloc = g_strdup_vprintf (format, args);
+
+  if (expected_messages)
+    {
+      GTestExpectedMessage *expected = expected_messages->data;
+
+      if (g_strcmp0 (expected->log_domain, log_domain) == 0 &&
+          ((log_level & expected->log_level) == expected->log_level) &&
+          g_pattern_match_simple (expected->pattern, msg))
+        {
+          expected_messages = g_slist_delete_link (expected_messages,
+                                                   expected_messages);
+          g_free (expected->log_domain);
+          g_free (expected->pattern);
+          g_free (expected);
+          g_free (msg_alloc);
+          return;
+        }
+      else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
+        {
+          gchar level_prefix[STRING_BUFFER_SIZE];
+          gchar *expected_message;
+
+          mklevel_prefix (level_prefix, expected->log_level);
+          expected_message = g_strdup_printf ("Did not see expected message %s-%s: %s",
+                                              expected->log_domain ? expected->log_domain : "**",
+                                              level_prefix, expected->pattern);
+          g_log_default_handler (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, expected_message, NULL);
+          g_free (expected_message);
+
+          log_level |= G_LOG_FLAG_FATAL;
+        }
+    }
+
+  for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
+    {
+      GLogLevelFlags test_level;
+
+      test_level = 1 << i;
+      if (log_level & test_level)
+       {
+         GLogDomain *domain;
+         GLogFunc log_func;
+         GLogLevelFlags domain_fatal_mask;
+         gpointer data = NULL;
+          gboolean masquerade_fatal = FALSE;
+          guint depth;
+
+         if (was_fatal)
+           test_level |= G_LOG_FLAG_FATAL;
+         if (was_recursion)
+           test_level |= G_LOG_FLAG_RECURSION;
+
+         /* check recursion and lookup handler */
+         g_mutex_lock (&g_messages_lock);
+          depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
+         domain = g_log_find_domain_L (log_domain ? log_domain : "");
+         if (depth)
+           test_level |= G_LOG_FLAG_RECURSION;
+         depth++;
+         domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
+         if ((domain_fatal_mask | g_log_always_fatal) & test_level)
+           test_level |= G_LOG_FLAG_FATAL;
+         if (test_level & G_LOG_FLAG_RECURSION)
+           log_func = _g_log_fallback_handler;
+         else
+           log_func = g_log_domain_get_handler_L (domain, test_level, &data);
+         domain = NULL;
+         g_mutex_unlock (&g_messages_lock);
+
+         g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
+
+          log_func (log_domain, test_level, msg, data);
+
+          if ((test_level & G_LOG_FLAG_FATAL)
+              && !(test_level & G_LOG_LEVEL_ERROR))
+            {
+              masquerade_fatal = fatal_log_func
+                && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
+            }
+
+          if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
+            {
+#ifdef G_OS_WIN32
+              if (win32_keep_fatal_message)
+                {
+                  gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
+
+                  MessageBox (NULL, locale_msg, NULL,
+                              MB_ICONERROR|MB_SETFOREGROUND);
+                }
+             _g_log_abort (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION));
+#else
+             _g_log_abort (!(test_level & G_LOG_FLAG_RECURSION));
+#endif /* !G_OS_WIN32 */
+           }
+         
+         depth--;
+         g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
+       }
+    }
+
+  g_free (msg_alloc);
+}
+
+/**
+ * g_log:
+ * @log_domain: the log domain, usually #G_LOG_DOMAIN
+ * @log_level: the log level, either from #GLogLevelFlags
+ *     or a user-defined level
+ * @format: the message format. See the printf() documentation
+ * @...: the parameters to insert into the format string
+ *
+ * Logs an error or debugging message.
+ *
+ * If the log level has been set as fatal, the abort()
+ * function is called to terminate the program.
+ *
+ * If g_log_default_handler() is used as the log handler function, a new-line
+ * character will automatically be appended to @..., and need not be entered
+ * manually.
+ */
+void
+g_log (const gchar   *log_domain,
+       GLogLevelFlags log_level,
+       const gchar   *format,
+       ...)
+{
+  va_list args;
+  
+  va_start (args, format);
+  g_logv (log_domain, log_level, format, args);
+  va_end (args);
+}
+
+void
+g_return_if_fail_warning (const char *log_domain,
+                         const char *pretty_function,
+                         const char *expression)
+{
+  g_log (log_domain,
+        G_LOG_LEVEL_CRITICAL,
+        "%s: assertion '%s' failed",
+        pretty_function,
+        expression);
+}
+
+void
+g_warn_message (const char     *domain,
+                const char     *file,
+                int             line,
+                const char     *func,
+                const char     *warnexpr)
+{
+  char *s, lstr[32];
+  g_snprintf (lstr, 32, "%d", line);
+  if (warnexpr)
+    s = g_strconcat ("(", file, ":", lstr, "):",
+                     func, func[0] ? ":" : "",
+                     " runtime check failed: (", warnexpr, ")", NULL);
+  else
+    s = g_strconcat ("(", file, ":", lstr, "):",
+                     func, func[0] ? ":" : "",
+                     " ", "code should not be reached", NULL);
+  g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
+  g_free (s);
+}
+
+void
+g_assert_warning (const char *log_domain,
+                 const char *file,
+                 const int   line,
+                 const char *pretty_function,
+                 const char *expression)
+{
+  if (expression)
+    g_log (log_domain,
+          G_LOG_LEVEL_ERROR,
+          "file %s: line %d (%s): assertion failed: (%s)",
+          file,
+          line,
+          pretty_function,
+          expression);
+  else
+    g_log (log_domain,
+          G_LOG_LEVEL_ERROR,
+          "file %s: line %d (%s): should not be reached",
+          file,
+          line,
+          pretty_function);
+  _g_log_abort (FALSE);
+  abort ();
+}
+
+/**
+ * g_test_expect_message:
+ * @log_domain: (allow-none): the log domain of the message
+ * @log_level: the log level of the message
+ * @pattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
+ *
+ * Indicates that a message with the given @log_domain and @log_level,
+ * with text matching @pattern, is expected to be logged. When this
+ * message is logged, it will not be printed, and the test case will
+ * not abort.
+ *
+ * Use g_test_assert_expected_messages() to assert that all
+ * previously-expected messages have been seen and suppressed.
+ *
+ * You can call this multiple times in a row, if multiple messages are
+ * expected as a result of a single call. (The messages must appear in
+ * the same order as the calls to g_test_expect_message().)
+ *
+ * For example:
+ *
+ * |[<!-- language="C" --> 
+ *   // g_main_context_push_thread_default() should fail if the
+ *   // context is already owned by another thread.
+ *   g_test_expect_message (G_LOG_DOMAIN,
+ *                          G_LOG_LEVEL_CRITICAL,
+ *                          "assertion*acquired_context*failed");
+ *   g_main_context_push_thread_default (bad_context);
+ *   g_test_assert_expected_messages ();
+ * ]|
+ *
+ * Note that you cannot use this to test g_error() messages, since
+ * g_error() intentionally never returns even if the program doesn't
+ * abort; use g_test_trap_subprocess() in this case.
+ *
+ * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
+ * expected via g_test_expect_message() then they will be ignored.
+ *
+ * Since: 2.34
+ */
+void
+g_test_expect_message (const gchar    *log_domain,
+                       GLogLevelFlags  log_level,
+                       const gchar    *pattern)
+{
+  GTestExpectedMessage *expected;
+
+  g_return_if_fail (log_level != 0);
+  g_return_if_fail (pattern != NULL);
+  g_return_if_fail (~log_level & G_LOG_LEVEL_ERROR);
+
+  expected = g_new (GTestExpectedMessage, 1);
+  expected->log_domain = g_strdup (log_domain);
+  expected->log_level = log_level;
+  expected->pattern = g_strdup (pattern);
+
+  expected_messages = g_slist_append (expected_messages, expected);
+}
+
+void
+g_test_assert_expected_messages_internal (const char     *domain,
+                                          const char     *file,
+                                          int             line,
+                                          const char     *func)
+{
+  if (expected_messages)
+    {
+      GTestExpectedMessage *expected;
+      gchar level_prefix[STRING_BUFFER_SIZE];
+      gchar *message;
+
+      expected = expected_messages->data;
+
+      mklevel_prefix (level_prefix, expected->log_level);
+      message = g_strdup_printf ("Did not see expected message %s-%s: %s",
+                                 expected->log_domain ? expected->log_domain : "**",
+                                 level_prefix, expected->pattern);
+      g_assertion_message (G_LOG_DOMAIN, file, line, func, message);
+      g_free (message);
+    }
+}
+
+/**
+ * g_test_assert_expected_messages:
+ *
+ * Asserts that all messages previously indicated via
+ * g_test_expect_message() have been seen and suppressed.
+ *
+ * If messages at %G_LOG_LEVEL_DEBUG are emitted, but not explicitly
+ * expected via g_test_expect_message() then they will be ignored.
+ *
+ * Since: 2.34
+ */
+
 void
 _g_log_fallback_handler (const gchar   *log_domain,
                         GLogLevelFlags log_level,
@@ -749,10 +1242,9 @@ _g_log_fallback_handler (const gchar   *log_domain,
 #ifndef G_OS_WIN32
   gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
 #endif
-  gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
   int fd;
 
-  /* we can not call _any_ GLib functions in this fallback handler,
+  /* we cannot call _any_ GLib functions in this fallback handler,
    * which is why we skip UTF-8 conversion, etc.
    * since we either recursed or ran out of memory, we're in a pretty
    * pathologic situation anyways, what we can do is giving the
@@ -786,10 +1278,6 @@ _g_log_fallback_handler (const gchar   *log_domain,
   write_string (fd, level_prefix);
   write_string (fd, ": ");
   write_string (fd, message);
-  if (is_fatal)
-    write_string (fd, "\naborting...\n");
-  else
-    write_string (fd, "\n");
 }
 
 static void
@@ -852,17 +1340,57 @@ escape_string (GString *string)
     }
 }
 
+/**
+ * g_log_default_handler:
+ * @log_domain: the log domain of the message
+ * @log_level: the level of the message
+ * @message: the message
+ * @unused_data: data passed from g_log() which is unused
+ *
+ * The default log handler set up by GLib; g_log_set_default_handler()
+ * allows to install an alternate default log handler.
+ * This is used if no log handler has been set for the particular log
+ * domain and log level combination. It outputs the message to stderr
+ * or stdout and if the log level is fatal it calls abort(). It automatically
+ * prints a new-line character after the message, so one does not need to be
+ * manually included in @message.
+ *
+ * The behavior of this log handler can be influenced by a number of
+ * environment variables:
+ *
+ * - `G_MESSAGES_PREFIXED`: A :-separated list of log levels for which
+ *   messages should be prefixed by the program name and PID of the
+ *   aplication.
+ *
+ * - `G_MESSAGES_DEBUG`: A space-separated list of log domains for
+ *   which debug and informational messages are printed. By default
+ *   these messages are not printed.
+ *
+ * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
+ * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
+ * the rest.
+ */
 void
 g_log_default_handler (const gchar   *log_domain,
                       GLogLevelFlags log_level,
                       const gchar   *message,
                       gpointer       unused_data)
 {
-  gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
   gchar level_prefix[STRING_BUFFER_SIZE], *string;
   GString *gstring;
   int fd;
+  const gchar *domains;
 
+  if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
+    goto emit;
+
+  domains = g_getenv ("G_MESSAGES_DEBUG");
+  if (((log_level & INFO_LEVELS) == 0) ||
+      domains == NULL ||
+      (strcmp (domains, "all") != 0 && (!log_domain || !strstr (domains, log_domain))))
+    return;
+
+ emit:
   /* we can be called externally with recursion for whatever reason */
   if (log_level & G_LOG_FLAG_RECURSION)
     {
@@ -870,8 +1398,6 @@ g_log_default_handler (const gchar   *log_domain,
       return;
     }
 
-  g_messages_prefixed_init ();
-
   fd = mklevel_prefix (level_prefix, log_level);
 
   gstring = g_string_new (NULL);
@@ -880,7 +1406,7 @@ g_log_default_handler (const gchar   *log_domain,
   if (!log_domain)
     g_string_append (gstring, "** ");
 
-  if ((g_log_msg_prefix & log_level) == log_level)
+  if ((g_log_msg_prefix & (log_level & G_LOG_LEVEL_MASK)) == (log_level & G_LOG_LEVEL_MASK))
     {
       const gchar *prg_name = g_get_prgname ();
       
@@ -919,10 +1445,7 @@ g_log_default_handler (const gchar   *log_domain,
 
       g_string_free (msg, TRUE);
     }
-  if (is_fatal)
-    g_string_append (gstring, "\naborting...\n");
-  else
-    g_string_append (gstring, "\n");
+  g_string_append (gstring, "\n");
 
   string = g_string_free (gstring, FALSE);
 
@@ -930,37 +1453,67 @@ g_log_default_handler (const gchar   *log_domain,
   g_free (string);
 }
 
+/**
+ * g_set_print_handler:
+ * @func: the new print handler
+ *
+ * Sets the print handler.
+ *
+ * Any messages passed to g_print() will be output via
+ * the new handler. The default handler simply outputs
+ * the message to stdout. By providing your own handler
+ * you can redirect the output, to a GTK+ widget or a
+ * log file for example.
+ *
+ * Returns: the old print handler
+ */
 GPrintFunc
 g_set_print_handler (GPrintFunc func)
 {
   GPrintFunc old_print_func;
-  
-  g_mutex_lock (g_messages_lock);
+
+  g_mutex_lock (&g_messages_lock);
   old_print_func = glib_print_func;
   glib_print_func = func;
-  g_mutex_unlock (g_messages_lock);
-  
+  g_mutex_unlock (&g_messages_lock);
+
   return old_print_func;
 }
 
+/**
+ * g_print:
+ * @format: the message format. See the printf() documentation
+ * @...: the parameters to insert into the format string
+ *
+ * Outputs a formatted message via the print handler.
+ * The default print handler simply outputs the message to stdout, without
+ * appending a trailing new-line character. Typically, @format should end with
+ * its own new-line character.
+ *
+ * g_print() should not be used from within libraries for debugging
+ * messages, since it may be redirected by applications to special
+ * purpose message windows or even files. Instead, libraries should
+ * use g_log(), or the convenience functions g_message(), g_warning()
+ * and g_error().
+ */
 void
 g_print (const gchar *format,
-        ...)
+         ...)
 {
   va_list args;
   gchar *string;
   GPrintFunc local_glib_print_func;
-  
+
   g_return_if_fail (format != NULL);
-  
+
   va_start (args, format);
   string = g_strdup_vprintf (format, args);
   va_end (args);
-  
-  g_mutex_lock (g_messages_lock);
+
+  g_mutex_lock (&g_messages_lock);
   local_glib_print_func = glib_print_func;
-  g_mutex_unlock (g_messages_lock);
-  
+  g_mutex_unlock (&g_messages_lock);
+
   if (local_glib_print_func)
     local_glib_print_func (string);
   else
@@ -968,50 +1521,78 @@ g_print (const gchar *format,
       const gchar *charset;
 
       if (g_get_charset (&charset))
-       fputs (string, stdout); /* charset is UTF-8 already */
+        fputs (string, stdout); /* charset is UTF-8 already */
       else
-       {
-         gchar *lstring = strdup_convert (string, charset);
+        {
+          gchar *lstring = strdup_convert (string, charset);
 
-         fputs (lstring, stdout);
-         g_free (lstring);
-       }
+          fputs (lstring, stdout);
+          g_free (lstring);
+        }
       fflush (stdout);
     }
   g_free (string);
 }
 
+/**
+ * g_set_printerr_handler:
+ * @func: the new error message handler
+ *
+ * Sets the handler for printing error messages.
+ *
+ * Any messages passed to g_printerr() will be output via
+ * the new handler. The default handler simply outputs the
+ * message to stderr. By providing your own handler you can
+ * redirect the output, to a GTK+ widget or a log file for
+ * example.
+ *
+ * Returns: the old error message handler
+ */
 GPrintFunc
 g_set_printerr_handler (GPrintFunc func)
 {
   GPrintFunc old_printerr_func;
-  
-  g_mutex_lock (g_messages_lock);
+
+  g_mutex_lock (&g_messages_lock);
   old_printerr_func = glib_printerr_func;
   glib_printerr_func = func;
-  g_mutex_unlock (g_messages_lock);
-  
+  g_mutex_unlock (&g_messages_lock);
+
   return old_printerr_func;
 }
 
+/**
+ * g_printerr:
+ * @format: the message format. See the printf() documentation
+ * @...: the parameters to insert into the format string
+ *
+ * Outputs a formatted message via the error message handler.
+ * The default handler simply outputs the message to stderr, without appending
+ * a trailing new-line character. Typically, @format should end with its own
+ * new-line character.
+ *
+ * g_printerr() should not be used from within libraries.
+ * Instead g_log() should be used, or the convenience functions
+ * g_message(), g_warning() and g_error().
+ */
 void
 g_printerr (const gchar *format,
-           ...)
+            ...)
 {
   va_list args;
   gchar *string;
   GPrintFunc local_glib_printerr_func;
-  
+
   g_return_if_fail (format != NULL);
-  
+
   va_start (args, format);
   string = g_strdup_vprintf (format, args);
   va_end (args);
-  
-  g_mutex_lock (g_messages_lock);
+
+  g_mutex_lock (&g_messages_lock);
   local_glib_printerr_func = glib_printerr_func;
-  g_mutex_unlock (g_messages_lock);
-  
+  g_mutex_unlock (&g_messages_lock);
+
   if (local_glib_printerr_func)
     local_glib_printerr_func (string);
   else
@@ -1019,81 +1600,33 @@ g_printerr (const gchar *format,
       const gchar *charset;
 
       if (g_get_charset (&charset))
-       fputs (string, stderr); /* charset is UTF-8 already */
+        fputs (string, stderr); /* charset is UTF-8 already */
       else
-       {
-         gchar *lstring = strdup_convert (string, charset);
+        {
+          gchar *lstring = strdup_convert (string, charset);
 
-         fputs (lstring, stderr);
-         g_free (lstring);
-       }
+          fputs (lstring, stderr);
+          g_free (lstring);
+        }
       fflush (stderr);
     }
   g_free (string);
 }
 
+/**
+ * g_printf_string_upper_bound:
+ * @format: the format string. See the printf() documentation
+ * @args: the parameters to be inserted into the format string
+ *
+ * Calculates the maximum space needed to store the output
+ * of the sprintf() function.
+ *
+ * Returns: the maximum space needed to store the formatted string
+ */
 gsize
 g_printf_string_upper_bound (const gchar *format,
-                            va_list      args)
+                             va_list      args)
 {
   gchar c;
   return _g_vsnprintf (&c, 1, format, args) + 1;
 }
-
-void
-_g_messages_thread_init (void)
-{
-  g_messages_lock = g_mutex_new ();
-  g_messages_prefixed_init ();
-  _g_debug_init ();
-}
-
-void
-_g_messages_thread_private_init (void)
-{
-  g_assert (g_log_depth == NULL);
-  g_log_depth = g_private_new (NULL);
-}
-
-gboolean _g_debug_initialized = FALSE;
-guint _g_debug_flags = 0;
-
-void
-_g_debug_init (void) 
-{
-  const gchar *val;
-  
-  _g_debug_initialized = TRUE;
-  
-  val = g_getenv ("G_DEBUG");
-  if (val != NULL)
-    {
-      static const GDebugKey keys[] = {
-       {"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
-       {"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
-      };
-      
-      _g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
-    }
-  
-  if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS) 
-    {
-      GLogLevelFlags fatal_mask;
-      
-      fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
-      fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
-      g_log_set_always_fatal (fatal_mask);
-    }
-  
-  if (_g_debug_flags & G_DEBUG_FATAL_CRITICALS) 
-    {
-      GLogLevelFlags fatal_mask;
-      
-      fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
-      fatal_mask |= G_LOG_LEVEL_CRITICAL;
-      g_log_set_always_fatal (fatal_mask);
-    }
-}
-
-#define __G_MESSAGES_C__
-#include "galiasdef.c"