cosmetics
[tomato.git] / release / src / router / glib / gmessages.c
blob9c75c4de6a4a30e17c07ca235ee11cb56c37bc2d
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/.
27 /*
28 * MT safe
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "glib.h"
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #ifdef NATIVE_WIN32
45 #define STRICT
46 #include <windows.h>
48 /* Just use stdio. If we're out of memory, we're hosed anyway. */
49 #undef write
51 static inline int
52 write (FILE *fd,
53 const char *buf,
54 int len)
56 fwrite (buf, len, 1, fd);
58 return len;
61 static void
62 ensure_stdout_valid (void)
64 HANDLE handle;
66 handle = GetStdHandle (STD_OUTPUT_HANDLE);
68 if (handle == INVALID_HANDLE_VALUE)
70 AllocConsole ();
71 freopen ("CONOUT$", "w", stdout);
74 #else
75 #define ensure_stdout_valid() /* Define as empty */
76 #endif
79 /* --- structures --- */
80 typedef struct _GLogDomain GLogDomain;
81 typedef struct _GLogHandler GLogHandler;
82 struct _GLogDomain
84 gchar *log_domain;
85 GLogLevelFlags fatal_mask;
86 GLogHandler *handlers;
87 GLogDomain *next;
89 struct _GLogHandler
91 guint id;
92 GLogLevelFlags log_level;
93 GLogFunc log_func;
94 gpointer data;
95 GLogHandler *next;
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;
123 while (domain)
125 if (strcmp (domain->log_domain, log_domain) == 0)
127 g_mutex_unlock (g_messages_lock);
128 return domain;
130 domain = domain->next;
132 g_mutex_unlock (g_messages_lock);
133 return NULL;
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);
151 return domain;
154 static inline void
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;
162 last = NULL;
164 g_mutex_lock (g_messages_lock);
165 work = g_log_domains;
166 while (work)
168 if (work == domain)
170 if (last)
171 last->next = domain->next;
172 else
173 g_log_domains = domain->next;
174 g_free (domain->log_domain);
175 g_free (domain);
176 break;
178 last = work;
179 work = last->next;
181 g_mutex_unlock (g_messages_lock);
185 static inline GLogFunc
186 g_log_domain_get_handler (GLogDomain *domain,
187 GLogLevelFlags log_level,
188 gpointer *data)
190 if (domain && log_level)
192 register GLogHandler *handler;
194 handler = domain->handlers;
195 while (handler)
197 if ((handler->log_level & log_level) == log_level)
199 *data = handler->data;
200 return handler->log_func;
202 handler = handler->next;
205 return g_log_default_handler;
208 GLogLevelFlags
209 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
211 GLogLevelFlags old_mask;
213 /* restrict the global mask to levels that are known to glib */
214 fatal_mask &= (1 << G_LOG_LEVEL_USER_SHIFT) - 1;
215 /* force errors to be fatal */
216 fatal_mask |= G_LOG_LEVEL_ERROR;
217 /* remove bogus flag */
218 fatal_mask &= ~G_LOG_FLAG_FATAL;
220 g_mutex_lock (g_messages_lock);
221 old_mask = g_log_always_fatal;
222 g_log_always_fatal = fatal_mask;
223 g_mutex_unlock (g_messages_lock);
225 return old_mask;
228 GLogLevelFlags
229 g_log_set_fatal_mask (const gchar *log_domain,
230 GLogLevelFlags fatal_mask)
232 GLogLevelFlags old_flags;
233 register GLogDomain *domain;
235 if (!log_domain)
236 log_domain = "";
238 /* force errors to be fatal */
239 fatal_mask |= G_LOG_LEVEL_ERROR;
240 /* remove bogus flag */
241 fatal_mask &= ~G_LOG_FLAG_FATAL;
243 domain = g_log_find_domain (log_domain);
244 if (!domain)
245 domain = g_log_domain_new (log_domain);
246 old_flags = domain->fatal_mask;
248 domain->fatal_mask = fatal_mask;
249 g_log_domain_check_free (domain);
251 return old_flags;
254 guint
255 g_log_set_handler (const gchar *log_domain,
256 GLogLevelFlags log_levels,
257 GLogFunc log_func,
258 gpointer user_data)
260 register GLogDomain *domain;
261 register GLogHandler *handler;
262 static guint handler_id = 0;
264 g_return_val_if_fail ((log_levels & G_LOG_LEVEL_MASK) != 0, 0);
265 g_return_val_if_fail (log_func != NULL, 0);
267 if (!log_domain)
268 log_domain = "";
270 domain = g_log_find_domain (log_domain);
271 if (!domain)
272 domain = g_log_domain_new (log_domain);
274 handler = g_new (GLogHandler, 1);
275 g_mutex_lock (g_messages_lock);
276 handler->id = ++handler_id;
277 g_mutex_unlock (g_messages_lock);
278 handler->log_level = log_levels;
279 handler->log_func = log_func;
280 handler->data = user_data;
281 handler->next = domain->handlers;
282 domain->handlers = handler;
284 return handler_id;
287 void
288 g_log_remove_handler (const gchar *log_domain,
289 guint handler_id)
291 register GLogDomain *domain;
293 g_return_if_fail (handler_id > 0);
295 if (!log_domain)
296 log_domain = "";
298 domain = g_log_find_domain (log_domain);
299 if (domain)
301 register GLogHandler *work, *last;
303 last = NULL;
304 work = domain->handlers;
305 while (work)
307 if (work->id == handler_id)
309 if (last)
310 last->next = work->next;
311 else
312 domain->handlers = work->next;
313 g_free (work);
314 g_log_domain_check_free (domain);
315 return;
317 last = work;
318 work = last->next;
321 g_warning ("g_log_remove_handler(): could not find handler with id `%d' for domain \"%s\"",
322 handler_id,
323 log_domain);
326 void
327 g_logv (const gchar *log_domain,
328 GLogLevelFlags log_level,
329 const gchar *format,
330 va_list args1)
332 va_list args2;
333 gchar buffer[1025];
334 register gint i;
336 log_level &= G_LOG_LEVEL_MASK;
337 if (!log_level)
338 return;
340 /* we use a stack buffer of fixed size, because we might get called
341 * recursively.
343 G_VA_COPY (args2, args1);
344 if (g_printf_string_upper_bound (format, args1) < 1024)
345 vsprintf (buffer, format, args2);
346 else
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 */
355 buffer[1024] = 0;
357 va_end (args2);
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;
363 test_level = 1 << i;
364 if (log_level & test_level)
366 guint depth = GPOINTER_TO_UINT (g_private_get (g_log_depth));
367 GLogDomain *domain;
368 GLogFunc log_func;
369 gpointer data = NULL;
371 domain = g_log_find_domain (log_domain ? log_domain : "");
373 if (depth)
374 test_level |= G_LOG_FLAG_RECURSION;
376 depth++;
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)
391 abort ();
393 depth--;
394 g_private_set (g_log_depth, GUINT_TO_POINTER (depth));
399 void
400 g_log (const gchar *log_domain,
401 GLogLevelFlags log_level,
402 const gchar *format,
403 ...)
405 va_list args;
407 va_start (args, format);
408 g_logv (log_domain, log_level, format, args);
409 va_end (args);
412 void
413 g_log_default_handler (const gchar *log_domain,
414 GLogLevelFlags log_level,
415 const gchar *message,
416 gpointer unused_data)
418 #ifdef NATIVE_WIN32
419 FILE *fd;
420 #else
421 gint fd;
422 #endif
423 gboolean in_recursion;
424 gboolean is_fatal;
425 GErrorFunc local_glib_error_func;
426 GWarningFunc local_glib_warning_func;
427 GPrintFunc local_glib_message_func;
429 in_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
430 is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
431 log_level &= G_LOG_LEVEL_MASK;
433 if (!message)
434 message = "g_log_default_handler(): (NULL) message";
436 #ifdef NATIVE_WIN32
437 /* Use just stdout as stderr is hard to get redirected from the
438 * DOS prompt.
440 fd = stdout;
441 #else
442 fd = (log_level >= G_LOG_LEVEL_MESSAGE) ? 1 : 2;
443 #endif
445 g_mutex_lock (g_messages_lock);
446 local_glib_error_func = glib_error_func;
447 local_glib_warning_func = glib_warning_func;
448 local_glib_message_func = glib_message_func;
449 g_mutex_unlock (g_messages_lock);
451 switch (log_level)
453 case G_LOG_LEVEL_ERROR:
454 if (!log_domain && local_glib_error_func)
456 /* compatibility code */
457 local_glib_error_func (message);
458 return;
460 /* use write(2) for output, in case we are out of memeory */
461 ensure_stdout_valid ();
462 if (log_domain)
464 write (fd, "\n", 1);
465 write (fd, log_domain, strlen (log_domain));
466 write (fd, "-", 1);
468 else
469 write (fd, "\n** ", 4);
470 if (in_recursion)
471 write (fd, "ERROR (recursed) **: ", 21);
472 else
473 write (fd, "ERROR **: ", 10);
474 write (fd, message, strlen(message));
475 if (is_fatal)
476 write (fd, "\naborting...\n", 13);
477 else
478 write (fd, "\n", 1);
479 break;
480 case G_LOG_LEVEL_CRITICAL:
481 ensure_stdout_valid ();
482 if (log_domain)
484 write (fd, "\n", 1);
485 write (fd, log_domain, strlen (log_domain));
486 write (fd, "-", 1);
488 else
489 write (fd, "\n** ", 4);
490 if (in_recursion)
491 write (fd, "CRITICAL (recursed) **: ", 24);
492 else
493 write (fd, "CRITICAL **: ", 13);
494 write (fd, message, strlen(message));
495 if (is_fatal)
496 write (fd, "\naborting...\n", 13);
497 else
498 write (fd, "\n", 1);
499 break;
500 case G_LOG_LEVEL_WARNING:
501 if (!log_domain && local_glib_warning_func)
503 /* compatibility code */
504 local_glib_warning_func (message);
505 return;
507 ensure_stdout_valid ();
508 if (log_domain)
510 write (fd, "\n", 1);
511 write (fd, log_domain, strlen (log_domain));
512 write (fd, "-", 1);
514 else
515 write (fd, "\n** ", 4);
516 if (in_recursion)
517 write (fd, "WARNING (recursed) **: ", 23);
518 else
519 write (fd, "WARNING **: ", 12);
520 write (fd, message, strlen(message));
521 if (is_fatal)
522 write (fd, "\naborting...\n", 13);
523 else
524 write (fd, "\n", 1);
525 break;
526 case G_LOG_LEVEL_MESSAGE:
527 if (!log_domain && local_glib_message_func)
529 /* compatibility code */
530 local_glib_message_func (message);
531 return;
533 ensure_stdout_valid ();
534 if (log_domain)
536 write (fd, log_domain, strlen (log_domain));
537 write (fd, "-", 1);
539 if (in_recursion)
540 write (fd, "Message (recursed): ", 20);
541 else
542 write (fd, "Message: ", 9);
543 write (fd, message, strlen(message));
544 if (is_fatal)
545 write (fd, "\naborting...\n", 13);
546 else
547 write (fd, "\n", 1);
548 break;
549 case G_LOG_LEVEL_INFO:
550 ensure_stdout_valid ();
551 if (log_domain)
553 write (fd, log_domain, strlen (log_domain));
554 write (fd, "-", 1);
556 if (in_recursion)
557 write (fd, "INFO (recursed): ", 17);
558 else
559 write (fd, "INFO: ", 6);
560 write (fd, message, strlen(message));
561 if (is_fatal)
562 write (fd, "\naborting...\n", 13);
563 else
564 write (fd, "\n", 1);
565 break;
566 case G_LOG_LEVEL_DEBUG:
567 ensure_stdout_valid ();
568 if (log_domain)
570 write (fd, log_domain, strlen (log_domain));
571 write (fd, "-", 1);
573 if (in_recursion)
574 write (fd, "DEBUG (recursed): ", 18);
575 else
576 write (fd, "DEBUG: ", 7);
577 write (fd, message, strlen(message));
578 if (is_fatal)
579 write (fd, "\naborting...\n", 13);
580 else
581 write (fd, "\n", 1);
582 break;
583 default:
584 /* we are used for a log level that is not defined by GLib itself,
585 * try to make the best out of it.
587 ensure_stdout_valid ();
588 if (log_domain)
590 write (fd, log_domain, strlen (log_domain));
591 if (in_recursion)
592 write (fd, "-LOG (recursed:", 15);
593 else
594 write (fd, "-LOG (", 6);
596 else if (in_recursion)
597 write (fd, "LOG (recursed:", 14);
598 else
599 write (fd, "LOG (", 5);
600 if (log_level)
602 gchar string[] = "0x00): ";
603 gchar *p = string + 2;
604 guint i;
606 i = g_bit_nth_msf (log_level, -1);
607 *p = i >> 4;
608 p++;
609 *p = '0' + (i & 0xf);
610 if (*p > '9')
611 *p += 'A' - '9' - 1;
613 write (fd, string, 7);
615 else
616 write (fd, "): ", 3);
617 write (fd, message, strlen(message));
618 if (is_fatal)
619 write (fd, "\naborting...\n", 13);
620 else
621 write (fd, "\n", 1);
622 break;
626 GPrintFunc
627 g_set_print_handler (GPrintFunc func)
629 GPrintFunc old_print_func;
631 g_mutex_lock (g_messages_lock);
632 old_print_func = glib_print_func;
633 glib_print_func = func;
634 g_mutex_unlock (g_messages_lock);
636 return old_print_func;
639 void
640 g_print (const gchar *format,
641 ...)
643 va_list args;
644 gchar *string;
645 GPrintFunc local_glib_print_func;
647 g_return_if_fail (format != NULL);
649 va_start (args, format);
650 string = g_strdup_vprintf (format, args);
651 va_end (args);
653 g_mutex_lock (g_messages_lock);
654 local_glib_print_func = glib_print_func;
655 g_mutex_unlock (g_messages_lock);
657 if (local_glib_print_func)
658 local_glib_print_func (string);
659 else
661 ensure_stdout_valid ();
662 fputs (string, stdout);
663 fflush (stdout);
665 g_free (string);
668 GPrintFunc
669 g_set_printerr_handler (GPrintFunc func)
671 GPrintFunc old_printerr_func;
673 g_mutex_lock (g_messages_lock);
674 old_printerr_func = glib_printerr_func;
675 glib_printerr_func = func;
676 g_mutex_unlock (g_messages_lock);
678 return old_printerr_func;
681 void
682 g_printerr (const gchar *format,
683 ...)
685 va_list args;
686 gchar *string;
687 GPrintFunc local_glib_printerr_func;
689 g_return_if_fail (format != NULL);
691 va_start (args, format);
692 string = g_strdup_vprintf (format, args);
693 va_end (args);
695 g_mutex_lock (g_messages_lock);
696 local_glib_printerr_func = glib_printerr_func;
697 g_mutex_unlock (g_messages_lock);
699 if (local_glib_printerr_func)
700 local_glib_printerr_func (string);
701 else
703 fputs (string, stderr);
704 fflush (stderr);
706 g_free (string);
709 /* compatibility code */
710 GErrorFunc
711 g_set_error_handler (GErrorFunc func)
713 GErrorFunc old_error_func;
715 g_mutex_lock (g_messages_lock);
716 old_error_func = glib_error_func;
717 glib_error_func = func;
718 g_mutex_unlock (g_messages_lock);
720 return old_error_func;
723 /* compatibility code */
724 GWarningFunc
725 g_set_warning_handler (GWarningFunc func)
727 GWarningFunc old_warning_func;
729 g_mutex_lock (g_messages_lock);
730 old_warning_func = glib_warning_func;
731 glib_warning_func = func;
732 g_mutex_unlock (g_messages_lock);
734 return old_warning_func;
737 /* compatibility code */
738 GPrintFunc
739 g_set_message_handler (GPrintFunc func)
741 GPrintFunc old_message_func;
743 g_mutex_lock (g_messages_lock);
744 old_message_func = glib_message_func;
745 glib_message_func = func;
746 g_mutex_unlock (g_messages_lock);
748 return old_message_func;
751 void
752 g_messages_init (void)
754 g_messages_lock = g_mutex_new();
755 g_log_depth = g_private_new(NULL);