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.
41 /* Just use stdio. If we're out of memory, we're hosed anyway. */
49 fwrite (buf, len, 1, fd);
55 ensure_stdout_valid (void)
59 handle = GetStdHandle (STD_OUTPUT_HANDLE);
61 if (handle == INVALID_HANDLE_VALUE)
64 freopen ("CONOUT$", "w", stdout);
68 #define ensure_stdout_valid() /* Define as empty */
72 /* --- structures --- */
73 typedef struct _GLogDomain GLogDomain;
74 typedef struct _GLogHandler GLogHandler;
78 GLogLevelFlags fatal_mask;
79 GLogHandler *handlers;
85 GLogLevelFlags log_level;
92 /* --- variables --- */
94 static GMutex* g_messages_lock = NULL;
96 const gchar *g_log_domain_glib = "GLib";
97 static GLogDomain *g_log_domains = NULL;
98 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
99 static GPrintFunc glib_print_func = NULL;
100 static GPrintFunc glib_printerr_func = NULL;
101 static GErrorFunc glib_error_func = NULL;
102 static GWarningFunc glib_warning_func = NULL;
103 static GPrintFunc glib_message_func = NULL;
105 static GPrivate* g_log_depth = NULL;
108 /* --- functions --- */
109 static inline GLogDomain*
110 g_log_find_domain (const gchar *log_domain)
112 register GLogDomain *domain;
114 g_mutex_lock (g_messages_lock);
115 domain = g_log_domains;
118 if (strcmp (domain->log_domain, log_domain) == 0)
120 g_mutex_unlock (g_messages_lock);
123 domain = domain->next;
125 g_mutex_unlock (g_messages_lock);
129 static inline GLogDomain*
130 g_log_domain_new (const gchar *log_domain)
132 register GLogDomain *domain;
134 domain = g_new (GLogDomain, 1);
135 domain->log_domain = g_strdup (log_domain);
136 domain->fatal_mask = G_LOG_FATAL_MASK;
137 domain->handlers = NULL;
139 g_mutex_lock (g_messages_lock);
140 domain->next = g_log_domains;
141 g_log_domains = domain;
142 g_mutex_unlock (g_messages_lock);
148 g_log_domain_check_free (GLogDomain *domain)
150 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
151 domain->handlers == NULL)
153 register GLogDomain *last, *work;
157 g_mutex_lock (g_messages_lock);
158 work = g_log_domains;
164 last->next = domain->next;
166 g_log_domains = domain->next;
167 g_free (domain->log_domain);
173 g_mutex_unlock (g_messages_lock);
177 static inline GLogFunc
178 g_log_domain_get_handler (GLogDomain *domain,
179 GLogLevelFlags log_level,
182 if (domain && log_level)
184 register GLogHandler *handler;
186 handler = domain->handlers;
189 if ((handler->log_level & log_level) == log_level)
191 *data = handler->data;
192 return handler->log_func;
194 handler = handler->next;
197 return g_log_default_handler;
201 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
203 GLogLevelFlags old_mask;
205 /* restrict the global mask to levels that are known to glib */
206 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
207 /* force errors to be fatal */
208 fatal_mask |= G_LOG_LEVEL_ERROR;
209 /* remove bogus flag */
210 fatal_mask &= ~G_LOG_FLAG_FATAL;
212 g_mutex_lock (g_messages_lock);
213 old_mask = g_log_always_fatal;
214 g_log_always_fatal = fatal_mask;
215 g_mutex_unlock (g_messages_lock);
221 g_log_set_fatal_mask (const gchar *log_domain,
222 GLogLevelFlags fatal_mask)
224 GLogLevelFlags old_flags;
225 register GLogDomain *domain;
230 /* force errors to be fatal */
231 fatal_mask |= G_LOG_LEVEL_ERROR;
232 /* remove bogus flag */
233 fatal_mask &= ~G_LOG_FLAG_FATAL;
235 domain = g_log_find_domain (log_domain);
237 domain = g_log_domain_new (log_domain);
238 old_flags = domain->fatal_mask;
240 domain->fatal_mask = fatal_mask;
241 g_log_domain_check_free (domain);
247 g_log_set_handler (const gchar *log_domain,
248 GLogLevelFlags log_levels,
252 register GLogDomain *domain;
253 register GLogHandler *handler;
254 static guint handler_id = 0;
256 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
257 g_return_val_if_fail (log_func != NULL, 0);
262 domain = g_log_find_domain (log_domain);
264 domain = g_log_domain_new (log_domain);
266 handler = g_new (GLogHandler, 1);
267 g_mutex_lock (g_messages_lock);
268 handler->id = ++handler_id;
269 g_mutex_unlock (g_messages_lock);
270 handler->log_level = log_levels;
271 handler->log_func = log_func;
272 handler->data = user_data;
273 handler->next = domain->handlers;
274 domain->handlers = handler;
280 g_log_remove_handler (const gchar *log_domain,
283 register GLogDomain *domain;
285 g_return_if_fail (handler_id > 0);
290 domain = g_log_find_domain (log_domain);
293 register GLogHandler *work, *last;
296 work = domain->handlers;
299 if (work->id == handler_id)
302 last->next = work->next;
304 domain->handlers = work->next;
306 g_log_domain_check_free (domain);
312 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
318 g_logv (const gchar *log_domain,
319 GLogLevelFlags log_level,
327 log_level &= G_LOG_LEVEL_MASK;
331 /* we use a stack buffer of fixed size, because we might get called
334 G_VA_COPY (args2, args1);
335 if (g_printf_string_upper_bound (format, args1) < 1024)
336 vsprintf (buffer, format, args2);
339 /* since we might be out of memory, we can't use g_vsnprintf(). */
340 #ifdef HAVE_VSNPRINTF
341 vsnprintf (buffer, 1024, format, args2);
342 #else /* !HAVE_VSNPRINTF */
343 /* we are out of luck here */
344 strncpy (buffer, format, 1024);
345 #endif /* !HAVE_VSNPRINTF */
350 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
352 register GLogLevelFlags test_level;
355 if (log_level & test_level)
357 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
360 gpointer data = NULL;
362 domain = g_log_find_domain (log_domain ? log_domain : "");
365 test_level |= G_LOG_FLAG_RECURSION;
368 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
370 g_mutex_lock (g_messages_lock);
371 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
372 g_log_always_fatal) & test_level) != 0)
373 test_level |= G_LOG_FLAG_FATAL;
374 g_mutex_unlock (g_messages_lock);
376 log_func = g_log_domain_get_handler (domain, test_level, &data);
377 log_func (log_domain, test_level, buffer, data);
379 /* *domain can be cluttered now */
381 if (test_level & G_LOG_FLAG_FATAL)
385 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
391 g_log (const gchar *log_domain,
392 GLogLevelFlags log_level,
398 va_start (args, format);
399 g_logv (log_domain, log_level, format, args);
404 g_log_default_handler (const gchar *log_domain,
405 GLogLevelFlags log_level,
406 const gchar *message,
407 gpointer unused_data)
414 gboolean in_recursion;
416 GErrorFunc local_glib_error_func;
417 GWarningFunc local_glib_warning_func;
418 GPrintFunc local_glib_message_func;
420 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
421 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
422 log_level &= G_LOG_LEVEL_MASK;
425 message = "g_log_default_handler(): (NULL) message";
428 /* Use just stdout as stderr is hard to get redirected from the
433 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
436 g_mutex_lock (g_messages_lock);
437 local_glib_error_func = glib_error_func;
438 local_glib_warning_func = glib_warning_func;
439 local_glib_message_func = glib_message_func;
440 g_mutex_unlock (g_messages_lock);
444 case G_LOG_LEVEL_ERROR:
445 if (!log_domain && local_glib_error_func)
447 /* compatibility code */
448 local_glib_error_func (message);
451 /* use write(2) for output, in case we are out of memeory */
452 ensure_stdout_valid ();
456 write (fd, log_domain, strlen (log_domain));
460 write (fd, "\n** ", 4);
462 write (fd, "ERROR (recursed) **: ", 21);
464 write (fd, "ERROR **: ", 10);
465 write (fd, message, strlen(message));
467 write (fd, "\naborting...\n", 13);
471 case G_LOG_LEVEL_CRITICAL:
472 ensure_stdout_valid ();
476 write (fd, log_domain, strlen (log_domain));
480 write (fd, "\n** ", 4);
482 write (fd, "CRITICAL (recursed) **: ", 24);
484 write (fd, "CRITICAL **: ", 13);
485 write (fd, message, strlen(message));
487 write (fd, "\naborting...\n", 13);
491 case G_LOG_LEVEL_WARNING:
492 if (!log_domain && local_glib_warning_func)
494 /* compatibility code */
495 local_glib_warning_func (message);
498 ensure_stdout_valid ();
502 write (fd, log_domain, strlen (log_domain));
506 write (fd, "\n** ", 4);
508 write (fd, "WARNING (recursed) **: ", 23);
510 write (fd, "WARNING **: ", 12);
511 write (fd, message, strlen(message));
513 write (fd, "\naborting...\n", 13);
517 case G_LOG_LEVEL_MESSAGE:
518 if (!log_domain && local_glib_message_func)
520 /* compatibility code */
521 local_glib_message_func (message);
524 ensure_stdout_valid ();
527 write (fd, log_domain, strlen (log_domain));
531 write (fd, "Message (recursed): ", 20);
533 write (fd, "Message: ", 9);
534 write (fd, message, strlen(message));
536 write (fd, "\naborting...\n", 13);
540 case G_LOG_LEVEL_INFO:
541 ensure_stdout_valid ();
544 write (fd, log_domain, strlen (log_domain));
548 write (fd, "INFO (recursed): ", 17);
550 write (fd, "INFO: ", 6);
551 write (fd, message, strlen(message));
553 write (fd, "\naborting...\n", 13);
557 case G_LOG_LEVEL_DEBUG:
558 ensure_stdout_valid ();
561 write (fd, log_domain, strlen (log_domain));
565 write (fd, "DEBUG (recursed): ", 18);
567 write (fd, "DEBUG: ", 7);
568 write (fd, message, strlen(message));
570 write (fd, "\naborting...\n", 13);
575 /* we are used for a log level that is not defined by GLib itself,
576 * try to make the best out of it.
578 ensure_stdout_valid ();
581 write (fd, log_domain, strlen (log_domain));
583 write (fd, "-LOG (recursed:", 15);
585 write (fd, "-LOG (", 6);
587 else if (in_recursion)
588 write (fd, "LOG (recursed:", 14);
590 write (fd, "LOG (", 5);
593 gchar string[] = "0x00): ";
594 gchar *p = string + 2;
597 i = g_bit_nth_msf (log_level, -1);
600 *p = '0' + (i & 0xf);
604 write (fd, string, 7);
607 write (fd, "): ", 3);
608 write (fd, message, strlen(message));
610 write (fd, "\naborting...\n", 13);
618 g_set_print_handler (GPrintFunc func)
620 GPrintFunc old_print_func;
622 g_mutex_lock (g_messages_lock);
623 old_print_func = glib_print_func;
624 glib_print_func = func;
625 g_mutex_unlock (g_messages_lock);
627 return old_print_func;
631 g_print (const gchar *format,
636 GPrintFunc local_glib_print_func;
638 g_return_if_fail (format != NULL);
640 va_start (args, format);
641 string = g_strdup_vprintf (format, args);
644 g_mutex_lock (g_messages_lock);
645 local_glib_print_func = glib_print_func;
646 g_mutex_unlock (g_messages_lock);
648 if (local_glib_print_func)
649 local_glib_print_func (string);
652 ensure_stdout_valid ();
653 fputs (string, stdout);
660 g_set_printerr_handler (GPrintFunc func)
662 GPrintFunc old_printerr_func;
664 g_mutex_lock (g_messages_lock);
665 old_printerr_func = glib_printerr_func;
666 glib_printerr_func = func;
667 g_mutex_unlock (g_messages_lock);
669 return old_printerr_func;
673 g_printerr (const gchar *format,
678 GPrintFunc local_glib_printerr_func;
680 g_return_if_fail (format != NULL);
682 va_start (args, format);
683 string = g_strdup_vprintf (format, args);
686 g_mutex_lock (g_messages_lock);
687 local_glib_printerr_func = glib_printerr_func;
688 g_mutex_unlock (g_messages_lock);
690 if (local_glib_printerr_func)
691 local_glib_printerr_func (string);
694 fputs (string, stderr);
700 /* compatibility code */
702 g_set_error_handler (GErrorFunc func)
704 GErrorFunc old_error_func;
706 g_mutex_lock (g_messages_lock);
707 old_error_func = glib_error_func;
708 glib_error_func = func;
709 g_mutex_unlock (g_messages_lock);
711 return old_error_func;
714 /* compatibility code */
716 g_set_warning_handler (GWarningFunc func)
718 GWarningFunc old_warning_func;
720 g_mutex_lock (g_messages_lock);
721 old_warning_func = glib_warning_func;
722 glib_warning_func = func;
723 g_mutex_unlock (g_messages_lock);
725 return old_warning_func;
728 /* compatibility code */
730 g_set_message_handler (GPrintFunc func)
732 GPrintFunc old_message_func;
734 g_mutex_lock (g_messages_lock);
735 old_message_func = glib_message_func;
736 glib_message_func = func;
737 g_mutex_unlock (g_messages_lock);
739 return old_message_func;
743 g_messages_init (void)
745 g_messages_lock = g_mutex_new();
746 g_log_depth = g_private_new(NULL);