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);
139 #include <android/log.h>
141 static android_LogPriority
142 to_android_priority (GLogLevelFlags log_level)
144 switch (log_level & G_LOG_LEVEL_MASK)
146 case G_LOG_LEVEL_ERROR: return ANDROID_LOG_FATAL;
147 case G_LOG_LEVEL_CRITICAL: return ANDROID_LOG_ERROR;
148 case G_LOG_LEVEL_WARNING: return ANDROID_LOG_WARN;
149 case G_LOG_LEVEL_MESSAGE: return ANDROID_LOG_INFO;
150 case G_LOG_LEVEL_INFO: return ANDROID_LOG_DEBUG;
151 case G_LOG_LEVEL_DEBUG: return ANDROID_LOG_VERBOSE;
153 return ANDROID_LOG_UNKNOWN;
157 g_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data)
159 __android_log_write (to_android_priority (log_level), log_domain, message);
160 if (log_level & fatal)
165 default_stdout_handler (const gchar *message)
167 /* TODO: provide a proper app name */
168 __android_log_write (ANDROID_LOG_ERROR, "mono", message);
172 default_stderr_handler (const gchar *message)
174 /* TODO: provide a proper app name */
175 __android_log_write (ANDROID_LOG_ERROR, "mono", message);
179 #elif defined(HOST_IOS)
183 to_asl_priority (GLogLevelFlags log_level)
185 switch (log_level & G_LOG_LEVEL_MASK)
187 case G_LOG_LEVEL_ERROR: return ASL_LEVEL_CRIT;
188 case G_LOG_LEVEL_CRITICAL: return ASL_LEVEL_ERR;
189 case G_LOG_LEVEL_WARNING: return ASL_LEVEL_WARNING;
190 case G_LOG_LEVEL_MESSAGE: return ASL_LEVEL_NOTICE;
191 case G_LOG_LEVEL_INFO: return ASL_LEVEL_INFO;
192 case G_LOG_LEVEL_DEBUG: return ASL_LEVEL_DEBUG;
194 return ASL_LEVEL_ERR;
198 g_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data)
200 asl_log (NULL, NULL, to_asl_priority (log_level), "%s", message);
201 if (log_level & fatal)
206 default_stdout_handler (const gchar *message)
208 asl_log (NULL, NULL, ASL_LEVEL_WARNING, "%s", message);
212 default_stderr_handler (const gchar *message)
214 asl_log (NULL, NULL, ASL_LEVEL_WARNING, "%s", message);
220 g_log_default_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer unused_data)
222 FILE *target = stdout;
224 fprintf (target, "%s%s%s\n",
225 log_domain != NULL ? log_domain : "",
226 log_domain != NULL ? ": " : "",
229 if (log_level & fatal) {
237 default_stdout_handler (const gchar *string)
239 fprintf (stdout, "%s", string);
243 default_stderr_handler (const gchar *string)
245 fprintf (stderr, "%s", string);
251 g_log_set_default_handler (GLogFunc log_func, gpointer user_data)
253 GLogFunc old = default_log_func;
254 default_log_func = log_func;
255 default_log_func_user_data = user_data;
260 g_set_print_handler (GPrintFunc func)
262 GPrintFunc old = stdout_handler;
263 stdout_handler = func;
268 g_set_printerr_handler (GPrintFunc func)
270 GPrintFunc old = stderr_handler;
271 stderr_handler = func;