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 /* The current fatal levels, error is always fatal */
35 static GLogLevelFlags fatal
= G_LOG_LEVEL_ERROR
;
36 static GLogFunc default_log_func
;
37 static gpointer default_log_func_user_data
;
38 static GPrintFunc stdout_handler
, stderr_handler
;
40 static void default_stdout_handler (const gchar
*string
);
41 static void default_stderr_handler (const gchar
*string
);
43 static GAbortFunc internal_abort_func
;
46 g_assertion_disable_global (GAbortFunc abort_func
)
48 internal_abort_func
= abort_func
;
54 if (internal_abort_func
)
55 internal_abort_func ();
61 g_printv (const gchar
*format
, va_list args
)
66 ret
= g_vasprintf (&msg
, format
, args
);
71 stdout_handler
= default_stdout_handler
;
80 g_print (const gchar
*format
, ...)
83 va_start (args
, format
);
84 g_printv (format
, args
);
89 g_printf (gchar
const *format
, ...)
94 va_start (args
, format
);
95 ret
= g_printv (format
, args
);
102 g_printerr (const gchar
*format
, ...)
107 va_start (args
, format
);
108 if (g_vasprintf (&msg
, format
, args
) < 0) {
115 stderr_handler
= default_stderr_handler
;
117 stderr_handler (msg
);
122 g_log_set_always_fatal (GLogLevelFlags fatal_mask
)
124 GLogLevelFlags old_fatal
= fatal
;
132 g_log_set_fatal_mask (const gchar
*log_domain
, GLogLevelFlags fatal_mask
)
135 * Mono does not use a G_LOG_DOMAIN currently, so we just assume things are fatal
136 * if we decide to set G_LOG_DOMAIN (we probably should) we should implement
142 #define g_logstr monoeg_g_logstr
143 #define g_logv_nofree monoeg_g_logv_nofree
146 g_logstr (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*msg
)
148 if (!default_log_func
)
149 default_log_func
= g_log_default_handler
;
151 default_log_func (log_domain
, log_level
, msg
, default_log_func_user_data
);
155 g_logv_nofree (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*format
, va_list args
)
159 if (internal_abort_func
) {
160 g_async_safe_vprintf (format
, args
);
162 } else if (g_vasprintf (&msg
, format
, args
) < 0) {
166 g_logstr (log_domain
, log_level
, msg
);
171 g_logv (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*format
, va_list args
)
173 g_free (g_logv_nofree (log_domain
, log_level
, format
, args
));
177 g_log (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*format
, ...)
181 va_start (args
, format
);
182 g_logv (log_domain
, log_level
, format
, args
);
186 static char *failure_assertion
= NULL
;
189 g_get_assertion_message (void)
191 return failure_assertion
;
195 g_assertion_message (const gchar
*format
, ...)
199 va_start (args
, format
);
201 failure_assertion
= g_logv_nofree (G_LOG_DOMAIN
, G_LOG_LEVEL_ERROR
, format
, args
);
207 // Emscriptem emulates varargs, and fails to stack pack multiple outgoing varargs areas,
208 // so this function serves to remove varargs in its caller and conserve stack.
210 mono_assertion_message_disabled (const char *file
, int line
)
212 mono_assertion_message (file
, line
, "<disabled>");
215 // Emscriptem emulates varargs, and fails to stack pack multiple outgoing varargs areas,
216 // so this function serves to remove varargs in its caller and conserve stack.
218 mono_assertion_message (const char *file
, int line
, const char *condition
)
220 g_assertion_message ("* Assertion at %s:%d, condition `%s' not met\n", file
, line
, condition
);
223 // Emscriptem emulates varargs, and fails to stack pack multiple outgoing varargs areas,
224 // so this function serves to remove varargs in its caller and conserve stack.
226 mono_assertion_message_unreachable (const char *file
, int line
)
228 g_assertion_message ("* Assertion: should not be reached at %s:%d\n", file
, line
);
232 #include <android/log.h>
234 static android_LogPriority
235 to_android_priority (GLogLevelFlags log_level
)
237 switch (log_level
& G_LOG_LEVEL_MASK
)
239 case G_LOG_LEVEL_ERROR
: return ANDROID_LOG_FATAL
;
240 case G_LOG_LEVEL_CRITICAL
: return ANDROID_LOG_ERROR
;
241 case G_LOG_LEVEL_WARNING
: return ANDROID_LOG_WARN
;
242 case G_LOG_LEVEL_MESSAGE
: return ANDROID_LOG_INFO
;
243 case G_LOG_LEVEL_INFO
: return ANDROID_LOG_DEBUG
;
244 case G_LOG_LEVEL_DEBUG
: return ANDROID_LOG_VERBOSE
;
246 return ANDROID_LOG_UNKNOWN
;
249 #define LOG_MESSAGE_MAX_LEN 4096
252 android_log_line (gint log_priority
, const gchar
*log_domain
, const gchar
*log_message
, gint log_len
)
254 gchar log_buf
[LOG_MESSAGE_MAX_LEN
];
256 g_assert (log_len
<= LOG_MESSAGE_MAX_LEN
- 1);
258 /* If line is longer than LOG_MESSAGE_MAX_LEN - 1, then we simply cut it out. This is consistent with the previous behavior. */
259 strncpy (log_buf
, log_message
, log_len
);
260 log_buf
[log_len
] = '\0';
262 __android_log_write (log_priority
, log_domain
, log_buf
);
266 android_log (gint log_priority
, const gchar
*log_domain
, const gchar
*log_message
)
268 gint log_message_len
, log_message_p_len
;
269 const gchar
*log_message_p
;
271 log_message_len
= strlen (log_message
);
272 if (log_message_len
<= LOG_MESSAGE_MAX_LEN
) {
273 __android_log_write (log_priority
, log_domain
, log_message
);
277 for (log_message_p
= log_message
; log_message_p
< log_message
+ log_message_len
;) {
278 const gchar
*p
= strstr (log_message_p
, "\n");
280 /* There is no more "\n". */
281 android_log_line (log_priority
, log_domain
, log_message_p
, LOG_MESSAGE_MAX_LEN
- 1);
285 log_message_p_len
= p
- log_message_p
;
286 if (log_message_p_len
> LOG_MESSAGE_MAX_LEN
- 1)
287 log_message_p_len
= LOG_MESSAGE_MAX_LEN
- 1;
289 android_log_line (log_priority
, log_domain
, log_message_p
, log_message_p_len
);
291 /* Set `log_message_p` to the character right after "\n" */
292 log_message_p
= p
+ 1;
297 g_log_default_handler (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer unused_data
)
299 android_log (to_android_priority (log_level
), log_domain
, message
);
300 if (log_level
& fatal
)
305 default_stdout_handler (const gchar
*message
)
307 /* TODO: provide a proper app name */
308 android_log (ANDROID_LOG_ERROR
, "mono", message
);
312 default_stderr_handler (const gchar
*message
)
314 /* TODO: provide a proper app name */
315 android_log (ANDROID_LOG_ERROR
, "mono", message
);
319 #elif defined(HOST_IOS)
323 to_asl_priority (GLogLevelFlags log_level
)
325 switch (log_level
& G_LOG_LEVEL_MASK
)
327 case G_LOG_LEVEL_ERROR
: return ASL_LEVEL_CRIT
;
328 case G_LOG_LEVEL_CRITICAL
: return ASL_LEVEL_ERR
;
329 case G_LOG_LEVEL_WARNING
: return ASL_LEVEL_WARNING
;
330 case G_LOG_LEVEL_MESSAGE
: return ASL_LEVEL_NOTICE
;
331 case G_LOG_LEVEL_INFO
: return ASL_LEVEL_INFO
;
332 case G_LOG_LEVEL_DEBUG
: return ASL_LEVEL_DEBUG
;
334 return ASL_LEVEL_ERR
;
338 g_log_default_handler (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer unused_data
)
340 asl_log (NULL
, NULL
, to_asl_priority (log_level
), "%s", message
);
341 if (log_level
& fatal
)
346 default_stdout_handler (const gchar
*message
)
348 asl_log (NULL
, NULL
, ASL_LEVEL_WARNING
, "%s", message
);
352 default_stderr_handler (const gchar
*message
)
354 asl_log (NULL
, NULL
, ASL_LEVEL_WARNING
, "%s", message
);
360 g_log_default_handler (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer unused_data
)
362 FILE *target
= stdout
;
364 fprintf (target
, "%s%s%s\n",
365 log_domain
!= NULL
? log_domain
: "",
366 log_domain
!= NULL
? ": " : "",
369 if (log_level
& fatal
) {
377 default_stdout_handler (const gchar
*string
)
379 fprintf (stdout
, "%s", string
);
383 default_stderr_handler (const gchar
*string
)
385 fprintf (stderr
, "%s", string
);
391 g_log_set_default_handler (GLogFunc log_func
, gpointer user_data
)
393 GLogFunc old
= default_log_func
;
394 default_log_func
= log_func
;
395 default_log_func_user_data
= user_data
;
400 g_set_print_handler (GPrintFunc func
)
402 GPrintFunc old
= stdout_handler
;
403 stdout_handler
= func
;
408 g_set_printerr_handler (GPrintFunc func
)
410 GPrintFunc old
= stderr_handler
;
411 stderr_handler
= func
;