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
)
65 if (g_vasprintf (&msg
, format
, args
) < 0)
69 stdout_handler
= default_stdout_handler
;
76 g_print (const gchar
*format
, ...)
79 va_start (args
, format
);
80 g_printv (format
, args
);
85 g_printerr (const gchar
*format
, ...)
90 va_start (args
, format
);
91 if (g_vasprintf (&msg
, format
, args
) < 0) {
98 stderr_handler
= default_stderr_handler
;
100 stderr_handler (msg
);
105 g_log_set_always_fatal (GLogLevelFlags fatal_mask
)
107 GLogLevelFlags old_fatal
= fatal
;
115 g_log_set_fatal_mask (const gchar
*log_domain
, GLogLevelFlags fatal_mask
)
118 * Mono does not use a G_LOG_DOMAIN currently, so we just assume things are fatal
119 * if we decide to set G_LOG_DOMAIN (we probably should) we should implement
125 #define g_logstr monoeg_g_logstr
126 #define g_logv_nofree monoeg_g_logv_nofree
129 g_logstr (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*msg
)
131 if (!default_log_func
)
132 default_log_func
= g_log_default_handler
;
134 default_log_func (log_domain
, log_level
, msg
, default_log_func_user_data
);
138 g_logv_nofree (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*format
, va_list args
)
142 if (internal_abort_func
) {
143 g_async_safe_vprintf (format
, args
);
145 } else if (g_vasprintf (&msg
, format
, args
) < 0) {
149 g_logstr (log_domain
, log_level
, msg
);
154 g_logv (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*format
, va_list args
)
156 g_free (g_logv_nofree (log_domain
, log_level
, format
, args
));
160 g_log (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*format
, ...)
164 va_start (args
, format
);
165 g_logv (log_domain
, log_level
, format
, args
);
169 static char *failure_assertion
= NULL
;
172 g_get_assertion_message (void)
174 return failure_assertion
;
178 g_assertion_message (const gchar
*format
, ...)
182 va_start (args
, format
);
184 failure_assertion
= g_logv_nofree (G_LOG_DOMAIN
, G_LOG_LEVEL_ERROR
, format
, args
);
190 // Emscriptem emulates varargs, and fails to stack pack multiple outgoing varargs areas,
191 // so this function serves to remove varargs in its caller and conserve stack.
193 mono_assertion_message_disabled (const char *file
, int line
)
195 mono_assertion_message (file
, line
, "<disabled>");
198 // Emscriptem emulates varargs, and fails to stack pack multiple outgoing varargs areas,
199 // so this function serves to remove varargs in its caller and conserve stack.
201 mono_assertion_message (const char *file
, int line
, const char *condition
)
203 g_assertion_message ("* Assertion at %s:%d, condition `%s' not met\n", file
, line
, condition
);
206 // Emscriptem emulates varargs, and fails to stack pack multiple outgoing varargs areas,
207 // so this function serves to remove varargs in its caller and conserve stack.
209 mono_assertion_message_unreachable (const char *file
, int line
)
211 g_assertion_message ("* Assertion: should not be reached at %s:%d\n", file
, line
);
215 #include <android/log.h>
217 static android_LogPriority
218 to_android_priority (GLogLevelFlags log_level
)
220 switch (log_level
& G_LOG_LEVEL_MASK
)
222 case G_LOG_LEVEL_ERROR
: return ANDROID_LOG_FATAL
;
223 case G_LOG_LEVEL_CRITICAL
: return ANDROID_LOG_ERROR
;
224 case G_LOG_LEVEL_WARNING
: return ANDROID_LOG_WARN
;
225 case G_LOG_LEVEL_MESSAGE
: return ANDROID_LOG_INFO
;
226 case G_LOG_LEVEL_INFO
: return ANDROID_LOG_DEBUG
;
227 case G_LOG_LEVEL_DEBUG
: return ANDROID_LOG_VERBOSE
;
229 return ANDROID_LOG_UNKNOWN
;
232 #define LOG_MESSAGE_MAX_LEN 4096
235 android_log_line (gint log_priority
, const gchar
*log_domain
, const gchar
*log_message
, gint log_len
)
237 gchar log_buf
[LOG_MESSAGE_MAX_LEN
];
239 g_assert (log_len
<= LOG_MESSAGE_MAX_LEN
- 1);
241 /* If line is longer than LOG_MESSAGE_MAX_LEN - 1, then we simply cut it out. This is consistent with the previous behavior. */
242 strncpy (log_buf
, log_message
, log_len
);
243 log_buf
[log_len
] = '\0';
245 __android_log_write (log_priority
, log_domain
, log_buf
);
249 android_log (gint log_priority
, const gchar
*log_domain
, const gchar
*log_message
)
251 gint log_message_len
, log_message_p_len
;
252 const gchar
*log_message_p
;
254 log_message_len
= strlen (log_message
);
255 if (log_message_len
<= LOG_MESSAGE_MAX_LEN
) {
256 __android_log_write (log_priority
, log_domain
, log_message
);
260 for (log_message_p
= log_message
; log_message_p
< log_message
+ log_message_len
;) {
261 const gchar
*p
= strstr (log_message_p
, "\n");
263 /* There is no more "\n". */
264 android_log_line (log_priority
, log_domain
, log_message_p
, LOG_MESSAGE_MAX_LEN
- 1);
268 log_message_p_len
= p
- log_message_p
;
269 if (log_message_p_len
> LOG_MESSAGE_MAX_LEN
- 1)
270 log_message_p_len
= LOG_MESSAGE_MAX_LEN
- 1;
272 android_log_line (log_priority
, log_domain
, log_message_p
, log_message_p_len
);
274 /* Set `log_message_p` to the character right after "\n" */
275 log_message_p
= p
+ 1;
280 g_log_default_handler (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer unused_data
)
282 android_log (to_android_priority (log_level
), log_domain
, message
);
283 if (log_level
& fatal
)
288 default_stdout_handler (const gchar
*message
)
290 /* TODO: provide a proper app name */
291 android_log (ANDROID_LOG_ERROR
, "mono", message
);
295 default_stderr_handler (const gchar
*message
)
297 /* TODO: provide a proper app name */
298 android_log (ANDROID_LOG_ERROR
, "mono", message
);
302 #elif defined(HOST_IOS)
306 to_asl_priority (GLogLevelFlags log_level
)
308 switch (log_level
& G_LOG_LEVEL_MASK
)
310 case G_LOG_LEVEL_ERROR
: return ASL_LEVEL_CRIT
;
311 case G_LOG_LEVEL_CRITICAL
: return ASL_LEVEL_ERR
;
312 case G_LOG_LEVEL_WARNING
: return ASL_LEVEL_WARNING
;
313 case G_LOG_LEVEL_MESSAGE
: return ASL_LEVEL_NOTICE
;
314 case G_LOG_LEVEL_INFO
: return ASL_LEVEL_INFO
;
315 case G_LOG_LEVEL_DEBUG
: return ASL_LEVEL_DEBUG
;
317 return ASL_LEVEL_ERR
;
321 g_log_default_handler (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer unused_data
)
323 asl_log (NULL
, NULL
, to_asl_priority (log_level
), "%s", message
);
324 if (log_level
& fatal
)
329 default_stdout_handler (const gchar
*message
)
331 asl_log (NULL
, NULL
, ASL_LEVEL_WARNING
, "%s", message
);
335 default_stderr_handler (const gchar
*message
)
337 asl_log (NULL
, NULL
, ASL_LEVEL_WARNING
, "%s", message
);
343 g_log_default_handler (const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer unused_data
)
345 FILE *target
= stdout
;
347 fprintf (target
, "%s%s%s\n",
348 log_domain
!= NULL
? log_domain
: "",
349 log_domain
!= NULL
? ": " : "",
352 if (log_level
& fatal
) {
360 default_stdout_handler (const gchar
*string
)
362 fprintf (stdout
, "%s", string
);
366 default_stderr_handler (const gchar
*string
)
368 fprintf (stderr
, "%s", string
);
374 g_log_set_default_handler (GLogFunc log_func
, gpointer user_data
)
376 GLogFunc old
= default_log_func
;
377 default_log_func
= log_func
;
378 default_log_func_user_data
= user_data
;
383 g_set_print_handler (GPrintFunc func
)
385 GPrintFunc old
= stdout_handler
;
386 stdout_handler
= func
;
391 g_set_printerr_handler (GPrintFunc func
)
393 GPrintFunc old
= stderr_handler
;
394 stderr_handler
= func
;