1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 * @Title: Message Output and Debugging Functions
34 * @Short_description: functions to output messages and help debug applications
36 * These functions provide support for outputting messages.
38 * The <function>g_return</function> family of macros (g_return_if_fail(),
39 * g_return_val_if_fail(), g_return_if_reached(), g_return_val_if_reached())
40 * should only be used for programming errors, a typical use case is
41 * checking for invalid parameters at the beginning of a public function.
42 * They should not be used if you just mean "if (error) return", they
43 * should only be used if you mean "if (bug in program) return".
44 * The program behavior is generally considered undefined after one
45 * of these checks fails. They are not intended for normal control
46 * flow, only to give a perhaps-helpful warning before giving up.
62 #include "gmessages.h"
64 #include "glib-init.h"
65 #include "gbacktrace.h"
69 #include "gprintfint.h"
70 #include "gtestutils.h"
72 #include "gstrfuncs.h"
76 #include <process.h> /* For getpid() */
78 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
85 * @title: Message Logging
86 * @short_description: versatile support for logging messages
87 * with different levels of importance
89 * These functions provide support for logging error messages
90 * or messages used for debugging.
92 * There are several built-in levels of messages, defined in
93 * #GLogLevelFlags. These can be extended with user-defined levels.
99 * Defines the log domain.
101 * For applications, this is typically left as the default %NULL
102 * (or "") domain. Libraries should define this so that any messages
103 * which they log can be differentiated from messages from other
104 * libraries and application code. But be careful not to define
105 * it in any public header files.
107 * For example, GTK+ uses this in its Makefile.am:
109 * INCLUDES = -DG_LOG_DOMAIN=\"Gtk\"
116 * GLib log levels that are considered fatal by default.
121 * @log_domain: the log domain of the message
122 * @log_level: the log level of the message (including the
123 * fatal and recursion flags)
124 * @message: the message to process
125 * @user_data: user data, set in g_log_set_handler()
127 * Specifies the prototype of log handler functions.
132 * @G_LOG_FLAG_RECURSION: internal flag
133 * @G_LOG_FLAG_FATAL: internal flag
134 * @G_LOG_LEVEL_ERROR: log level for errors, see g_error().
135 * This level is also used for messages produced by g_assert().
136 * @G_LOG_LEVEL_CRITICAL: log level for critical messages, see g_critical().
137 * This level is also used for messages produced by g_return_if_fail()
138 * and g_return_val_if_fail().
139 * @G_LOG_LEVEL_WARNING: log level for warnings, see g_warning()
140 * @G_LOG_LEVEL_MESSAGE: log level for messages, see g_message()
141 * @G_LOG_LEVEL_INFO: log level for informational messages
142 * @G_LOG_LEVEL_DEBUG: log level for debug messages, see g_debug()
143 * @G_LOG_LEVEL_MASK: a mask including all log levels
145 * Flags specifying the level of log messages.
147 * It is possible to change how GLib treats messages of the various
148 * levels using g_log_set_handler() and g_log_set_fatal_mask().
153 * @...: format string, followed by parameters to insert
154 * into the format string (as with printf())
156 * A convenience function/macro to log a normal message.
161 * @...: format string, followed by parameters to insert
162 * into the format string (as with printf())
164 * A convenience function/macro to log a warning message.
166 * You can make warnings fatal at runtime by setting the
167 * %G_DEBUG environment variable (see
168 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
173 * @...: format string, followed by parameters to insert
174 * into the format string (as with printf())
176 * Logs a "critical warning" (#G_LOG_LEVEL_CRITICAL).
177 * It's more or less application-defined what constitutes
178 * a critical vs. a regular warning. You could call
179 * g_log_set_always_fatal() to make critical warnings exit
180 * the program, then use g_critical() for fatal errors, for
183 * You can also make critical warnings fatal at runtime by
184 * setting the %G_DEBUG environment variable (see
185 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
190 * @...: format string, followed by parameters to insert
191 * into the format string (as with printf())
193 * A convenience function/macro to log an error message.
195 * Error messages are always fatal, resulting in a call to
196 * abort() to terminate the application. This function will
197 * result in a core dump; don't use it for errors you expect.
198 * Using this function indicates a bug in your program, i.e.
199 * an assertion failure.
205 * @...: format string, followed by parameters to insert
206 * into the format string (as with printf())
208 * A convenience function/macro to log a debug message.
213 /* --- structures --- */
214 typedef struct _GLogDomain GLogDomain;
215 typedef struct _GLogHandler GLogHandler;
219 GLogLevelFlags fatal_mask;
220 GLogHandler *handlers;
226 GLogLevelFlags log_level;
233 /* --- variables --- */
234 static GMutex g_messages_lock;
235 static GLogDomain *g_log_domains = NULL;
236 static GPrintFunc glib_print_func = NULL;
237 static GPrintFunc glib_printerr_func = NULL;
238 static GPrivate g_log_depth;
239 static GLogFunc default_log_func = g_log_default_handler;
240 static gpointer default_log_data = NULL;
241 static GTestLogFatalFunc fatal_log_func = NULL;
242 static gpointer fatal_log_data;
244 /* --- functions --- */
246 # include <windows.h>
247 static gboolean win32_keep_fatal_message = FALSE;
249 /* This default message will usually be overwritten. */
250 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
251 * called with huge strings, is it?
253 static gchar fatal_msg_buf[1000] = "Unspecified fatal error encountered, aborting.";
254 static gchar *fatal_msg_ptr = fatal_msg_buf;
262 if (win32_keep_fatal_message)
264 memcpy (fatal_msg_ptr, buf, len);
265 fatal_msg_ptr += len;
270 write (fd, buf, len);
274 #define write(fd, buf, len) dowrite(fd, buf, len)
279 write_string (int fd,
282 write (fd, string, strlen (string));
286 g_log_find_domain_L (const gchar *log_domain)
288 register GLogDomain *domain;
290 domain = g_log_domains;
293 if (strcmp (domain->log_domain, log_domain) == 0)
295 domain = domain->next;
301 g_log_domain_new_L (const gchar *log_domain)
303 register GLogDomain *domain;
305 domain = g_new (GLogDomain, 1);
306 domain->log_domain = g_strdup (log_domain);
307 domain->fatal_mask = G_LOG_FATAL_MASK;
308 domain->handlers = NULL;
310 domain->next = g_log_domains;
311 g_log_domains = domain;
317 g_log_domain_check_free_L (GLogDomain *domain)
319 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
320 domain->handlers == NULL)
322 register GLogDomain *last, *work;
326 work = g_log_domains;
332 last->next = domain->next;
334 g_log_domains = domain->next;
335 g_free (domain->log_domain);
346 g_log_domain_get_handler_L (GLogDomain *domain,
347 GLogLevelFlags log_level,
350 if (domain && log_level)
352 register GLogHandler *handler;
354 handler = domain->handlers;
357 if ((handler->log_level & log_level) == log_level)
359 *data = handler->data;
360 return handler->log_func;
362 handler = handler->next;
366 *data = default_log_data;
367 return default_log_func;
371 * g_log_set_always_fatal:
372 * @fatal_mask: the mask containing bits set for each level
373 * of error which is to be fatal
375 * Sets the message levels which are always fatal, in any log domain.
376 * When a message with any of these levels is logged the program terminates.
377 * You can only set the levels defined by GLib to be fatal.
378 * %G_LOG_LEVEL_ERROR is always fatal.
380 * You can also make some message levels fatal at runtime by setting
381 * the %G_DEBUG environment variable (see
382 * <ulink url="glib-running.html">Running GLib Applications</ulink>).
384 * Returns: the old fatal mask
387 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
389 GLogLevelFlags old_mask;
391 /* restrict the global mask to levels that are known to glib
392 * since this setting applies to all domains
394 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
395 /* force errors to be fatal */
396 fatal_mask |= G_LOG_LEVEL_ERROR;
397 /* remove bogus flag */
398 fatal_mask &= ~G_LOG_FLAG_FATAL;
400 g_mutex_lock (&g_messages_lock);
401 old_mask = g_log_always_fatal;
402 g_log_always_fatal = fatal_mask;
403 g_mutex_unlock (&g_messages_lock);
409 * g_log_set_fatal_mask:
410 * @log_domain: the log domain
411 * @fatal_mask: the new fatal mask
413 * Sets the log levels which are fatal in the given domain.
414 * %G_LOG_LEVEL_ERROR is always fatal.
416 * Returns: the old fatal mask for the log domain
419 g_log_set_fatal_mask (const gchar *log_domain,
420 GLogLevelFlags fatal_mask)
422 GLogLevelFlags old_flags;
423 register GLogDomain *domain;
428 /* force errors to be fatal */
429 fatal_mask |= G_LOG_LEVEL_ERROR;
430 /* remove bogus flag */
431 fatal_mask &= ~G_LOG_FLAG_FATAL;
433 g_mutex_lock (&g_messages_lock);
435 domain = g_log_find_domain_L (log_domain);
437 domain = g_log_domain_new_L (log_domain);
438 old_flags = domain->fatal_mask;
440 domain->fatal_mask = fatal_mask;
441 g_log_domain_check_free_L (domain);
443 g_mutex_unlock (&g_messages_lock);
450 * @log_domain: the log domain, or %NULL for the default ""
452 * @log_levels: the log levels to apply the log handler for.
453 * To handle fatal and recursive messages as well, combine
454 * the log levels with the #G_LOG_FLAG_FATAL and
455 * #G_LOG_FLAG_RECURSION bit flags.
456 * @log_func: the log handler function
457 * @user_data: data passed to the log handler
459 * Sets the log handler for a domain and a set of log levels.
460 * To handle fatal and recursive messages the @log_levels parameter
461 * must be combined with the #G_LOG_FLAG_FATAL and #G_LOG_FLAG_RECURSION
464 * Note that since the #G_LOG_LEVEL_ERROR log level is always fatal, if
465 * you want to set a handler for this log level you must combine it with
469 * <title>Adding a log handler for all warning messages in the default
470 * (application) domain</title>
472 * g_log_set_handler (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
473 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
478 * <title>Adding a log handler for all critical messages from GTK+</title>
480 * g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
481 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
486 * <title>Adding a log handler for <emphasis>all</emphasis> messages from
489 * g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
490 * | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
494 * Returns: the id of the new handler
497 g_log_set_handler (const gchar *log_domain,
498 GLogLevelFlags log_levels,
502 static guint handler_id = 0;
504 GLogHandler *handler;
506 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
507 g_return_val_if_fail (log_func != NULL, 0);
512 handler = g_new (GLogHandler, 1);
514 g_mutex_lock (&g_messages_lock);
516 domain = g_log_find_domain_L (log_domain);
518 domain = g_log_domain_new_L (log_domain);
520 handler->id = ++handler_id;
521 handler->log_level = log_levels;
522 handler->log_func = log_func;
523 handler->data = user_data;
524 handler->next = domain->handlers;
525 domain->handlers = handler;
527 g_mutex_unlock (&g_messages_lock);
533 * g_log_set_default_handler:
534 * @log_func: the log handler function
535 * @user_data: data passed to the log handler
537 * Installs a default log handler which is used if no
538 * log handler has been set for the particular log domain
539 * and log level combination. By default, GLib uses
540 * g_log_default_handler() as default log handler.
542 * Returns: the previous default log handler
547 g_log_set_default_handler (GLogFunc log_func,
550 GLogFunc old_log_func;
552 g_mutex_lock (&g_messages_lock);
553 old_log_func = default_log_func;
554 default_log_func = log_func;
555 default_log_data = user_data;
556 g_mutex_unlock (&g_messages_lock);
562 * g_test_log_set_fatal_handler:
563 * @log_func: the log handler function.
564 * @user_data: data passed to the log handler.
566 * Installs a non-error fatal log handler which can be
567 * used to decide whether log messages which are counted
568 * as fatal abort the program.
570 * The use case here is that you are running a test case
571 * that depends on particular libraries or circumstances
572 * and cannot prevent certain known critical or warning
573 * messages. So you install a handler that compares the
574 * domain and message to precisely not abort in such a case.
576 * Note that the handler is reset at the beginning of
577 * any test case, so you have to set it inside each test
578 * function which needs the special behavior.
580 * This handler has no effect on g_error messages.
585 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
588 g_mutex_lock (&g_messages_lock);
589 fatal_log_func = log_func;
590 fatal_log_data = user_data;
591 g_mutex_unlock (&g_messages_lock);
595 * g_log_remove_handler:
596 * @log_domain: the log domain
597 * @handler_id: the id of the handler, which was returned
598 * in g_log_set_handler()
600 * Removes the log handler.
603 g_log_remove_handler (const gchar *log_domain,
606 register GLogDomain *domain;
608 g_return_if_fail (handler_id > 0);
613 g_mutex_lock (&g_messages_lock);
614 domain = g_log_find_domain_L (log_domain);
617 GLogHandler *work, *last;
620 work = domain->handlers;
623 if (work->id == handler_id)
626 last->next = work->next;
628 domain->handlers = work->next;
629 g_log_domain_check_free_L (domain);
630 g_mutex_unlock (&g_messages_lock);
638 g_mutex_unlock (&g_messages_lock);
639 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
640 G_STRLOC, handler_id, log_domain);
645 * @log_domain: the log domain
646 * @log_level: the log level
647 * @format: the message format. See the printf() documentation
648 * @args: the parameters to insert into the format string
650 * Logs an error or debugging message.
652 * If the log level has been set as fatal, the abort()
653 * function is called to terminate the program.
656 g_logv (const gchar *log_domain,
657 GLogLevelFlags log_level,
661 gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
662 gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
665 log_level &= G_LOG_LEVEL_MASK;
669 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
671 register GLogLevelFlags test_level;
674 if (log_level & test_level)
678 GLogLevelFlags domain_fatal_mask;
679 gpointer data = NULL;
680 gboolean masquerade_fatal = FALSE;
684 test_level |= G_LOG_FLAG_FATAL;
686 test_level |= G_LOG_FLAG_RECURSION;
688 /* check recursion and lookup handler */
689 g_mutex_lock (&g_messages_lock);
690 depth = GPOINTER_TO_UINT (g_private_get (&g_log_depth));
691 domain = g_log_find_domain_L (log_domain ? log_domain : "");
693 test_level |= G_LOG_FLAG_RECURSION;
695 domain_fatal_mask = domain ? domain->fatal_mask : G_LOG_FATAL_MASK;
696 if ((domain_fatal_mask | g_log_always_fatal) & test_level)
697 test_level |= G_LOG_FLAG_FATAL;
698 if (test_level & G_LOG_FLAG_RECURSION)
699 log_func = _g_log_fallback_handler;
701 log_func = g_log_domain_get_handler_L (domain, test_level, &data);
703 g_mutex_unlock (&g_messages_lock);
705 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
708 if (test_level & G_LOG_FLAG_RECURSION)
710 /* we use a stack buffer of fixed size, since we're likely
711 * in an out-of-memory situation
714 gsize size G_GNUC_UNUSED;
717 G_VA_COPY (args2, args1);
718 size = _g_vsnprintf (buffer, 1024, format, args2);
721 log_func (log_domain, test_level, buffer, data);
728 G_VA_COPY (args2, args1);
729 msg = g_strdup_vprintf (format, args2);
732 log_func (log_domain, test_level, msg, data);
734 if ((test_level & G_LOG_FLAG_FATAL)
735 && !(test_level & G_LOG_LEVEL_ERROR))
737 masquerade_fatal = fatal_log_func
738 && !fatal_log_func (log_domain, test_level, msg, fatal_log_data);
744 if ((test_level & G_LOG_FLAG_FATAL) && !masquerade_fatal)
747 gchar *locale_msg = g_locale_from_utf8 (fatal_msg_buf, -1, NULL, NULL, NULL);
749 MessageBox (NULL, locale_msg, NULL,
750 MB_ICONERROR|MB_SETFOREGROUND);
751 if (IsDebuggerPresent () && !(test_level & G_LOG_FLAG_RECURSION))
756 if (!(test_level & G_LOG_FLAG_RECURSION))
760 #endif /* !G_OS_WIN32 */
764 g_private_set (&g_log_depth, GUINT_TO_POINTER (depth));
771 * @log_domain: the log domain, usually #G_LOG_DOMAIN
772 * @log_level: the log level, either from #GLogLevelFlags
773 * or a user-defined level
774 * @format: the message format. See the printf() documentation
775 * @...: the parameters to insert into the format string
777 * Logs an error or debugging message.
779 * If the log level has been set as fatal, the abort()
780 * function is called to terminate the program.
783 g_log (const gchar *log_domain,
784 GLogLevelFlags log_level,
790 va_start (args, format);
791 g_logv (log_domain, log_level, format, args);
796 g_return_if_fail_warning (const char *log_domain,
797 const char *pretty_function,
798 const char *expression)
801 G_LOG_LEVEL_CRITICAL,
802 "%s: assertion `%s' failed",
808 g_warn_message (const char *domain,
812 const char *warnexpr)
815 g_snprintf (lstr, 32, "%d", line);
817 s = g_strconcat ("(", file, ":", lstr, "):",
818 func, func[0] ? ":" : "",
819 " runtime check failed: (", warnexpr, ")", NULL);
821 s = g_strconcat ("(", file, ":", lstr, "):",
822 func, func[0] ? ":" : "",
823 " ", "code should not be reached", NULL);
824 g_log (domain, G_LOG_LEVEL_WARNING, "%s", s);
829 g_assert_warning (const char *log_domain,
832 const char *pretty_function,
833 const char *expression)
838 ? "file %s: line %d (%s): assertion failed: (%s)"
839 : "file %s: line %d (%s): should not be reached",
847 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
849 (wc >= 0x80 && wc < 0xa0)))
852 strdup_convert (const gchar *string,
853 const gchar *charset)
855 if (!g_utf8_validate (string, -1, NULL))
857 GString *gstring = g_string_new ("[Invalid UTF-8] ");
860 for (p = (guchar *)string; *p; p++)
862 if (CHAR_IS_SAFE(*p) &&
863 !(*p == '\r' && *(p + 1) != '\n') &&
865 g_string_append_c (gstring, *p);
867 g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
870 return g_string_free (gstring, FALSE);
876 gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
881 /* Not thread-safe, but doesn't matter if we print the warning twice
883 static gboolean warned = FALSE;
887 _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
891 return g_strdup (string);
896 /* For a radix of 8 we need at most 3 output bytes for 1 input
897 * byte. Additionally we might need up to 2 output bytes for the
898 * readix prefix and 1 byte for the trailing NULL.
900 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
903 format_unsigned (gchar *buf,
911 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
913 if (radix != 8 && radix != 10 && radix != 16)
946 /* Again we can't use g_assert; actually this check should _never_ fail. */
947 if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
960 buf[i] = c + 'a' - 10;
967 /* string size big enough to hold level prefix */
968 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
970 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
973 mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
974 GLogLevelFlags log_level)
976 gboolean to_stdout = TRUE;
978 /* we may not call _any_ GLib functions here */
980 switch (log_level & G_LOG_LEVEL_MASK)
982 case G_LOG_LEVEL_ERROR:
983 strcpy (level_prefix, "ERROR");
986 case G_LOG_LEVEL_CRITICAL:
987 strcpy (level_prefix, "CRITICAL");
990 case G_LOG_LEVEL_WARNING:
991 strcpy (level_prefix, "WARNING");
994 case G_LOG_LEVEL_MESSAGE:
995 strcpy (level_prefix, "Message");
998 case G_LOG_LEVEL_INFO:
999 strcpy (level_prefix, "INFO");
1001 case G_LOG_LEVEL_DEBUG:
1002 strcpy (level_prefix, "DEBUG");
1007 strcpy (level_prefix, "LOG-");
1008 format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
1011 strcpy (level_prefix, "LOG");
1014 if (log_level & G_LOG_FLAG_RECURSION)
1015 strcat (level_prefix, " (recursed)");
1016 if (log_level & ALERT_LEVELS)
1017 strcat (level_prefix, " **");
1020 win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
1022 return to_stdout ? 1 : 2;
1026 _g_log_fallback_handler (const gchar *log_domain,
1027 GLogLevelFlags log_level,
1028 const gchar *message,
1029 gpointer unused_data)
1031 gchar level_prefix[STRING_BUFFER_SIZE];
1033 gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
1037 /* we cannot call _any_ GLib functions in this fallback handler,
1038 * which is why we skip UTF-8 conversion, etc.
1039 * since we either recursed or ran out of memory, we're in a pretty
1040 * pathologic situation anyways, what we can do is giving the
1041 * the process ID unconditionally however.
1044 fd = mklevel_prefix (level_prefix, log_level);
1046 message = "(NULL) message";
1049 format_unsigned (pid_string, getpid (), 10);
1053 write_string (fd, "\n");
1055 write_string (fd, "\n** ");
1058 write_string (fd, "(process:");
1059 write_string (fd, pid_string);
1060 write_string (fd, "): ");
1065 write_string (fd, log_domain);
1066 write_string (fd, "-");
1068 write_string (fd, level_prefix);
1069 write_string (fd, ": ");
1070 write_string (fd, message);
1074 escape_string (GString *string)
1076 const char *p = string->str;
1079 while (p < string->str + string->len)
1083 wc = g_utf8_get_char_validated (p, -1);
1084 if (wc == (gunichar)-1 || wc == (gunichar)-2)
1089 pos = p - string->str;
1091 /* Emit invalid UTF-8 as hex escapes
1093 tmp = g_strdup_printf ("\\x%02x", (guint)(guchar)*p);
1094 g_string_erase (string, pos, 1);
1095 g_string_insert (string, pos, tmp);
1097 p = string->str + (pos + 4); /* Skip over escape sequence */
1104 safe = *(p + 1) == '\n';
1108 safe = CHAR_IS_SAFE (wc);
1116 pos = p - string->str;
1118 /* Largest char we escape is 0x0a, so we don't have to worry
1119 * about 8-digit \Uxxxxyyyy
1121 tmp = g_strdup_printf ("\\u%04x", wc);
1122 g_string_erase (string, pos, g_utf8_next_char (p) - p);
1123 g_string_insert (string, pos, tmp);
1126 p = string->str + (pos + 6); /* Skip over escape sequence */
1129 p = g_utf8_next_char (p);
1134 * g_log_default_handler:
1135 * @log_domain: the log domain of the message
1136 * @log_level: the level of the message
1137 * @message: the message
1138 * @unused_data: data passed from g_log() which is unused
1140 * The default log handler set up by GLib; g_log_set_default_handler()
1141 * allows to install an alternate default log handler.
1142 * This is used if no log handler has been set for the particular log
1143 * domain and log level combination. It outputs the message to stderr
1144 * or stdout and if the log level is fatal it calls abort().
1146 * stderr is used for levels %G_LOG_LEVEL_ERROR, %G_LOG_LEVEL_CRITICAL,
1147 * %G_LOG_LEVEL_WARNING and %G_LOG_LEVEL_MESSAGE. stdout is used for
1151 g_log_default_handler (const gchar *log_domain,
1152 GLogLevelFlags log_level,
1153 const gchar *message,
1154 gpointer unused_data)
1156 gchar level_prefix[STRING_BUFFER_SIZE], *string;
1160 /* we can be called externally with recursion for whatever reason */
1161 if (log_level & G_LOG_FLAG_RECURSION)
1163 _g_log_fallback_handler (log_domain, log_level, message, unused_data);
1167 fd = mklevel_prefix (level_prefix, log_level);
1169 gstring = g_string_new (NULL);
1170 if (log_level & ALERT_LEVELS)
1171 g_string_append (gstring, "\n");
1173 g_string_append (gstring, "** ");
1175 if ((g_log_msg_prefix & log_level) == log_level)
1177 const gchar *prg_name = g_get_prgname ();
1180 g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
1182 g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
1187 g_string_append (gstring, log_domain);
1188 g_string_append_c (gstring, '-');
1190 g_string_append (gstring, level_prefix);
1192 g_string_append (gstring, ": ");
1194 g_string_append (gstring, "(NULL) message");
1198 const gchar *charset;
1200 msg = g_string_new (message);
1201 escape_string (msg);
1203 if (g_get_charset (&charset))
1204 g_string_append (gstring, msg->str); /* charset is UTF-8 already */
1207 string = strdup_convert (msg->str, charset);
1208 g_string_append (gstring, string);
1212 g_string_free (msg, TRUE);
1214 g_string_append (gstring, "\n");
1216 string = g_string_free (gstring, FALSE);
1218 write_string (fd, string);
1223 * g_set_print_handler:
1224 * @func: the new print handler
1226 * Sets the print handler.
1228 * Any messages passed to g_print() will be output via
1229 * the new handler. The default handler simply outputs
1230 * the message to stdout. By providing your own handler
1231 * you can redirect the output, to a GTK+ widget or a
1232 * log file for example.
1234 * Returns: the old print handler
1237 g_set_print_handler (GPrintFunc func)
1239 GPrintFunc old_print_func;
1241 g_mutex_lock (&g_messages_lock);
1242 old_print_func = glib_print_func;
1243 glib_print_func = func;
1244 g_mutex_unlock (&g_messages_lock);
1246 return old_print_func;
1251 * @format: the message format. See the printf() documentation
1252 * @...: the parameters to insert into the format string
1254 * Outputs a formatted message via the print handler.
1255 * The default print handler simply outputs the message to stdout.
1257 * g_print() should not be used from within libraries for debugging
1258 * messages, since it may be redirected by applications to special
1259 * purpose message windows or even files. Instead, libraries should
1260 * use g_log(), or the convenience functions g_message(), g_warning()
1264 g_print (const gchar *format,
1269 GPrintFunc local_glib_print_func;
1271 g_return_if_fail (format != NULL);
1273 va_start (args, format);
1274 string = g_strdup_vprintf (format, args);
1277 g_mutex_lock (&g_messages_lock);
1278 local_glib_print_func = glib_print_func;
1279 g_mutex_unlock (&g_messages_lock);
1281 if (local_glib_print_func)
1282 local_glib_print_func (string);
1285 const gchar *charset;
1287 if (g_get_charset (&charset))
1288 fputs (string, stdout); /* charset is UTF-8 already */
1291 gchar *lstring = strdup_convert (string, charset);
1293 fputs (lstring, stdout);
1302 * g_set_printerr_handler:
1303 * @func: the new error message handler
1305 * Sets the handler for printing error messages.
1307 * Any messages passed to g_printerr() will be output via
1308 * the new handler. The default handler simply outputs the
1309 * message to stderr. By providing your own handler you can
1310 * redirect the output, to a GTK+ widget or a log file for
1313 * Returns: the old error message handler
1316 g_set_printerr_handler (GPrintFunc func)
1318 GPrintFunc old_printerr_func;
1320 g_mutex_lock (&g_messages_lock);
1321 old_printerr_func = glib_printerr_func;
1322 glib_printerr_func = func;
1323 g_mutex_unlock (&g_messages_lock);
1325 return old_printerr_func;
1330 * @format: the message format. See the printf() documentation
1331 * @...: the parameters to insert into the format string
1333 * Outputs a formatted message via the error message handler.
1334 * The default handler simply outputs the message to stderr.
1336 * g_printerr() should not be used from within libraries.
1337 * Instead g_log() should be used, or the convenience functions
1338 * g_message(), g_warning() and g_error().
1341 g_printerr (const gchar *format,
1346 GPrintFunc local_glib_printerr_func;
1348 g_return_if_fail (format != NULL);
1350 va_start (args, format);
1351 string = g_strdup_vprintf (format, args);
1354 g_mutex_lock (&g_messages_lock);
1355 local_glib_printerr_func = glib_printerr_func;
1356 g_mutex_unlock (&g_messages_lock);
1358 if (local_glib_printerr_func)
1359 local_glib_printerr_func (string);
1362 const gchar *charset;
1364 if (g_get_charset (&charset))
1365 fputs (string, stderr); /* charset is UTF-8 already */
1368 gchar *lstring = strdup_convert (string, charset);
1370 fputs (lstring, stderr);
1379 * g_printf_string_upper_bound:
1380 * @format: the format string. See the printf() documentation
1381 * @args: the parameters to be inserted into the format string
1383 * Calculates the maximum space needed to store the output
1384 * of the sprintf() function.
1386 * Returns: the maximum space needed to store the formatted string
1389 g_printf_string_upper_bound (const gchar *format,
1393 return _g_vsnprintf (&c, 1, format, args) + 1;