[eglib] Add log/print redirection functions to eglib.
[mono.git] / eglib / src / goutput.c
1 /*
2  * Output and debugging functions
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@novell.com)
6  *
7  * (C) 2006 Novell, Inc.
8  * Copyright 2011 Xamarin Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29 #include <config.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <glib.h>
33
34 #include "vasprintf.h"
35
36 /* The current fatal levels, error is always fatal */
37 static GLogLevelFlags fatal = G_LOG_LEVEL_ERROR;
38 static GLogFunc default_log_func;
39 static gpointer default_log_func_user_data;
40 static GPrintFunc stdout_handler, stderr_handler;
41
42 static void default_stdout_handler (const gchar *string);
43 static void default_stderr_handler (const gchar *string);
44
45 void
46 g_print (const gchar *format, ...)
47 {
48         char *msg;
49         va_list args;
50
51         va_start (args, format);
52         if (vasprintf (&msg, format, args) < 0)
53                 return;
54         va_end (args);
55
56         if (!stdout_handler)
57                 stdout_handler = default_stdout_handler;
58
59         stdout_handler (msg);
60         free (msg);
61 }
62
63 void
64 g_printerr (const gchar *format, ...)
65 {
66         char *msg;
67         va_list args;
68
69         va_start (args, format);
70         if (vasprintf (&msg, format, args) < 0)
71                 return;
72         va_end (args);
73
74         if (!stderr_handler)
75                 stderr_handler = default_stderr_handler;
76
77         stdout_handler (msg);
78         free (msg);
79 }
80
81 GLogLevelFlags
82 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
83 {
84         GLogLevelFlags old_fatal = fatal;
85
86         fatal |= fatal_mask;
87         
88         return old_fatal;
89 }
90
91 GLogLevelFlags
92 g_log_set_fatal_mask (const gchar *log_domain, GLogLevelFlags fatal_mask)
93 {
94         /*
95          * Mono does not use a G_LOG_DOMAIN currently, so we just assume things are fatal
96          * if we decide to set G_LOG_DOMAIN (we probably should) we should implement
97          * this.
98          */
99         return fatal_mask;
100 }
101
102 void
103 g_logv (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
104 {
105         char *msg;
106
107         if (!default_log_func)
108                 default_log_func = g_log_default_handler;
109         
110         if (vasprintf (&msg, format, args) < 0)
111                 return;
112
113         default_log_func (log_domain, log_level, msg, default_log_func_user_data);
114         free (msg);
115 }
116
117 void
118 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
119 {
120         va_list args;
121
122         va_start (args, format);
123         g_logv (log_domain, log_level, format, args);
124         va_end (args);
125 }
126
127 void
128 g_assertion_message (const gchar *format, ...)
129 {
130         va_list args;
131
132         va_start (args, format);
133         g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
134         va_end (args);
135         abort ();
136 }
137
138
139 void
140 g_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data)
141 {
142         FILE *target = stdout;
143
144         fprintf (target, "%s%s%s\n",
145                 log_domain != NULL ? log_domain : "",
146                 log_domain != NULL ? ": " : "",
147                 message);
148
149         if (log_level & fatal) {
150                 fflush (stdout);
151                 fflush (stderr);
152                 abort ();
153         }
154 }
155
156 static void
157 default_stdout_handler (const gchar *string)
158 {
159         fprintf (stdout, "%s", string);
160 }
161
162 static void
163 default_stderr_handler (const gchar *string)
164 {
165         fprintf (stderr, "%s", string);
166 }
167
168
169 GLogFunc
170 g_log_set_default_handler (GLogFunc log_func, gpointer user_data)
171 {
172         GLogFunc old = default_log_func;
173         default_log_func = log_func;
174         default_log_func_user_data = user_data;
175         return old;
176 }
177
178 GPrintFunc
179 g_set_print_handler (GPrintFunc func)
180 {
181         GPrintFunc old = stdout_handler;
182         stdout_handler = func;
183         return old;
184 }
185
186 GPrintFunc
187 g_set_printerr_handler (GPrintFunc func)
188 {
189         GPrintFunc old = stderr_handler;
190         stdout_handler = func;
191         return old;
192 }
193