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, fatal_log_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)))
651
/* For a radix of 8 we need at most 3 output bytes for 1 input
652
* byte. Additionally we might need up to 2 output bytes for the
653
* readix prefix and 1 byte for the trailing NULL.
655
#define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
658
format_unsigned (gchar *buf,
666
/* we may not call _any_ GLib functions here (or macros like g_return_if_fail()) */
668
if (radix != 8 && radix != 10 && radix != 16)
701
/* Again we can't use g_assert; actually this check should _never_ fail. */
702
if (n > FORMAT_UNSIGNED_BUFSIZE - 3)
715
buf[i] = c + 'a' - 10;
722
/* string size big enough to hold level prefix */
723
#define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
725
#define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
728
mklevel_prefix (gchar level_prefix[STRING_BUFFER_SIZE],
729
GLogLevelFlags log_level)
731
gboolean to_stdout = TRUE;
733
/* we may not call _any_ GLib functions here */
735
switch (log_level & G_LOG_LEVEL_MASK)
737
case G_LOG_LEVEL_ERROR:
738
strcpy (level_prefix, "ERROR");
741
case G_LOG_LEVEL_CRITICAL:
742
strcpy (level_prefix, "CRITICAL");
745
case G_LOG_LEVEL_WARNING:
746
strcpy (level_prefix, "WARNING");
749
case G_LOG_LEVEL_MESSAGE:
750
strcpy (level_prefix, "Message");
753
case G_LOG_LEVEL_INFO:
754
strcpy (level_prefix, "INFO");
756
case G_LOG_LEVEL_DEBUG:
757
strcpy (level_prefix, "DEBUG");
762
strcpy (level_prefix, "LOG-");
763
format_unsigned (level_prefix + 4, log_level & G_LOG_LEVEL_MASK, 16);
766
strcpy (level_prefix, "LOG");
769
if (log_level & G_LOG_FLAG_RECURSION)
770
strcat (level_prefix, " (recursed)");
771
if (log_level & ALERT_LEVELS)
772
strcat (level_prefix, " **");
775
win32_keep_fatal_message = (log_level & G_LOG_FLAG_FATAL) != 0;
777
return to_stdout ? 1 : 2;
781
_g_log_fallback_handler (const gchar *log_domain,
782
GLogLevelFlags log_level,
783
const gchar *message,
784
gpointer unused_data)
786
gchar level_prefix[STRING_BUFFER_SIZE];
788
gchar pid_string[FORMAT_UNSIGNED_BUFSIZE];
792
/* we cannot call _any_ GLib functions in this fallback handler,
793
* which is why we skip UTF-8 conversion, etc.
794
* since we either recursed or ran out of memory, we're in a pretty
795
* pathologic situation anyways, what we can do is giving the
796
* the process ID unconditionally however.
799
fd = mklevel_prefix (level_prefix, log_level);
801
message = "(NULL) message";
804
format_unsigned (pid_string, getpid (), 10);
808
write_string (fd, "\n");
810
write_string (fd, "\n** ");
813
write_string (fd, "(process:");
814
write_string (fd, pid_string);
815
write_string (fd, "): ");
820
write_string (fd, log_domain);
821
write_string (fd, "-");
823
write_string (fd, level_prefix);
824
write_string (fd, ": ");
825
write_string (fd, message);
829
g_log_default_handler (const gchar *log_domain,
830
GLogLevelFlags log_level,
831
const gchar *message,
832
gpointer unused_data)
834
gchar level_prefix[STRING_BUFFER_SIZE], *string;
838
/* we can be called externally with recursion for whatever reason */
839
if (log_level & G_LOG_FLAG_RECURSION)
841
_g_log_fallback_handler (log_domain, log_level, message, unused_data);
845
g_messages_prefixed_init ();
847
fd = mklevel_prefix (level_prefix, log_level);
849
gstring = g_string_new (NULL);
850
if (log_level & ALERT_LEVELS)
851
g_string_append (gstring, "\n");
853
g_string_append (gstring, "** ");
855
if ((g_log_msg_prefix & log_level) == log_level)
857
const gchar *prg_name = g_get_prgname ();
860
g_string_append_printf (gstring, "(process:%lu): ", (gulong)getpid ());
862
g_string_append_printf (gstring, "(%s:%lu): ", prg_name, (gulong)getpid ());
867
g_string_append (gstring, log_domain);
868
g_string_append_c (gstring, '-');
870
g_string_append (gstring, level_prefix);
872
g_string_append (gstring, ": ");
874
g_string_append (gstring, "(NULL) message");
879
msg = g_string_new (message);
881
g_string_append (gstring, msg->str); /* assume UTF-8 */
883
g_string_free (msg, TRUE);
885
g_string_append (gstring, "\n");
887
string = g_string_free (gstring, FALSE);
889
write_string (fd, string);
894
* g_set_print_handler:
895
* @func: the new print handler
897
* Sets the print handler.
899
* Any messages passed to g_print() will be output via
900
* the new handler. The default handler simply outputs
901
* the message to stdout. By providing your own handler
902
* you can redirect the output, to a GTK+ widget or a
903
* log file for example.
905
* Returns: the old print handler
908
g_set_print_handler (GPrintFunc func)
910
GPrintFunc old_print_func;
912
g_mutex_lock (g_messages_lock);
913
old_print_func = glib_print_func;
914
glib_print_func = func;
915
g_mutex_unlock (g_messages_lock);
917
return old_print_func;
922
* @format: the message format. See the printf() documentation
923
* @...: the parameters to insert into the format string
925
* Outputs a formatted message via the print handler.
926
* The default print handler simply outputs the message to stdout.
928
* g_print() should not be used from within libraries for debugging
929
* messages, since it may be redirected by applications to special
930
* purpose message windows or even files. Instead, libraries should
931
* use g_log(), or the convenience functions g_message(), g_warning()
935
g_print (const gchar *format,
940
GPrintFunc local_glib_print_func;
942
g_return_if_fail (format != NULL);
944
va_start (args, format);
945
string = g_strdup_vprintf (format, args);
948
g_mutex_lock (g_messages_lock);
949
local_glib_print_func = glib_print_func;
950
g_mutex_unlock (g_messages_lock);
952
if (local_glib_print_func)
953
local_glib_print_func (string);
956
fputs (string, stdout); /* assume UTF-8 */
963
* g_set_printerr_handler:
964
* @func: the new error message handler
966
* Sets the handler for printing error messages.
968
* Any messages passed to g_printerr() will be output via
969
* the new handler. The default handler simply outputs the
970
* message to stderr. By providing your own handler you can
971
* redirect the output, to a GTK+ widget or a log file for
974
* Returns: the old error message handler
977
g_set_printerr_handler (GPrintFunc func)
979
GPrintFunc old_printerr_func;
981
g_mutex_lock (g_messages_lock);
982
old_printerr_func = glib_printerr_func;
983
glib_printerr_func = func;
984
g_mutex_unlock (g_messages_lock);
986
return old_printerr_func;
991
* @format: the message format. See the printf() documentation
992
* @...: the parameters to insert into the format string
994
* Outputs a formatted message via the error message handler.
995
* The default handler simply outputs the message to stderr.
997
* g_printerr() should not be used from within libraries.
998
* Instead g_log() should be used, or the convenience functions
999
* g_message(), g_warning() and g_error().
1002
g_printerr (const gchar *format,
1007
GPrintFunc local_glib_printerr_func;
1009
g_return_if_fail (format != NULL);
1011
va_start (args, format);
1012
string = g_strdup_vprintf (format, args);
1015
g_mutex_lock (g_messages_lock);
1016
local_glib_printerr_func = glib_printerr_func;
1017
g_mutex_unlock (g_messages_lock);
1019
if (local_glib_printerr_func)
1020
local_glib_printerr_func (string);
1023
fputs (string, stderr); /* assume UTF-8 */
1030
g_printf_string_upper_bound (const gchar *format,
1034
return _g_vsnprintf (&c, 1, format, args) + 1;
1038
_g_messages_thread_init_nomessage (void)
1040
g_messages_lock = g_mutex_new ();
1041
g_log_depth = g_private_new (NULL);
1042
g_messages_prefixed_init ();
1046
gboolean _g_debug_initialized = FALSE;
1047
guint _g_debug_flags = 0;
1050
_g_debug_init (void)
1054
_g_debug_initialized = TRUE;
1056
val = g_getenv ("G_DEBUG");
1059
const GDebugKey keys[] = {
1060
{"fatal_warnings", G_DEBUG_FATAL_WARNINGS},
1061
{"fatal_criticals", G_DEBUG_FATAL_CRITICALS}
1064
_g_debug_flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1067
if (_g_debug_flags & G_DEBUG_FATAL_WARNINGS)
1069
GLogLevelFlags fatal_mask;
1071
fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1072
fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1073
g_log_set_always_fatal (fatal_mask);
1076
if (_g_debug_flags & G_DEBUG_FATAL_CRITICALS)
1078
GLogLevelFlags fatal_mask;
1080
fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1081
fatal_mask |= G_LOG_LEVEL_CRITICAL;
1082
g_log_set_always_fatal (fatal_mask);