2 * Output and debugging functions
5 * Miguel de Icaza (miguel@novell.com)
7 * (C) 2006 Novell, Inc.
8 * Copyright 2011 Xamarin Inc.
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:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
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.
34 #include "vasprintf.h"
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;
42 static void default_stdout_handler (const gchar *string);
43 static void default_stderr_handler (const gchar *string);
46 g_print (const gchar *format, ...)
51 va_start (args, format);
52 if (vasprintf (&msg, format, args) < 0)
57 stdout_handler = default_stdout_handler;
64 g_printerr (const gchar *format, ...)
69 va_start (args, format);
70 if (vasprintf (&msg, format, args) < 0)
75 stderr_handler = default_stderr_handler;
82 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
84 GLogLevelFlags old_fatal = fatal;
92 g_log_set_fatal_mask (const gchar *log_domain, GLogLevelFlags fatal_mask)
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
103 g_logv (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
107 if (!default_log_func)
108 default_log_func = g_log_default_handler;
110 if (vasprintf (&msg, format, args) < 0)
113 default_log_func (log_domain, log_level, msg, default_log_func_user_data);
118 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
122 va_start (args, format);
123 g_logv (log_domain, log_level, format, args);
128 g_assertion_message (const gchar *format, ...)
132 va_start (args, format);
133 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
140 g_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data)
142 FILE *target = stdout;
144 fprintf (target, "%s%s%s\n",
145 log_domain != NULL ? log_domain : "",
146 log_domain != NULL ? ": " : "",
149 if (log_level & fatal) {
157 default_stdout_handler (const gchar *string)
159 fprintf (stdout, "%s", string);
163 default_stderr_handler (const gchar *string)
165 fprintf (stderr, "%s", string);
170 g_log_set_default_handler (GLogFunc log_func, gpointer user_data)
172 GLogFunc old = default_log_func;
173 default_log_func = log_func;
174 default_log_func_user_data = user_data;
179 g_set_print_handler (GPrintFunc func)
181 GPrintFunc old = stdout_handler;
182 stdout_handler = func;
187 g_set_printerr_handler (GPrintFunc func)
189 GPrintFunc old = stderr_handler;
190 stdout_handler = func;