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);
53 #endif /* NATIVE_WIN32 */
56 ensure_stdout_valid (void)
61 handle = GetStdHandle (STD_OUTPUT_HANDLE);
63 if (handle == INVALID_HANDLE_VALUE)
66 freopen ("CONOUT$", "w", stdout);
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
432 ensure_stdout_valid ();
434 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
437 g_mutex_lock (g_messages_lock);
438 local_glib_error_func = glib_error_func;
439 local_glib_warning_func = glib_warning_func;
440 local_glib_message_func = glib_message_func;
441 g_mutex_unlock (g_messages_lock);
445 case G_LOG_LEVEL_ERROR:
446 if (!log_domain && local_glib_error_func)
448 /* compatibility code */
449 local_glib_error_func (message);
452 /* use write(2) for output, in case we are out of memeory */
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:
475 write (fd, log_domain, strlen (log_domain));
479 write (fd, "\n** ", 4);
481 write (fd, "CRITICAL (recursed) **: ", 24);
483 write (fd, "CRITICAL **: ", 13);
484 write (fd, message, strlen(message));
486 write (fd, "\naborting...\n", 13);
490 case G_LOG_LEVEL_WARNING:
491 if (!log_domain && local_glib_warning_func)
493 /* compatibility code */
494 local_glib_warning_func (message);
500 write (fd, log_domain, strlen (log_domain));
504 write (fd, "\n** ", 4);
506 write (fd, "WARNING (recursed) **: ", 23);
508 write (fd, "WARNING **: ", 12);
509 write (fd, message, strlen(message));
511 write (fd, "\naborting...\n", 13);
515 case G_LOG_LEVEL_MESSAGE:
516 if (!log_domain && local_glib_message_func)
518 /* compatibility code */
519 local_glib_message_func (message);
524 write (fd, log_domain, strlen (log_domain));
528 write (fd, "Message (recursed): ", 20);
530 write (fd, "Message: ", 9);
531 write (fd, message, strlen(message));
533 write (fd, "\naborting...\n", 13);
537 case G_LOG_LEVEL_INFO:
540 write (fd, log_domain, strlen (log_domain));
544 write (fd, "INFO (recursed): ", 17);
546 write (fd, "INFO: ", 6);
547 write (fd, message, strlen(message));
549 write (fd, "\naborting...\n", 13);
553 case G_LOG_LEVEL_DEBUG:
556 write (fd, log_domain, strlen (log_domain));
560 write (fd, "DEBUG (recursed): ", 18);
562 write (fd, "DEBUG: ", 7);
563 write (fd, message, strlen(message));
565 write (fd, "\naborting...\n", 13);
570 /* we are used for a log level that is not defined by GLib itself,
571 * try to make the best out of it.
575 write (fd, log_domain, strlen (log_domain));
577 write (fd, "-LOG (recursed:", 15);
579 write (fd, "-LOG (", 6);
581 else if (in_recursion)
582 write (fd, "LOG (recursed:", 14);
584 write (fd, "LOG (", 5);
587 gchar string[] = "0x00): ";
588 gchar *p = string + 2;
591 i = g_bit_nth_msf (log_level, -1);
594 *p = '0' + (i & 0xf);
598 write (fd, string, 7);
601 write (fd, "): ", 3);
602 write (fd, message, strlen(message));
604 write (fd, "\naborting...\n", 13);
612 g_set_print_handler (GPrintFunc func)
614 GPrintFunc old_print_func;
616 g_mutex_lock (g_messages_lock);
617 old_print_func = glib_print_func;
618 glib_print_func = func;
619 g_mutex_unlock (g_messages_lock);
621 return old_print_func;
625 g_print (const gchar *format,
630 GPrintFunc local_glib_print_func;
632 g_return_if_fail (format != NULL);
634 va_start (args, format);
635 string = g_strdup_vprintf (format, args);
638 g_mutex_lock (g_messages_lock);
639 local_glib_print_func = glib_print_func;
640 g_mutex_unlock (g_messages_lock);
642 if (local_glib_print_func)
643 local_glib_print_func (string);
646 ensure_stdout_valid ();
647 fputs (string, stdout);
654 g_set_printerr_handler (GPrintFunc func)
656 GPrintFunc old_printerr_func;
658 g_mutex_lock (g_messages_lock);
659 old_printerr_func = glib_printerr_func;
660 glib_printerr_func = func;
661 g_mutex_unlock (g_messages_lock);
663 return old_printerr_func;
667 g_printerr (const gchar *format,
672 GPrintFunc local_glib_printerr_func;
674 g_return_if_fail (format != NULL);
676 va_start (args, format);
677 string = g_strdup_vprintf (format, args);
680 g_mutex_lock (g_messages_lock);
681 local_glib_printerr_func = glib_printerr_func;
682 g_mutex_unlock (g_messages_lock);
684 if (local_glib_printerr_func)
685 local_glib_printerr_func (string);
688 fputs (string, stderr);
694 /* compatibility code */
696 g_set_error_handler (GErrorFunc func)
698 GErrorFunc old_error_func;
700 g_mutex_lock (g_messages_lock);
701 old_error_func = glib_error_func;
702 glib_error_func = func;
703 g_mutex_unlock (g_messages_lock);
705 return old_error_func;
708 /* compatibility code */
710 g_set_warning_handler (GWarningFunc func)
712 GWarningFunc old_warning_func;
714 g_mutex_lock (g_messages_lock);
715 old_warning_func = glib_warning_func;
716 glib_warning_func = func;
717 g_mutex_unlock (g_messages_lock);
719 return old_warning_func;
722 /* compatibility code */
724 g_set_message_handler (GPrintFunc func)
726 GPrintFunc old_message_func;
728 g_mutex_lock (g_messages_lock);
729 old_message_func = glib_message_func;
730 glib_message_func = func;
731 g_mutex_unlock (g_messages_lock);
733 return old_message_func;
737 g_messages_init (void)
739 g_messages_lock = g_mutex_new();
740 g_log_depth = g_private_new(NULL);