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 # include <process.h> /* For _getpid() */
50 /* Just use stdio. If we're out of memory, we're hosed anyway. */
58 fwrite (buf, len, 1, fd);
64 ensure_stdout_valid (void)
68 handle = GetStdHandle (STD_OUTPUT_HANDLE);
70 if (handle == INVALID_HANDLE_VALUE)
73 freopen ("CONOUT$", "w", stdout);
77 #define ensure_stdout_valid() /* Define as empty */
81 /* --- structures --- */
82 typedef struct _GLogDomain GLogDomain;
83 typedef struct _GLogHandler GLogHandler;
87 GLogLevelFlags fatal_mask;
88 GLogHandler *handlers;
94 GLogLevelFlags log_level;
101 /* --- variables --- */
103 static GMutex* g_messages_lock = NULL;
105 const gchar *g_log_domain_glib = "GLib";
106 static GLogDomain *g_log_domains = NULL;
107 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
108 static GPrintFunc glib_print_func = NULL;
109 static GPrintFunc glib_printerr_func = NULL;
110 static GErrorFunc glib_error_func = NULL;
111 static GWarningFunc glib_warning_func = NULL;
112 static GPrintFunc glib_message_func = NULL;
114 static GPrivate* g_log_depth = NULL;
117 /* --- functions --- */
118 static inline GLogDomain*
119 g_log_find_domain (const gchar *log_domain)
121 register GLogDomain *domain;
123 g_mutex_lock (g_messages_lock);
124 domain = g_log_domains;
127 if (strcmp (domain->log_domain, log_domain) == 0)
129 g_mutex_unlock (g_messages_lock);
132 domain = domain->next;
134 g_mutex_unlock (g_messages_lock);
138 static inline GLogDomain*
139 g_log_domain_new (const gchar *log_domain)
141 register GLogDomain *domain;
143 domain = g_new (GLogDomain, 1);
144 domain->log_domain = g_strdup (log_domain);
145 domain->fatal_mask = G_LOG_FATAL_MASK;
146 domain->handlers = NULL;
148 g_mutex_lock (g_messages_lock);
149 domain->next = g_log_domains;
150 g_log_domains = domain;
151 g_mutex_unlock (g_messages_lock);
157 g_log_domain_check_free (GLogDomain *domain)
159 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
160 domain->handlers == NULL)
162 register GLogDomain *last, *work;
166 g_mutex_lock (g_messages_lock);
167 work = g_log_domains;
173 last->next = domain->next;
175 g_log_domains = domain->next;
176 g_free (domain->log_domain);
182 g_mutex_unlock (g_messages_lock);
186 static inline GLogFunc
187 g_log_domain_get_handler (GLogDomain *domain,
188 GLogLevelFlags log_level,
191 if (domain && log_level)
193 register GLogHandler *handler;
195 handler = domain->handlers;
198 if ((handler->log_level & log_level) == log_level)
200 *data = handler->data;
201 return handler->log_func;
203 handler = handler->next;
206 return g_log_default_handler;
210 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
212 GLogLevelFlags old_mask;
214 /* restrict the global mask to levels that are known to glib */
215 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
216 /* force errors to be fatal */
217 fatal_mask |= G_LOG_LEVEL_ERROR;
218 /* remove bogus flag */
219 fatal_mask &= ~G_LOG_FLAG_FATAL;
221 g_mutex_lock (g_messages_lock);
222 old_mask = g_log_always_fatal;
223 g_log_always_fatal = fatal_mask;
224 g_mutex_unlock (g_messages_lock);
230 g_log_set_fatal_mask (const gchar *log_domain,
231 GLogLevelFlags fatal_mask)
233 GLogLevelFlags old_flags;
234 register GLogDomain *domain;
239 /* force errors to be fatal */
240 fatal_mask |= G_LOG_LEVEL_ERROR;
241 /* remove bogus flag */
242 fatal_mask &= ~G_LOG_FLAG_FATAL;
244 domain = g_log_find_domain (log_domain);
246 domain = g_log_domain_new (log_domain);
247 old_flags = domain->fatal_mask;
249 domain->fatal_mask = fatal_mask;
250 g_log_domain_check_free (domain);
256 g_log_set_handler (const gchar *log_domain,
257 GLogLevelFlags log_levels,
261 register GLogDomain *domain;
262 register GLogHandler *handler;
263 static guint handler_id = 0;
265 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
266 g_return_val_if_fail (log_func != NULL, 0);
271 domain = g_log_find_domain (log_domain);
273 domain = g_log_domain_new (log_domain);
275 handler = g_new (GLogHandler, 1);
276 g_mutex_lock (g_messages_lock);
277 handler->id = ++handler_id;
278 g_mutex_unlock (g_messages_lock);
279 handler->log_level = log_levels;
280 handler->log_func = log_func;
281 handler->data = user_data;
282 handler->next = domain->handlers;
283 domain->handlers = handler;
289 g_log_remove_handler (const gchar *log_domain,
292 register GLogDomain *domain;
294 g_return_if_fail (handler_id > 0);
299 domain = g_log_find_domain (log_domain);
302 register GLogHandler *work, *last;
305 work = domain->handlers;
308 if (work->id == handler_id)
311 last->next = work->next;
313 domain->handlers = work->next;
315 g_log_domain_check_free (domain);
321 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
327 g_logv (const gchar *log_domain,
328 GLogLevelFlags log_level,
336 log_level &= G_LOG_LEVEL_MASK;
340 /* we use a stack buffer of fixed size, because we might get called
343 G_VA_COPY (args2, args1);
344 if (g_printf_string_upper_bound (format, args1) < 1024)
345 vsprintf (buffer, format, args2);
348 /* since we might be out of memory, we can't use g_vsnprintf(). */
349 #ifdef HAVE_VSNPRINTF
350 vsnprintf (buffer, 1024, format, args2);
351 #else /* !HAVE_VSNPRINTF */
352 /* we are out of luck here */
353 strncpy (buffer, format, 1024);
354 #endif /* !HAVE_VSNPRINTF */
359 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
361 register GLogLevelFlags test_level;
364 if (log_level & test_level)
366 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
369 gpointer data = NULL;
371 domain = g_log_find_domain (log_domain ? log_domain : "");
374 test_level |= G_LOG_FLAG_RECURSION;
377 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
379 g_mutex_lock (g_messages_lock);
380 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
381 g_log_always_fatal) & test_level) != 0)
382 test_level |= G_LOG_FLAG_FATAL;
383 g_mutex_unlock (g_messages_lock);
385 log_func = g_log_domain_get_handler (domain, test_level, &data);
386 log_func (log_domain, test_level, buffer, data);
388 /* *domain can be cluttered now */
390 if (test_level & G_LOG_FLAG_FATAL)
392 #if defined (G_ENABLE_DEBUG) && defined (SIGTRAP)
393 if (!(test_level & G_LOG_FLAG_RECURSION))
397 #else /* !G_ENABLE_DEBUG || !SIGTRAP */
399 #endif /* !G_ENABLE_DEBUG || !SIGTRAP */
403 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
409 g_log (const gchar *log_domain,
410 GLogLevelFlags log_level,
416 va_start (args, format);
417 g_logv (log_domain, log_level, format, args);
422 g_log_default_handler (const gchar *log_domain,
423 GLogLevelFlags log_level,
424 const gchar *message,
425 gpointer unused_data)
432 gboolean in_recursion;
434 GErrorFunc local_glib_error_func;
435 GWarningFunc local_glib_warning_func;
436 GPrintFunc local_glib_message_func;
437 gchar prg_pid[64], *prg_name = g_get_prgname ();
439 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
440 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
441 log_level &= G_LOG_LEVEL_MASK;
444 message = "g_log_default_handler(): (NULL) message";
447 prg_name = "(process";
448 sprintf (prg_pid, ":%u): ", getpid ());
451 sprintf (prg_pid, " (pid:%u): ", getpid ());
454 /* Use just stdout as stderr is hard to get redirected from the
459 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
462 g_mutex_lock (g_messages_lock);
463 local_glib_error_func = glib_error_func;
464 local_glib_warning_func = glib_warning_func;
465 local_glib_message_func = glib_message_func;
466 g_mutex_unlock (g_messages_lock);
470 case G_LOG_LEVEL_ERROR:
471 if (!log_domain && local_glib_error_func)
473 /* compatibility code */
474 local_glib_error_func (message);
477 /* use write(2) for output, in case we are out of memeory */
478 ensure_stdout_valid ();
480 #ifdef G_ENABLE_MSG_PREFIX
481 write (fd, prg_name, strlen (prg_name));
482 write (fd, prg_pid, strlen (prg_pid));
483 #endif /* G_ENABLE_MSG_PREFIX */
486 write (fd, log_domain, strlen (log_domain));
490 write (fd, "** ", 3);
492 write (fd, "ERROR (recursed) **: ", 21);
494 write (fd, "ERROR **: ", 10);
495 write (fd, message, strlen (message));
497 write (fd, "\naborting...\n", 13);
501 case G_LOG_LEVEL_CRITICAL:
502 ensure_stdout_valid ();
504 #ifdef G_ENABLE_MSG_PREFIX
505 write (fd, prg_name, strlen (prg_name));
506 write (fd, prg_pid, strlen (prg_pid));
507 #endif /* G_ENABLE_MSG_PREFIX */
510 write (fd, log_domain, strlen (log_domain));
514 write (fd, "** ", 3);
516 write (fd, "CRITICAL (recursed) **: ", 24);
518 write (fd, "CRITICAL **: ", 13);
519 write (fd, message, strlen (message));
521 write (fd, "\naborting...\n", 13);
525 case G_LOG_LEVEL_WARNING:
526 if (!log_domain && local_glib_warning_func)
528 /* compatibility code */
529 local_glib_warning_func (message);
532 ensure_stdout_valid ();
534 #ifdef G_ENABLE_MSG_PREFIX
535 write (fd, prg_name, strlen (prg_name));
536 write (fd, prg_pid, strlen (prg_pid));
537 #endif /* G_ENABLE_MSG_PREFIX */
540 write (fd, log_domain, strlen (log_domain));
544 write (fd, "** ", 3);
546 write (fd, "WARNING (recursed) **: ", 23);
548 write (fd, "WARNING **: ", 12);
549 write (fd, message, strlen (message));
551 write (fd, "\naborting...\n", 13);
555 case G_LOG_LEVEL_MESSAGE:
556 if (!log_domain && local_glib_message_func)
558 /* compatibility code */
559 local_glib_message_func (message);
562 ensure_stdout_valid ();
563 #ifdef G_ENABLE_MSG_PREFIX
564 write (fd, prg_name, strlen (prg_name));
565 write (fd, prg_pid, strlen (prg_pid));
566 #endif /* G_ENABLE_MSG_PREFIX */
569 write (fd, log_domain, strlen (log_domain));
573 write (fd, "Message (recursed): ", 20);
575 write (fd, "Message: ", 9);
576 write (fd, message, strlen (message));
578 write (fd, "\naborting...\n", 13);
582 case G_LOG_LEVEL_INFO:
583 ensure_stdout_valid ();
584 #ifdef G_ENABLE_MSG_PREFIX
585 write (fd, prg_name, strlen (prg_name));
586 write (fd, prg_pid, strlen (prg_pid));
587 #endif /* G_ENABLE_MSG_PREFIX */
590 write (fd, log_domain, strlen (log_domain));
594 write (fd, "INFO (recursed): ", 17);
596 write (fd, "INFO: ", 6);
597 write (fd, message, strlen (message));
599 write (fd, "\naborting...\n", 13);
603 case G_LOG_LEVEL_DEBUG:
604 ensure_stdout_valid ();
605 #ifdef G_ENABLE_MSG_PREFIX
606 write (fd, prg_name, strlen (prg_name));
607 write (fd, prg_pid, strlen (prg_pid));
608 #endif /* G_ENABLE_MSG_PREFIX */
611 write (fd, log_domain, strlen (log_domain));
615 write (fd, "DEBUG (recursed): ", 18);
617 write (fd, "DEBUG: ", 7);
618 write (fd, message, strlen (message));
620 write (fd, "\naborting...\n", 13);
625 /* we are used for a log level that is not defined by GLib itself,
626 * try to make the best out of it.
628 ensure_stdout_valid ();
629 #ifdef G_ENABLE_MSG_PREFIX
630 write (fd, prg_name, strlen (prg_name));
631 write (fd, prg_pid, strlen (prg_pid));
632 #endif /* G_ENABLE_MSG_PREFIX */
635 write (fd, log_domain, strlen (log_domain));
637 write (fd, "-LOG (recursed:", 15);
639 write (fd, "-LOG (", 6);
641 else if (in_recursion)
642 write (fd, "LOG (recursed:", 14);
644 write (fd, "LOG (", 5);
647 gchar string[] = "0x00): ";
648 gchar *p = string + 2;
651 i = g_bit_nth_msf (log_level, -1);
654 *p = '0' + (i & 0xf);
658 write (fd, string, 7);
661 write (fd, "): ", 3);
662 write (fd, message, strlen (message));
664 write (fd, "\naborting...\n", 13);
672 g_set_print_handler (GPrintFunc func)
674 GPrintFunc old_print_func;
676 g_mutex_lock (g_messages_lock);
677 old_print_func = glib_print_func;
678 glib_print_func = func;
679 g_mutex_unlock (g_messages_lock);
681 return old_print_func;
685 g_print (const gchar *format,
690 GPrintFunc local_glib_print_func;
692 g_return_if_fail (format != NULL);
694 va_start (args, format);
695 string = g_strdup_vprintf (format, args);
698 g_mutex_lock (g_messages_lock);
699 local_glib_print_func = glib_print_func;
700 g_mutex_unlock (g_messages_lock);
702 if (local_glib_print_func)
703 local_glib_print_func (string);
706 ensure_stdout_valid ();
707 fputs (string, stdout);
714 g_set_printerr_handler (GPrintFunc func)
716 GPrintFunc old_printerr_func;
718 g_mutex_lock (g_messages_lock);
719 old_printerr_func = glib_printerr_func;
720 glib_printerr_func = func;
721 g_mutex_unlock (g_messages_lock);
723 return old_printerr_func;
727 g_printerr (const gchar *format,
732 GPrintFunc local_glib_printerr_func;
734 g_return_if_fail (format != NULL);
736 va_start (args, format);
737 string = g_strdup_vprintf (format, args);
740 g_mutex_lock (g_messages_lock);
741 local_glib_printerr_func = glib_printerr_func;
742 g_mutex_unlock (g_messages_lock);
744 if (local_glib_printerr_func)
745 local_glib_printerr_func (string);
748 fputs (string, stderr);
754 /* compatibility code */
756 g_set_error_handler (GErrorFunc func)
758 GErrorFunc old_error_func;
760 g_mutex_lock (g_messages_lock);
761 old_error_func = glib_error_func;
762 glib_error_func = func;
763 g_mutex_unlock (g_messages_lock);
765 return old_error_func;
768 /* compatibility code */
770 g_set_warning_handler (GWarningFunc func)
772 GWarningFunc old_warning_func;
774 g_mutex_lock (g_messages_lock);
775 old_warning_func = glib_warning_func;
776 glib_warning_func = func;
777 g_mutex_unlock (g_messages_lock);
779 return old_warning_func;
782 /* compatibility code */
784 g_set_message_handler (GPrintFunc func)
786 GPrintFunc old_message_func;
788 g_mutex_lock (g_messages_lock);
789 old_message_func = glib_message_func;
790 glib_message_func = func;
791 g_mutex_unlock (g_messages_lock);
793 return old_message_func;
797 g_messages_init (void)
799 g_messages_lock = g_mutex_new();
800 g_log_depth = g_private_new(NULL);