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 "gbacktrace.h"
68 #include "gprintfint.h"
69 #include "gtestutils.h"
71 #include "gthreadprivate.h"
72 #include "gstrfuncs.h"
76 #include <process.h> /* For getpid() */
78 # define STRICT /* Strict typing, please */
79 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
85 /* --- structures --- */
86 typedef struct _GLogDomain GLogDomain
;
87 typedef struct _GLogHandler GLogHandler
;
91 GLogLevelFlags fatal_mask
;
92 GLogHandler
*handlers
;
98 GLogLevelFlags log_level
;
105 /* --- variables --- */
106 static GMutex
*g_messages_lock
= NULL
;
107 static GLogDomain
*g_log_domains
= NULL
;
108 static GLogLevelFlags g_log_always_fatal
= G_LOG_FATAL_MASK
;
109 static GPrintFunc glib_print_func
= NULL
;
110 static GPrintFunc glib_printerr_func
= NULL
;
111 static GPrivate
*g_log_depth
= NULL
;
112 static GLogLevelFlags g_log_msg_prefix
= G_LOG_LEVEL_ERROR
| G_LOG_LEVEL_WARNING
| G_LOG_LEVEL_CRITICAL
| G_LOG_LEVEL_DEBUG
;
113 static GLogFunc default_log_func
= g_log_default_handler
;
114 static gpointer default_log_data
= NULL
;
115 static GTestLogFatalFunc fatal_log_func
= NULL
;
116 static gpointer fatal_log_data
;
118 /* --- functions --- */
121 # include <windows.h>
123 static gboolean win32_keep_fatal_message
= FALSE
;
125 /* This default message will usually be overwritten. */
126 /* Yes, a fixed size buffer is bad. So sue me. But g_error() is never
127 * called with huge strings, is it?
129 static gchar fatal_msg_buf
[1000] = "Unspecified fatal error encountered, aborting.";
130 static gchar
*fatal_msg_ptr
= fatal_msg_buf
;
138 if (win32_keep_fatal_message
)
140 memcpy (fatal_msg_ptr
, buf
, len
);
141 fatal_msg_ptr
+= len
;
146 write (fd
, buf
, len
);
150 #define write(fd, buf, len) dowrite(fd, buf, len)
155 write_string (int fd
,
158 write (fd
, string
, strlen (string
));
162 g_messages_prefixed_init (void)
164 static gboolean initialized
= FALSE
;
171 val
= g_getenv ("G_MESSAGES_PREFIXED");
175 const GDebugKey keys
[] = {
176 { "error", G_LOG_LEVEL_ERROR
},
177 { "critical", G_LOG_LEVEL_CRITICAL
},
178 { "warning", G_LOG_LEVEL_WARNING
},
179 { "message", G_LOG_LEVEL_MESSAGE
},
180 { "info", G_LOG_LEVEL_INFO
},
181 { "debug", G_LOG_LEVEL_DEBUG
}
184 g_log_msg_prefix
= g_parse_debug_string (val
, keys
, G_N_ELEMENTS (keys
));
190 g_log_find_domain_L (const gchar
*log_domain
)
192 register GLogDomain
*domain
;
194 domain
= g_log_domains
;
197 if (strcmp (domain
->log_domain
, log_domain
) == 0)
199 domain
= domain
->next
;
205 g_log_domain_new_L (const gchar
*log_domain
)
207 register GLogDomain
*domain
;
209 domain
= g_new (GLogDomain
, 1);
210 domain
->log_domain
= g_strdup (log_domain
);
211 domain
->fatal_mask
= G_LOG_FATAL_MASK
;
212 domain
->handlers
= NULL
;
214 domain
->next
= g_log_domains
;
215 g_log_domains
= domain
;
221 g_log_domain_check_free_L (GLogDomain
*domain
)
223 if (domain
->fatal_mask
== G_LOG_FATAL_MASK
&&
224 domain
->handlers
== NULL
)
226 register GLogDomain
*last
, *work
;
230 work
= g_log_domains
;
236 last
->next
= domain
->next
;
238 g_log_domains
= domain
->next
;
239 g_free (domain
->log_domain
);
250 g_log_domain_get_handler_L (GLogDomain
*domain
,
251 GLogLevelFlags log_level
,
254 if (domain
&& log_level
)
256 register GLogHandler
*handler
;
258 handler
= domain
->handlers
;
261 if ((handler
->log_level
& log_level
) == log_level
)
263 *data
= handler
->data
;
264 return handler
->log_func
;
266 handler
= handler
->next
;
270 *data
= default_log_data
;
271 return default_log_func
;
275 g_log_set_always_fatal (GLogLevelFlags fatal_mask
)
277 GLogLevelFlags old_mask
;
279 /* restrict the global mask to levels that are known to glib
280 * since this setting applies to all domains
282 fatal_mask
&= (1 << G_LOG_LEVEL_USER_SHIFT
) - 1;
283 /* force errors to be fatal */
284 fatal_mask
|= G_LOG_LEVEL_ERROR
;
285 /* remove bogus flag */
286 fatal_mask
&= ~G_LOG_FLAG_FATAL
;
288 g_mutex_lock (g_messages_lock
);
289 old_mask
= g_log_always_fatal
;
290 g_log_always_fatal
= fatal_mask
;
291 g_mutex_unlock (g_messages_lock
);
297 g_log_set_fatal_mask (const gchar
*log_domain
,
298 GLogLevelFlags fatal_mask
)
300 GLogLevelFlags old_flags
;
301 register GLogDomain
*domain
;
306 /* force errors to be fatal */
307 fatal_mask
|= G_LOG_LEVEL_ERROR
;
308 /* remove bogus flag */
309 fatal_mask
&= ~G_LOG_FLAG_FATAL
;
311 g_mutex_lock (g_messages_lock
);
313 domain
= g_log_find_domain_L (log_domain
);
315 domain
= g_log_domain_new_L (log_domain
);
316 old_flags
= domain
->fatal_mask
;
318 domain
->fatal_mask
= fatal_mask
;
319 g_log_domain_check_free_L (domain
);
321 g_mutex_unlock (g_messages_lock
);
327 g_log_set_handler (const gchar
*log_domain
,
328 GLogLevelFlags log_levels
,
332 static guint handler_id
= 0;
334 GLogHandler
*handler
;
336 g_return_val_if_fail ((log_levels
& G_LOG_LEVEL_MASK
) != 0, 0);
337 g_return_val_if_fail (log_func
!= NULL
, 0);
342 handler
= g_new (GLogHandler
, 1);
344 g_mutex_lock (g_messages_lock
);
346 domain
= g_log_find_domain_L (log_domain
);
348 domain
= g_log_domain_new_L (log_domain
);
350 handler
->id
= ++handler_id
;
351 handler
->log_level
= log_levels
;
352 handler
->log_func
= log_func
;
353 handler
->data
= user_data
;
354 handler
->next
= domain
->handlers
;
355 domain
->handlers
= handler
;
357 g_mutex_unlock (g_messages_lock
);
363 g_log_set_default_handler (GLogFunc log_func
,
366 GLogFunc old_log_func
;
368 g_mutex_lock (g_messages_lock
);
369 old_log_func
= default_log_func
;
370 default_log_func
= log_func
;
371 default_log_data
= user_data
;
372 g_mutex_unlock (g_messages_lock
);
378 * g_test_log_set_fatal_handler:
379 * @log_func: the log handler function.
380 * @user_data: data passed to the log handler.
382 * Installs a non-error fatal log handler which can be
383 * used to decide whether log messages which are counted
384 * as fatal abort the program.
386 * The use case here is that you are running a test case
387 * that depends on particular libraries or circumstances
388 * and cannot prevent certain known critical or warning
389 * messages. So you install a handler that compares the
390 * domain and message to precisely not abort in such a case.
392 * Note that the handler is reset at the beginning of
393 * any test case, so you have to set it inside each test
394 * function which needs the special behavior.
396 * This handler has no effect on g_error messages.
401 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func
,
404 g_mutex_lock (g_messages_lock
);
405 fatal_log_func
= log_func
;
406 fatal_log_data
= user_data
;
407 g_mutex_unlock (g_messages_lock
);
411 g_log_remove_handler (const gchar
*log_domain
,
414 register GLogDomain
*domain
;
416 g_return_if_fail (handler_id
> 0);
421 g_mutex_lock (g_messages_lock
);
422 domain
= g_log_find_domain_L (log_domain
);
425 GLogHandler
*work
, *last
;
428 work
= domain
->handlers
;
431 if (work
->id
== handler_id
)
434 last
->next
= work
->next
;
436 domain
->handlers
= work
->next
;
437 g_log_domain_check_free_L (domain
);
438 g_mutex_unlock (g_messages_lock
);
446 g_mutex_unlock (g_messages_lock
);
447 g_warning ("%s: could not find handler with id `%d' for domain \"%s\"",
448 G_STRLOC
, handler_id
, log_domain
);
452 g_logv (const gchar
*log_domain
,
453 GLogLevelFlags log_level
,
457 gboolean was_fatal
= (log_level
& G_LOG_FLAG_FATAL
) != 0;
458 gboolean was_recursion
= (log_level
& G_LOG_FLAG_RECURSION
) != 0;
461 log_level
&= G_LOG_LEVEL_MASK
;
465 for (i
= g_bit_nth_msf (log_level
, -1); i
>= 0; i
= g_bit_nth_msf (log_level
, i
))
467 register GLogLevelFlags test_level
;
470 if (log_level
& test_level
)
472 guint depth
= GPOINTER_TO_UINT (g_private_get (g_log_depth
));
475 GLogLevelFlags domain_fatal_mask
;
476 gpointer data
= NULL
;
477 gboolean masquerade_fatal
= FALSE
;
480 test_level
|= G_LOG_FLAG_FATAL
;
482 test_level
|= G_LOG_FLAG_RECURSION
;
484 /* check recursion and lookup handler */
485 g_mutex_lock (g_messages_lock
);
486 domain
= g_log_find_domain_L (log_domain
? log_domain
: "");
488 test_level
|= G_LOG_FLAG_RECURSION
;
490 domain_fatal_mask
= domain
? domain
->fatal_mask
: G_LOG_FATAL_MASK
;
491 if ((domain_fatal_mask
| g_log_always_fatal
) & test_level
)
492 test_level
|= G_LOG_FLAG_FATAL
;
493 if (test_level
& G_LOG_FLAG_RECURSION
)
494 log_func
= _g_log_fallback_handler
;
496 log_func
= g_log_domain_get_handler_L (domain
, test_level
, &data
);
498 g_mutex_unlock (g_messages_lock
);
500 g_private_set (g_log_depth
, GUINT_TO_POINTER (depth
));
502 /* had to defer debug initialization until we can keep track of recursion */
503 if (!(test_level
& G_LOG_FLAG_RECURSION
) && !_g_debug_initialized
)
505 GLogLevelFlags orig_test_level
= test_level
;
508 if ((domain_fatal_mask
| g_log_always_fatal
) & test_level
)
509 test_level
|= G_LOG_FLAG_FATAL
;
510 if (test_level
!= orig_test_level
)
512 /* need a relookup, not nice, but not too bad either */
513 g_mutex_lock (g_messages_lock
);
514 domain
= g_log_find_domain_L (log_domain
? log_domain
: "");
515 log_func
= g_log_domain_get_handler_L (domain
, test_level
, &data
);
517 g_mutex_unlock (g_messages_lock
);
521 if (test_level
& G_LOG_FLAG_RECURSION
)
523 /* we use a stack buffer of fixed size, since we're likely
524 * in an out-of-memory situation
527 gsize size G_GNUC_UNUSED
;
530 G_VA_COPY (args2
, args1
);
531 size
= _g_vsnprintf (buffer
, 1024, format
, args2
);
534 log_func (log_domain
, test_level
, buffer
, data
);
541 G_VA_COPY (args2
, args1
);
542 msg
= g_strdup_vprintf (format
, args2
);
545 log_func (log_domain
, test_level
, msg
, data
);
547 if ((test_level
& G_LOG_FLAG_FATAL
)
548 && !(test_level
& G_LOG_LEVEL_ERROR
))
550 masquerade_fatal
= fatal_log_func
551 && !fatal_log_func (log_domain
, test_level
, msg
, data
);
557 if ((test_level
& G_LOG_FLAG_FATAL
) && !masquerade_fatal
)
560 gchar
*locale_msg
= g_locale_from_utf8 (fatal_msg_buf
, -1, NULL
, NULL
, NULL
);
562 MessageBox (NULL
, locale_msg
, NULL
,
563 MB_ICONERROR
|MB_SETFOREGROUND
);
564 if (IsDebuggerPresent () && !(test_level
& G_LOG_FLAG_RECURSION
))
569 if (!(test_level
& G_LOG_FLAG_RECURSION
))
573 #endif /* !G_OS_WIN32 */
577 g_private_set (g_log_depth
, GUINT_TO_POINTER (depth
));
583 g_log (const gchar
*log_domain
,
584 GLogLevelFlags log_level
,
590 va_start (args
, format
);
591 g_logv (log_domain
, log_level
, format
, args
);
596 g_return_if_fail_warning (const char *log_domain
,
597 const char *pretty_function
,
598 const char *expression
)
601 G_LOG_LEVEL_CRITICAL
,
602 "%s: assertion `%s' failed",
608 g_warn_message (const char *domain
,
612 const char *warnexpr
)
615 g_snprintf (lstr
, 32, "%d", line
);
617 s
= g_strconcat ("(", file
, ":", lstr
, "):",
618 func
, func
[0] ? ":" : "",
619 " runtime check failed: (", warnexpr
, ")", NULL
);
621 s
= g_strconcat ("(", file
, ":", lstr
, "):",
622 func
, func
[0] ? ":" : "",
623 " ", "code should not be reached", NULL
);
624 g_log (domain
, G_LOG_LEVEL_WARNING
, "%s", s
);
629 g_assert_warning (const char *log_domain
,
632 const char *pretty_function
,
633 const char *expression
)
638 ? "file %s: line %d (%s): assertion failed: (%s)"
639 : "file %s: line %d (%s): should not be reached",
647 #define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
649 (wc >= 0x80 && wc < 0xa0)))
652 strdup_convert (const gchar
*string
,
653 const gchar
*charset
)
655 if (!g_utf8_validate (string
, -1, NULL
))
657 GString
*gstring
= g_string_new ("[Invalid UTF-8] ");
660 for (p
= (guchar
*)string
; *p
; p
++)
662 if (CHAR_IS_SAFE(*p
) &&
663 !(*p
== '\r' && *(p
+ 1) != '\n') &&
665 g_string_append_c (gstring
, *p
);
667 g_string_append_printf (gstring
, "\\x%02x", (guint
)(guchar
)*p
);
670 return g_string_free (gstring
, FALSE
);
676 gchar
*result
= g_convert_with_fallback (string
, -1, charset
, "UTF-8", "?", NULL
, NULL
, &err
);
681 /* Not thread-safe, but doesn't matter if we print the warning twice
683 static gboolean warned
= FALSE
;
687 _g_fprintf (stderr
, "GLib: Cannot convert message: %s\n", err
->message
);
691 return g_strdup (string
);
696 /* For a radix of 8 we need at most 3 output bytes for 1 input
697 * byte. Additionally we might need up to 2 output bytes for the
698 * readix prefix and 1 byte for the trailing NULL.
700 #define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
703 format_unsigned (gchar
*buf
,
711 /* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
713 if (radix
!= 8 && radix
!= 10 && radix
!= 16)
746 /* Again we can't use g_assert; actually this check should _never_ fail. */
747 if (n
> FORMAT_UNSIGNED_BUFSIZE
- 3)
760 buf
[i
] = c
+ 'a' - 10;
767 /* string size big enough to hold level prefix */
768 #define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
770 #define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
773 mklevel_prefix (gchar level_prefix
[STRING_BUFFER_SIZE
],
774 GLogLevelFlags log_level
)
776 gboolean to_stdout
= TRUE
;
778 /* we may not call _any_ GLib functions here */
780 switch (log_level
& G_LOG_LEVEL_MASK
)
782 case G_LOG_LEVEL_ERROR
:
783 strcpy (level_prefix
, "ERROR");
786 case G_LOG_LEVEL_CRITICAL
:
787 strcpy (level_prefix
, "CRITICAL");
790 case G_LOG_LEVEL_WARNING
:
791 strcpy (level_prefix
, "WARNING");
794 case G_LOG_LEVEL_MESSAGE
:
795 strcpy (level_prefix
, "Message");
798 case G_LOG_LEVEL_INFO
:
799 strcpy (level_prefix
, "INFO");
801 case G_LOG_LEVEL_DEBUG
:
802 strcpy (level_prefix
, "DEBUG");
807 strcpy (level_prefix
, "LOG-");
808 format_unsigned (level_prefix
+ 4, log_level
& G_LOG_LEVEL_MASK
, 16);
811 strcpy (level_prefix
, "LOG");
814 if (log_level
& G_LOG_FLAG_RECURSION
)
815 strcat (level_prefix
, " (recursed)");
816 if (log_level
& ALERT_LEVELS
)
817 strcat (level_prefix
, " **");
820 win32_keep_fatal_message
= (log_level
& G_LOG_FLAG_FATAL
) != 0;
822 return to_stdout
? 1 : 2;
826 _g_log_fallback_handler (const gchar
*log_domain
,
827 GLogLevelFlags log_level
,
828 const gchar
*message
,
829 gpointer unused_data
)
831 gchar level_prefix
[STRING_BUFFER_SIZE
];
833 gchar pid_string
[FORMAT_UNSIGNED_BUFSIZE
];
837 /* we cannot call _any_ GLib functions in this fallback handler,
838 * which is why we skip UTF-8 conversion, etc.
839 * since we either recursed or ran out of memory, we're in a pretty
840 * pathologic situation anyways, what we can do is giving the
841 * the process ID unconditionally however.
844 fd
= mklevel_prefix (level_prefix
, log_level
);
846 message
= "(NULL) message";
849 format_unsigned (pid_string
, getpid (), 10);
853 write_string (fd
, "\n");
855 write_string (fd
, "\n** ");
858 write_string (fd
, "(process:");
859 write_string (fd
, pid_string
);
860 write_string (fd
, "): ");
865 write_string (fd
, log_domain
);
866 write_string (fd
, "-");
868 write_string (fd
, level_prefix
);
869 write_string (fd
, ": ");
870 write_string (fd
, message
);
874 escape_string (GString
*string
)
876 const char *p
= string
->str
;
879 while (p
< string
->str
+ string
->len
)
883 wc
= g_utf8_get_char_validated (p
, -1);
884 if (wc
== (gunichar
)-1 || wc
== (gunichar
)-2)
889 pos
= p
- string
->str
;
891 /* Emit invalid UTF-8 as hex escapes
893 tmp
= g_strdup_printf ("\\x%02x", (guint
)(guchar
)*p
);
894 g_string_erase (string
, pos
, 1);
895 g_string_insert (string
, pos
, tmp
);
897 p
= string
->str
+ (pos
+ 4); /* Skip over escape sequence */
904 safe
= *(p
+ 1) == '\n';
908 safe
= CHAR_IS_SAFE (wc
);
916 pos
= p
- string
->str
;
918 /* Largest char we escape is 0x0a, so we don't have to worry
919 * about 8-digit \Uxxxxyyyy
921 tmp
= g_strdup_printf ("\\u%04x", wc
);
922 g_string_erase (string
, pos
, g_utf8_next_char (p
) - p
);
923 g_string_insert (string
, pos
, tmp
);
926 p
= string
->str
+ (pos
+ 6); /* Skip over escape sequence */
929 p
= g_utf8_next_char (p
);
934 g_log_default_handler (const gchar
*log_domain
,
935 GLogLevelFlags log_level
,
936 const gchar
*message
,
937 gpointer unused_data
)
939 gchar level_prefix
[STRING_BUFFER_SIZE
], *string
;
943 /* we can be called externally with recursion for whatever reason */
944 if (log_level
& G_LOG_FLAG_RECURSION
)
946 _g_log_fallback_handler (log_domain
, log_level
, message
, unused_data
);
950 g_messages_prefixed_init ();
952 fd
= mklevel_prefix (level_prefix
, log_level
);
954 gstring
= g_string_new (NULL
);
955 if (log_level
& ALERT_LEVELS
)
956 g_string_append (gstring
, "\n");
958 g_string_append (gstring
, "** ");
960 if ((g_log_msg_prefix
& log_level
) == log_level
)
962 const gchar
*prg_name
= g_get_prgname ();
965 g_string_append_printf (gstring
, "(process:%lu): ", (gulong
)getpid ());
967 g_string_append_printf (gstring
, "(%s:%lu): ", prg_name
, (gulong
)getpid ());
972 g_string_append (gstring
, log_domain
);
973 g_string_append_c (gstring
, '-');
975 g_string_append (gstring
, level_prefix
);
977 g_string_append (gstring
, ": ");
979 g_string_append (gstring
, "(NULL) message");
983 const gchar
*charset
;
985 msg
= g_string_new (message
);
988 if (g_get_charset (&charset
))
989 g_string_append (gstring
, msg
->str
); /* charset is UTF-8 already */
992 string
= strdup_convert (msg
->str
, charset
);
993 g_string_append (gstring
, string
);
997 g_string_free (msg
, TRUE
);
999 g_string_append (gstring
, "\n");
1001 string
= g_string_free (gstring
, FALSE
);
1003 write_string (fd
, string
);
1008 * g_set_print_handler:
1009 * @func: the new print handler
1011 * Sets the print handler.
1013 * Any messages passed to g_print() will be output via
1014 * the new handler. The default handler simply outputs
1015 * the message to stdout. By providing your own handler
1016 * you can redirect the output, to a GTK+ widget or a
1017 * log file for example.
1019 * Returns: the old print handler
1022 g_set_print_handler (GPrintFunc func
)
1024 GPrintFunc old_print_func
;
1026 g_mutex_lock (g_messages_lock
);
1027 old_print_func
= glib_print_func
;
1028 glib_print_func
= func
;
1029 g_mutex_unlock (g_messages_lock
);
1031 return old_print_func
;
1036 * @format: the message format. See the printf() documentation
1037 * @...: the parameters to insert into the format string
1039 * Outputs a formatted message via the print handler.
1040 * The default print handler simply outputs the message to stdout.
1042 * g_print() should not be used from within libraries for debugging
1043 * messages, since it may be redirected by applications to special
1044 * purpose message windows or even files. Instead, libraries should
1045 * use g_log(), or the convenience functions g_message(), g_warning()
1049 g_print (const gchar
*format
,
1054 GPrintFunc local_glib_print_func
;
1056 g_return_if_fail (format
!= NULL
);
1058 va_start (args
, format
);
1059 string
= g_strdup_vprintf (format
, args
);
1062 g_mutex_lock (g_messages_lock
);
1063 local_glib_print_func
= glib_print_func
;
1064 g_mutex_unlock (g_messages_lock
);
1066 if (local_glib_print_func
)
1067 local_glib_print_func (string
);
1070 const gchar
*charset
;
1072 if (g_get_charset (&charset
))
1073 fputs (string
, stdout
); /* charset is UTF-8 already */
1076 gchar
*lstring
= strdup_convert (string
, charset
);
1078 fputs (lstring
, stdout
);
1087 * g_set_printerr_handler:
1088 * @func: the new error message handler
1090 * Sets the handler for printing error messages.
1092 * Any messages passed to g_printerr() will be output via
1093 * the new handler. The default handler simply outputs the
1094 * message to stderr. By providing your own handler you can
1095 * redirect the output, to a GTK+ widget or a log file for
1098 * Returns: the old error message handler
1101 g_set_printerr_handler (GPrintFunc func
)
1103 GPrintFunc old_printerr_func
;
1105 g_mutex_lock (g_messages_lock
);
1106 old_printerr_func
= glib_printerr_func
;
1107 glib_printerr_func
= func
;
1108 g_mutex_unlock (g_messages_lock
);
1110 return old_printerr_func
;
1115 * @format: the message format. See the printf() documentation
1116 * @...: the parameters to insert into the format string
1118 * Outputs a formatted message via the error message handler.
1119 * The default handler simply outputs the message to stderr.
1121 * g_printerr() should not be used from within libraries.
1122 * Instead g_log() should be used, or the convenience functions
1123 * g_message(), g_warning() and g_error().
1126 g_printerr (const gchar
*format
,
1131 GPrintFunc local_glib_printerr_func
;
1133 g_return_if_fail (format
!= NULL
);
1135 va_start (args
, format
);
1136 string
= g_strdup_vprintf (format
, args
);
1139 g_mutex_lock (g_messages_lock
);
1140 local_glib_printerr_func
= glib_printerr_func
;
1141 g_mutex_unlock (g_messages_lock
);
1143 if (local_glib_printerr_func
)
1144 local_glib_printerr_func (string
);
1147 const gchar
*charset
;
1149 if (g_get_charset (&charset
))
1150 fputs (string
, stderr
); /* charset is UTF-8 already */
1153 gchar
*lstring
= strdup_convert (string
, charset
);
1155 fputs (lstring
, stderr
);
1164 g_printf_string_upper_bound (const gchar
*format
,
1168 return _g_vsnprintf (&c
, 1, format
, args
) + 1;
1172 _g_messages_thread_init_nomessage (void)
1174 g_messages_lock
= g_mutex_new ();
1175 g_log_depth
= g_private_new (NULL
);
1176 g_messages_prefixed_init ();
1180 gboolean _g_debug_initialized
= FALSE
;
1181 guint _g_debug_flags
= 0;
1184 _g_debug_init (void)
1188 _g_debug_initialized
= TRUE
;
1190 val
= g_getenv ("G_DEBUG");
1193 const GDebugKey keys
[] = {
1194 {"fatal_warnings", G_DEBUG_FATAL_WARNINGS
},
1195 {"fatal_criticals", G_DEBUG_FATAL_CRITICALS
}
1198 _g_debug_flags
= g_parse_debug_string (val
, keys
, G_N_ELEMENTS (keys
));
1201 if (_g_debug_flags
& G_DEBUG_FATAL_WARNINGS
)
1203 GLogLevelFlags fatal_mask
;
1205 fatal_mask
= g_log_set_always_fatal (G_LOG_FATAL_MASK
);
1206 fatal_mask
|= G_LOG_LEVEL_WARNING
| G_LOG_LEVEL_CRITICAL
;
1207 g_log_set_always_fatal (fatal_mask
);
1210 if (_g_debug_flags
& G_DEBUG_FATAL_CRITICALS
)
1212 GLogLevelFlags fatal_mask
;
1214 fatal_mask
= g_log_set_always_fatal (G_LOG_FATAL_MASK
);
1215 fatal_mask
|= G_LOG_LEVEL_CRITICAL
;
1216 g_log_set_always_fatal (fatal_mask
);