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.
38 /* Just use stdio. If we're out of memory, we're hosed anyway. */
46 fwrite (buf, len, 1, fd);
50 #endif /* NATIVE_WIN32 */
53 /* --- structures --- */
54 typedef struct _GLogDomain GLogDomain;
55 typedef struct _GLogHandler GLogHandler;
59 GLogLevelFlags fatal_mask;
60 GLogHandler *handlers;
66 GLogLevelFlags log_level;
73 /* --- variables --- */
75 static GMutex* g_messages_lock = NULL;
77 const gchar *g_log_domain_glib = "GLib";
78 static GLogDomain *g_log_domains = NULL;
79 static GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
80 static GPrintFunc glib_print_func = NULL;
81 static GPrintFunc glib_printerr_func = NULL;
82 static GErrorFunc glib_error_func = NULL;
83 static GWarningFunc glib_warning_func = NULL;
84 static GPrintFunc glib_message_func = NULL;
86 static GPrivate* g_log_depth = NULL;
89 /* --- functions --- */
90 static inline GLogDomain*
91 g_log_find_domain (const gchar *log_domain)
93 register GLogDomain *domain;
95 g_mutex_lock (g_messages_lock);
96 domain = g_log_domains;
99 if (strcmp (domain->log_domain, log_domain) == 0)
101 g_mutex_unlock (g_messages_lock);
104 domain = domain->next;
106 g_mutex_unlock (g_messages_lock);
110 static inline GLogDomain*
111 g_log_domain_new (const gchar *log_domain)
113 register GLogDomain *domain;
115 domain = g_new (GLogDomain, 1);
116 domain->log_domain = g_strdup (log_domain);
117 domain->fatal_mask = G_LOG_FATAL_MASK;
118 domain->handlers = NULL;
120 g_mutex_lock (g_messages_lock);
121 domain->next = g_log_domains;
122 g_log_domains = domain;
123 g_mutex_unlock (g_messages_lock);
129 g_log_domain_check_free (GLogDomain *domain)
131 if (domain->fatal_mask == G_LOG_FATAL_MASK &&
132 domain->handlers == NULL)
134 register GLogDomain *last, *work;
138 g_mutex_lock (g_messages_lock);
139 work = g_log_domains;
145 last->next = domain->next;
147 g_log_domains = domain->next;
148 g_free (domain->log_domain);
154 g_mutex_unlock (g_messages_lock);
158 static inline GLogFunc
159 g_log_domain_get_handler (GLogDomain *domain,
160 GLogLevelFlags log_level,
163 if (domain && log_level)
165 register GLogHandler *handler;
167 handler = domain->handlers;
170 if ((handler->log_level & log_level) == log_level)
172 *data = handler->data;
173 return handler->log_func;
175 handler = handler->next;
178 return g_log_default_handler;
182 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
184 GLogLevelFlags old_mask;
186 /* restrict the global mask to levels that are known to glib */
187 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
188 /* force errors to be fatal */
189 fatal_mask |= G_LOG_LEVEL_ERROR;
190 /* remove bogus flag */
191 fatal_mask &= ~G_LOG_FLAG_FATAL;
193 g_mutex_lock (g_messages_lock);
194 old_mask = g_log_always_fatal;
195 g_log_always_fatal = fatal_mask;
196 g_mutex_unlock (g_messages_lock);
202 g_log_set_fatal_mask (const gchar *log_domain,
203 GLogLevelFlags fatal_mask)
205 GLogLevelFlags old_flags;
206 register GLogDomain *domain;
211 /* force errors to be fatal */
212 fatal_mask |= G_LOG_LEVEL_ERROR;
213 /* remove bogus flag */
214 fatal_mask &= ~G_LOG_FLAG_FATAL;
216 domain = g_log_find_domain (log_domain);
218 domain = g_log_domain_new (log_domain);
219 old_flags = domain->fatal_mask;
221 domain->fatal_mask = fatal_mask;
222 g_log_domain_check_free (domain);
228 g_log_set_handler (const gchar *log_domain,
229 GLogLevelFlags log_levels,
233 register GLogDomain *domain;
234 register GLogHandler *handler;
235 static guint handler_id = 0;
237 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
238 g_return_val_if_fail (log_func != NULL, 0);
243 domain = g_log_find_domain (log_domain);
245 domain = g_log_domain_new (log_domain);
247 handler = g_new (GLogHandler, 1);
248 g_mutex_lock (g_messages_lock);
249 handler->id = ++handler_id;
250 g_mutex_unlock (g_messages_lock);
251 handler->log_level = log_levels;
252 handler->log_func = log_func;
253 handler->data = user_data;
254 handler->next = domain->handlers;
255 domain->handlers = handler;
261 g_log_remove_handler (const gchar *log_domain,
264 register GLogDomain *domain;
266 g_return_if_fail (handler_id > 0);
271 domain = g_log_find_domain (log_domain);
274 register GLogHandler *work, *last;
277 work = domain->handlers;
280 if (work->id == handler_id)
283 last->next = work->next;
285 domain->handlers = work->next;
287 g_log_domain_check_free (domain);
293 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
299 g_logv (const gchar *log_domain,
300 GLogLevelFlags log_level,
308 log_level &= G_LOG_LEVEL_MASK;
312 /* we use a stack buffer of fixed size, because we might get called
315 G_VA_COPY (args2, args1);
316 if (g_printf_string_upper_bound (format, args1) < 1024)
317 vsprintf (buffer, format, args2);
320 /* since we might be out of memory, we can't use g_vsnprintf(). */
321 #ifdef HAVE_VSNPRINTF
322 vsnprintf (buffer, 1024, format, args2);
323 #else /* !HAVE_VSNPRINTF */
324 /* we are out of luck here */
325 strncpy (buffer, format, 1024);
326 #endif /* !HAVE_VSNPRINTF */
331 for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
333 register GLogLevelFlags test_level;
336 if (log_level & test_level)
338 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
341 gpointer data = NULL;
343 domain = g_log_find_domain (log_domain ? log_domain : "");
346 test_level |= G_LOG_FLAG_RECURSION;
349 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
351 g_mutex_lock (g_messages_lock);
352 if ((((domain ? domain->fatal_mask : G_LOG_FATAL_MASK) |
353 g_log_always_fatal) & test_level) != 0)
354 test_level |= G_LOG_FLAG_FATAL;
355 g_mutex_unlock (g_messages_lock);
357 log_func = g_log_domain_get_handler (domain, test_level, &data);
358 log_func (log_domain, test_level, buffer, data);
360 /* *domain can be cluttered now */
362 if (test_level & G_LOG_FLAG_FATAL)
366 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
372 g_log (const gchar *log_domain,
373 GLogLevelFlags log_level,
379 va_start (args, format);
380 g_logv (log_domain, log_level, format, args);
385 g_log_default_handler (const gchar *log_domain,
386 GLogLevelFlags log_level,
387 const gchar *message,
388 gpointer unused_data)
395 gboolean in_recursion;
397 GErrorFunc local_glib_error_func;
398 GWarningFunc local_glib_warning_func;
399 GPrintFunc local_glib_message_func;
401 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
402 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
403 log_level &= G_LOG_LEVEL_MASK;
406 message = "g_log_default_handler(): (NULL) message";
409 /* Use just stdout as stderr is hard to get redirected from the
414 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
417 g_mutex_lock (g_messages_lock);
418 local_glib_error_func = glib_error_func;
419 local_glib_warning_func = glib_warning_func;
420 local_glib_message_func = glib_message_func;
421 g_mutex_unlock (g_messages_lock);
425 case G_LOG_LEVEL_ERROR:
426 if (!log_domain && local_glib_error_func)
428 /* compatibility code */
429 local_glib_error_func (message);
432 /* use write(2) for output, in case we are out of memeory */
436 write (fd, log_domain, strlen (log_domain));
440 write (fd, "\n** ", 4);
442 write (fd, "ERROR (recursed) **: ", 21);
444 write (fd, "ERROR **: ", 10);
445 write (fd, message, strlen(message));
447 write (fd, "\naborting...\n", 13);
451 case G_LOG_LEVEL_CRITICAL:
455 write (fd, log_domain, strlen (log_domain));
459 write (fd, "\n** ", 4);
461 write (fd, "CRITICAL (recursed) **: ", 24);
463 write (fd, "CRITICAL **: ", 13);
464 write (fd, message, strlen(message));
466 write (fd, "\naborting...\n", 13);
470 case G_LOG_LEVEL_WARNING:
471 if (!log_domain && local_glib_warning_func)
473 /* compatibility code */
474 local_glib_warning_func (message);
480 write (fd, log_domain, strlen (log_domain));
484 write (fd, "\n** ", 4);
486 write (fd, "WARNING (recursed) **: ", 23);
488 write (fd, "WARNING **: ", 12);
489 write (fd, message, strlen(message));
491 write (fd, "\naborting...\n", 13);
495 case G_LOG_LEVEL_MESSAGE:
496 if (!log_domain && local_glib_message_func)
498 /* compatibility code */
499 local_glib_message_func (message);
504 write (fd, log_domain, strlen (log_domain));
508 write (fd, "Message (recursed): ", 20);
510 write (fd, "Message: ", 9);
511 write (fd, message, strlen(message));
513 write (fd, "\naborting...\n", 13);
517 case G_LOG_LEVEL_INFO:
520 write (fd, log_domain, strlen (log_domain));
524 write (fd, "INFO (recursed): ", 17);
526 write (fd, "INFO: ", 6);
527 write (fd, message, strlen(message));
529 write (fd, "\naborting...\n", 13);
533 case G_LOG_LEVEL_DEBUG:
536 write (fd, log_domain, strlen (log_domain));
540 write (fd, "DEBUG (recursed): ", 18);
542 write (fd, "DEBUG: ", 7);
543 write (fd, message, strlen(message));
545 write (fd, "\naborting...\n", 13);
550 /* we are used for a log level that is not defined by GLib itself,
551 * try to make the best out of it.
555 write (fd, log_domain, strlen (log_domain));
557 write (fd, "-LOG (recursed:", 15);
559 write (fd, "-LOG (", 6);
561 else if (in_recursion)
562 write (fd, "LOG (recursed:", 14);
564 write (fd, "LOG (", 5);
567 gchar string[] = "0x00): ";
568 gchar *p = string + 2;
571 i = g_bit_nth_msf (log_level, -1);
574 *p = '0' + (i & 0xf);
578 write (fd, string, 7);
581 write (fd, "): ", 3);
582 write (fd, message, strlen(message));
584 write (fd, "\naborting...\n", 13);
592 g_set_print_handler (GPrintFunc func)
594 GPrintFunc old_print_func;
596 g_mutex_lock (g_messages_lock);
597 old_print_func = glib_print_func;
598 glib_print_func = func;
599 g_mutex_unlock (g_messages_lock);
601 return old_print_func;
605 g_print (const gchar *format,
610 GPrintFunc local_glib_print_func;
612 g_return_if_fail (format != NULL);
614 va_start (args, format);
615 string = g_strdup_vprintf (format, args);
618 g_mutex_lock (g_messages_lock);
619 local_glib_print_func = glib_print_func;
620 g_mutex_unlock (g_messages_lock);
622 if (local_glib_print_func)
623 local_glib_print_func (string);
626 fputs (string, stdout);
633 g_set_printerr_handler (GPrintFunc func)
635 GPrintFunc old_printerr_func;
637 g_mutex_lock (g_messages_lock);
638 old_printerr_func = glib_printerr_func;
639 glib_printerr_func = func;
640 g_mutex_unlock (g_messages_lock);
642 return old_printerr_func;
646 g_printerr (const gchar *format,
651 GPrintFunc local_glib_printerr_func;
653 g_return_if_fail (format != NULL);
655 va_start (args, format);
656 string = g_strdup_vprintf (format, args);
659 g_mutex_lock (g_messages_lock);
660 local_glib_printerr_func = glib_printerr_func;
661 g_mutex_unlock (g_messages_lock);
663 if (local_glib_printerr_func)
664 local_glib_printerr_func (string);
667 fputs (string, stderr);
673 /* compatibility code */
675 g_set_error_handler (GErrorFunc func)
677 GErrorFunc old_error_func;
679 g_mutex_lock (g_messages_lock);
680 old_error_func = glib_error_func;
681 glib_error_func = func;
682 g_mutex_unlock (g_messages_lock);
684 return old_error_func;
687 /* compatibility code */
689 g_set_warning_handler (GWarningFunc func)
691 GWarningFunc old_warning_func;
693 g_mutex_lock (g_messages_lock);
694 old_warning_func = glib_warning_func;
695 glib_warning_func = func;
696 g_mutex_unlock (g_messages_lock);
698 return old_warning_func;
701 /* compatibility code */
703 g_set_message_handler (GPrintFunc func)
705 GPrintFunc old_message_func;
707 g_mutex_lock (g_messages_lock);
708 old_message_func = glib_message_func;
709 glib_message_func = func;
710 g_mutex_unlock (g_messages_lock);
712 return old_message_func;
716 g_messages_init (void)
718 g_messages_lock = g_mutex_new();
719 g_log_depth = g_private_new(NULL);