Initial revision
[platform/upstream/glib.git] / glib / gmessages.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "glib.h"
25
26 static GErrorFunc glib_error_func = NULL;
27 static GWarningFunc glib_warning_func = NULL;
28 static GPrintFunc glib_message_func = NULL;
29 static GPrintFunc glib_print_func = NULL;
30
31 extern char* g_vsprintf (const gchar *fmt, va_list *args, va_list *args2);
32
33 void
34 g_error (const gchar *format, ...)
35 {
36   va_list args, args2;
37   char *buf;
38   static gboolean errored = 0;
39
40   if (errored++)
41     {
42       write (2, "g_error: recursed!\n", 19);
43       return;
44     }
45   
46   va_start (args, format);
47   va_start (args2, format);
48   buf = g_vsprintf (format, &args, &args2);
49   va_end (args);
50   va_end (args2);
51
52   if (glib_error_func)
53     {
54       (* glib_error_func) (buf);
55     }
56   else
57     {
58       /* Use write() here because we might be out of memory */
59       write (2, "\n** ERROR **: ", 14);
60       write (2, buf, strlen(buf));
61       write (2, "\n", 1);
62     }
63
64   abort ();
65 }
66
67 void
68 g_warning (const gchar *format, ...)
69 {
70   va_list args, args2;
71   char *buf;
72
73   va_start (args, format);
74   va_start (args2, format);
75   buf = g_vsprintf (format, &args, &args2);
76   va_end (args);
77   va_end (args2);
78
79   if (glib_warning_func)
80     {
81       (* glib_warning_func) (buf);
82     }
83   else
84     {
85       fputs ("\n** WARNING **: ", stderr);
86       fputs (buf, stderr);
87       fputc ('\n', stderr);
88     }
89 }
90
91 void
92 g_message (const gchar *format, ...)
93 {
94   va_list args, args2;
95   char *buf;
96
97   va_start (args, format);
98   va_start (args2, format);
99   buf = g_vsprintf (format, &args, &args2);
100   va_end (args);
101   va_end (args2);
102
103   if (glib_message_func)
104     {
105       (* glib_message_func) (buf);
106     }
107   else
108     {
109       fputs ("message: ", stdout);
110       fputs (buf, stdout);
111       fputc ('\n', stdout);
112     }
113 }
114
115 void
116 g_print (const gchar *format, ...)
117 {
118   va_list args, args2;
119   char *buf;
120
121   va_start (args, format);
122   va_start (args2, format);
123   buf = g_vsprintf (format, &args, &args2);
124   va_end (args);
125   va_end (args2);
126
127   if (glib_print_func)
128     {
129       (* glib_print_func) (buf);
130     }
131   else
132     {
133       fputs (buf, stdout);
134     }
135 }
136
137 GErrorFunc
138 g_set_error_handler (GErrorFunc func)
139 {
140   GErrorFunc old_error_func;
141
142   old_error_func = glib_error_func;
143   glib_error_func = func;
144
145   return old_error_func;
146 }
147
148 GWarningFunc
149 g_set_warning_handler (GWarningFunc func)
150 {
151   GWarningFunc old_warning_func;
152
153   old_warning_func = glib_warning_func;
154   glib_warning_func = func;
155
156   return old_warning_func;
157 }
158
159 GPrintFunc
160 g_set_message_handler (GPrintFunc func)
161 {
162   GPrintFunc old_message_func;
163
164   old_message_func = glib_message_func;
165   glib_message_func = func;
166
167   return old_message_func;
168 }
169
170 GPrintFunc
171 g_set_print_handler (GPrintFunc func)
172 {
173   GPrintFunc old_print_func;
174
175   old_print_func = glib_print_func;
176   glib_print_func = func;
177   
178   return old_print_func;
179 }
180