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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
48 /* Just use stdio. If we're out of memory, we're hosed anyway. */
56 fwrite (buf, len, 1, fd);
62 ensure_stdout_valid (void)
66 handle = GetStdHandle (STD_OUTPUT_HANDLE);
68 if (handle == INVALID_HANDLE_VALUE)
71 freopen ("CONOUT$", "w", stdout);
75 #define ensure_stdout_valid() /* Define as empty */
79 /* --- structures --- */
80 typedef struct _GLogDomain GLogDomain;
81 typedef struct _GLogHandler GLogHandler;
85 GLogLevelFlags fatal_mask;
86 GLogHandler *handlers;
92 GLogLevelFlags log_level;
99 /* --- variables --- */
101 static GMutex* g_messages_lock = NULL;
103 const gchar *g_log_domain_glib = "GLib";
104 static GLogDomain *g_log_domains = NULL;
105 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
106 static GPrintFunc glib_print_func = NULL;
107 static GPrintFunc glib_printerr_func = NULL;
108 static GErrorFunc glib_error_func = NULL;
109 static GWarningFunc glib_warning_func = NULL;
110 static GPrintFunc glib_message_func = NULL;
112 static GPrivate* g_log_depth = NULL;
115 /* --- functions --- */
116 static inline GLogDomain*
117 g_log_find_domain (const gchar *log_domain)
119 register GLogDomain *domain;
121 g_mutex_lock (g_messages_lock);
122 domain = g_log_domains;
125 if (strcmp (domain->log_domain, log_domain) == 0)
127 g_mutex_unlock (g_messages_lock);
130 domain = domain->next;
132 g_mutex_unlock (g_messages_lock);
136 static inline GLogDomain*
137 g_log_domain_new (const gchar *log_domain)
139 register GLogDomain *domain;
141 domain = g_new (GLogDomain, 1);
142 domain->log_domain = g_strdup (log_domain);
143 domain->fatal_mask = G_LOG_FATAL_MASK;
144 domain->handlers = NULL;
146 g_mutex_lock (g_messages_lock);
147 domain->next = g_log_domains;
148 g_log_domains = domain;
149 g_mutex_unlock (g_messages_lock);
155 g_log_domain_check_free (GLogDomain *domain)
157 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
158 domain->handlers == NULL)
160 register GLogDomain *last, *work;
164 g_mutex_lock (g_messages_lock);
165 work = g_log_domains;
171 last->next = domain->next;
173 g_log_domains = domain->next;
174 g_free (domain->log_domain);
180 g_mutex_unlock (g_messages_lock);
184 static inline GLogFunc
185 g_log_domain_get_handler (GLogDomain *domain,
186 GLogLevelFlags log_level,
189 if (domain && log_level)
191 register GLogHandler *handler;
193 handler = domain->handlers;
196 if ((handler->log_level & log_level) == log_level)
198 *data = handler->data;
199 return handler->log_func;
201 handler = handler->next;
204 return g_log_default_handler;
208 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
210 GLogLevelFlags old_mask;
212 /* restrict the global mask to levels that are known to glib */
213 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
214 /* force errors to be fatal */
215 fatal_mask |= G_LOG_LEVEL_ERROR;
216 /* remove bogus flag */
217 fatal_mask &= ~G_LOG_FLAG_FATAL;
219 g_mutex_lock (g_messages_lock);
220 old_mask = g_log_always_fatal;
221 g_log_always_fatal = fatal_mask;
222 g_mutex_unlock (g_messages_lock);
228 g_log_set_fatal_mask (const gchar *log_domain,
229 GLogLevelFlags fatal_mask)
231 GLogLevelFlags old_flags;
232 register GLogDomain *domain;
237 /* force errors to be fatal */
238 fatal_mask |= G_LOG_LEVEL_ERROR;
239 /* remove bogus flag */
240 fatal_mask &= ~G_LOG_FLAG_FATAL;
242 domain = g_log_find_domain (log_domain);
244 domain = g_log_domain_new (log_domain);
245 old_flags = domain->fatal_mask;
247 domain->fatal_mask = fatal_mask;
248 g_log_domain_check_free (domain);
254 g_log_set_handler (const gchar *log_domain,
255 GLogLevelFlags log_levels,
259 register GLogDomain *domain;
260 register GLogHandler *handler;
261 static guint handler_id = 0;
263 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
264 g_return_val_if_fail (log_func != NULL, 0);
269 domain = g_log_find_domain (log_domain);
271 domain = g_log_domain_new (log_domain);
273 handler = g_new (GLogHandler, 1);
274 g_mutex_lock (g_messages_lock);
275 handler->id = ++handler_id;
276 g_mutex_unlock (g_messages_lock);
277 handler->log_level = log_levels;
278 handler->log_func = log_func;
279 handler->data = user_data;
280 handler->next = domain->handlers;
281 domain->handlers = handler;
287 g_log_remove_handler (const gchar *log_domain,
290 register GLogDomain *domain;
292 g_return_if_fail (handler_id > 0);
297 domain = g_log_find_domain (log_domain);
300 register GLogHandler *work, *last;
303 work = domain->handlers;
306 if (work->id == handler_id)
309 last->next = work->next;
311 domain->handlers = work->next;
313 g_log_domain_check_free (domain);
319 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
325 g_logv (const gchar *log_domain,
326 GLogLevelFlags log_level,
334 log_level &= G_LOG_LEVEL_MASK;
338 /* we use a stack buffer of fixed size, because we might get called
341 G_VA_COPY (args2, args1);
342 if (g_printf_string_upper_bound (format, args1) < 1024)
343 vsprintf (buffer, format, args2);
346 /* since we might be out of memory, we can't use g_vsnprintf(). */
347 #ifdef HAVE_VSNPRINTF
348 vsnprintf (buffer, 1024, format, args2);
349 #else /* !HAVE_VSNPRINTF */
350 /* we are out of luck here */
351 strncpy (buffer, format, 1024);
352 #endif /* !HAVE_VSNPRINTF */
357 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
359 register GLogLevelFlags test_level;
362 if (log_level & test_level)
364 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
367 gpointer data = NULL;
369 domain = g_log_find_domain (log_domain ? log_domain : "");
372 test_level |= G_LOG_FLAG_RECURSION;
375 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
377 g_mutex_lock (g_messages_lock);
378 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
379 g_log_always_fatal) & test_level) != 0)
380 test_level |= G_LOG_FLAG_FATAL;
381 g_mutex_unlock (g_messages_lock);
383 log_func = g_log_domain_get_handler (domain, test_level, &data);
384 log_func (log_domain, test_level, buffer, data);
386 /* *domain can be cluttered now */
388 if (test_level & G_LOG_FLAG_FATAL)
392 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
398 g_log (const gchar *log_domain,
399 GLogLevelFlags log_level,
405 va_start (args, format);
406 g_logv (log_domain, log_level, format, args);
411 g_log_default_handler (const gchar *log_domain,
412 GLogLevelFlags log_level,
413 const gchar *message,
414 gpointer unused_data)
421 gboolean in_recursion;
423 GErrorFunc local_glib_error_func;
424 GWarningFunc local_glib_warning_func;
425 GPrintFunc local_glib_message_func;
427 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
428 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
429 log_level &= G_LOG_LEVEL_MASK;
432 message = "g_log_default_handler(): (NULL) message";
435 /* Use just stdout as stderr is hard to get redirected from the
440 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
443 g_mutex_lock (g_messages_lock);
444 local_glib_error_func = glib_error_func;
445 local_glib_warning_func = glib_warning_func;
446 local_glib_message_func = glib_message_func;
447 g_mutex_unlock (g_messages_lock);
451 case G_LOG_LEVEL_ERROR:
452 if (!log_domain && local_glib_error_func)
454 /* compatibility code */
455 local_glib_error_func (message);
458 /* use write(2) for output, in case we are out of memeory */
459 ensure_stdout_valid ();
463 write (fd, log_domain, strlen (log_domain));
467 write (fd, "\n** ", 4);
469 write (fd, "ERROR (recursed) **: ", 21);
471 write (fd, "ERROR **: ", 10);
472 write (fd, message, strlen(message));
474 write (fd, "\naborting...\n", 13);
478 case G_LOG_LEVEL_CRITICAL:
479 ensure_stdout_valid ();
483 write (fd, log_domain, strlen (log_domain));
487 write (fd, "\n** ", 4);
489 write (fd, "CRITICAL (recursed) **: ", 24);
491 write (fd, "CRITICAL **: ", 13);
492 write (fd, message, strlen(message));
494 write (fd, "\naborting...\n", 13);
498 case G_LOG_LEVEL_WARNING:
499 if (!log_domain && local_glib_warning_func)
501 /* compatibility code */
502 local_glib_warning_func (message);
505 ensure_stdout_valid ();
509 write (fd, log_domain, strlen (log_domain));
513 write (fd, "\n** ", 4);
515 write (fd, "WARNING (recursed) **: ", 23);
517 write (fd, "WARNING **: ", 12);
518 write (fd, message, strlen(message));
520 write (fd, "\naborting...\n", 13);
524 case G_LOG_LEVEL_MESSAGE:
525 if (!log_domain && local_glib_message_func)
527 /* compatibility code */
528 local_glib_message_func (message);
531 ensure_stdout_valid ();
534 write (fd, log_domain, strlen (log_domain));
538 write (fd, "Message (recursed): ", 20);
540 write (fd, "Message: ", 9);
541 write (fd, message, strlen(message));
543 write (fd, "\naborting...\n", 13);
547 case G_LOG_LEVEL_INFO:
548 ensure_stdout_valid ();
551 write (fd, log_domain, strlen (log_domain));
555 write (fd, "INFO (recursed): ", 17);
557 write (fd, "INFO: ", 6);
558 write (fd, message, strlen(message));
560 write (fd, "\naborting...\n", 13);
564 case G_LOG_LEVEL_DEBUG:
565 ensure_stdout_valid ();
568 write (fd, log_domain, strlen (log_domain));
572 write (fd, "DEBUG (recursed): ", 18);
574 write (fd, "DEBUG: ", 7);
575 write (fd, message, strlen(message));
577 write (fd, "\naborting...\n", 13);
582 /* we are used for a log level that is not defined by GLib itself,
583 * try to make the best out of it.
585 ensure_stdout_valid ();
588 write (fd, log_domain, strlen (log_domain));
590 write (fd, "-LOG (recursed:", 15);
592 write (fd, "-LOG (", 6);
594 else if (in_recursion)
595 write (fd, "LOG (recursed:", 14);
597 write (fd, "LOG (", 5);
600 gchar string[] = "0x00): ";
601 gchar *p = string + 2;
604 i = g_bit_nth_msf (log_level, -1);
607 *p = '0' + (i & 0xf);
611 write (fd, string, 7);
614 write (fd, "): ", 3);
615 write (fd, message, strlen(message));
617 write (fd, "\naborting...\n", 13);
625 g_set_print_handler (GPrintFunc func)
627 GPrintFunc old_print_func;
629 g_mutex_lock (g_messages_lock);
630 old_print_func = glib_print_func;
631 glib_print_func = func;
632 g_mutex_unlock (g_messages_lock);
634 return old_print_func;
638 g_print (const gchar *format,
643 GPrintFunc local_glib_print_func;
645 g_return_if_fail (format != NULL);
647 va_start (args, format);
648 string = g_strdup_vprintf (format, args);
651 g_mutex_lock (g_messages_lock);
652 local_glib_print_func = glib_print_func;
653 g_mutex_unlock (g_messages_lock);
655 if (local_glib_print_func)
656 local_glib_print_func (string);
659 ensure_stdout_valid ();
660 fputs (string, stdout);
667 g_set_printerr_handler (GPrintFunc func)
669 GPrintFunc old_printerr_func;
671 g_mutex_lock (g_messages_lock);
672 old_printerr_func = glib_printerr_func;
673 glib_printerr_func = func;
674 g_mutex_unlock (g_messages_lock);
676 return old_printerr_func;
680 g_printerr (const gchar *format,
685 GPrintFunc local_glib_printerr_func;
687 g_return_if_fail (format != NULL);
689 va_start (args, format);
690 string = g_strdup_vprintf (format, args);
693 g_mutex_lock (g_messages_lock);
694 local_glib_printerr_func = glib_printerr_func;
695 g_mutex_unlock (g_messages_lock);
697 if (local_glib_printerr_func)
698 local_glib_printerr_func (string);
701 fputs (string, stderr);
707 /* compatibility code */
709 g_set_error_handler (GErrorFunc func)
711 GErrorFunc old_error_func;
713 g_mutex_lock (g_messages_lock);
714 old_error_func = glib_error_func;
715 glib_error_func = func;
716 g_mutex_unlock (g_messages_lock);
718 return old_error_func;
721 /* compatibility code */
723 g_set_warning_handler (GWarningFunc func)
725 GWarningFunc old_warning_func;
727 g_mutex_lock (g_messages_lock);
728 old_warning_func = glib_warning_func;
729 glib_warning_func = func;
730 g_mutex_unlock (g_messages_lock);
732 return old_warning_func;
735 /* compatibility code */
737 g_set_message_handler (GPrintFunc func)
739 GPrintFunc old_message_func;
741 g_mutex_lock (g_messages_lock);
742 old_message_func = glib_message_func;
743 glib_message_func = func;
744 g_mutex_unlock (g_messages_lock);
746 return old_message_func;
750 g_messages_init (void)
752 g_messages_lock = g_mutex_new();
753 g_log_depth = g_private_new(NULL);