Remove some #if __cplusplus wrt inlining. (#18891)
[mono-project.git] / mono / eglib / goutput.c
blobf9da72dfe57de8528e4f9503702ec322365082d2
1 /*
2 * Output and debugging functions
4 * Author:
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.
29 #include <config.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <glib.h>
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;
45 void
46 g_assertion_disable_global (GAbortFunc abort_func)
48 internal_abort_func = abort_func;
51 void
52 g_assert_abort (void)
54 if (internal_abort_func)
55 internal_abort_func ();
56 else
57 abort ();
60 gint
61 g_printv (const gchar *format, va_list args)
63 char *msg;
64 int ret;
66 ret = g_vasprintf (&msg, format, args);
67 if (ret < 0)
68 return -1;
70 if (!stdout_handler)
71 stdout_handler = default_stdout_handler;
73 stdout_handler (msg);
74 g_free (msg);
76 return ret;
79 void
80 g_print (const gchar *format, ...)
82 va_list args;
83 va_start (args, format);
84 g_printv (format, args);
85 va_end (args);
88 gint
89 g_printf (gchar const *format, ...)
91 va_list args;
92 gint ret;
94 va_start (args, format);
95 ret = g_printv (format, args);
96 va_end (args);
98 return ret;
101 void
102 g_printerr (const gchar *format, ...)
104 char *msg;
105 va_list args;
107 va_start (args, format);
108 if (g_vasprintf (&msg, format, args) < 0) {
109 va_end (args);
110 return;
112 va_end (args);
114 if (!stderr_handler)
115 stderr_handler = default_stderr_handler;
117 stderr_handler (msg);
118 g_free (msg);
121 GLogLevelFlags
122 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
124 GLogLevelFlags old_fatal = fatal;
126 fatal |= fatal_mask;
128 return old_fatal;
131 GLogLevelFlags
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
137 * this.
139 return fatal_mask;
142 #define g_logstr monoeg_g_logstr
143 #define g_logv_nofree monoeg_g_logv_nofree
145 static void
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);
154 static gchar*
155 g_logv_nofree (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
157 char *msg;
159 if (internal_abort_func) {
160 g_async_safe_vprintf (format, args);
161 return NULL;
162 } else if (g_vasprintf (&msg, format, args) < 0) {
163 return NULL;
166 g_logstr (log_domain, log_level, msg);
167 return msg;
170 void
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));
176 void
177 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
179 va_list args;
181 va_start (args, format);
182 g_logv (log_domain, log_level, format, args);
183 va_end (args);
186 static char *failure_assertion = NULL;
188 const char *
189 g_get_assertion_message (void)
191 return failure_assertion;
194 void
195 g_assertion_message (const gchar *format, ...)
197 va_list args;
199 va_start (args, format);
201 failure_assertion = g_logv_nofree (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
203 va_end (args);
204 exit (0);
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.
209 void
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.
217 void
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.
225 void
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);
231 #if HOST_ANDROID
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
251 static void
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);
265 static void
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);
274 return;
277 for (log_message_p = log_message; log_message_p < log_message + log_message_len;) {
278 const gchar *p = strstr (log_message_p, "\n");
279 if (p == NULL) {
280 /* There is no more "\n". */
281 android_log_line (log_priority, log_domain, log_message_p, LOG_MESSAGE_MAX_LEN - 1);
282 break;
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;
296 void
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)
301 g_assert_abort ();
304 static void
305 default_stdout_handler (const gchar *message)
307 /* TODO: provide a proper app name */
308 android_log (ANDROID_LOG_ERROR, "mono", message);
311 static void
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)
320 #include <asl.h>
322 static int
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;
337 void
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)
342 g_assert_abort ();
345 static void
346 default_stdout_handler (const gchar *message)
348 asl_log (NULL, NULL, ASL_LEVEL_WARNING, "%s", message);
351 static void
352 default_stderr_handler (const gchar *message)
354 asl_log (NULL, NULL, ASL_LEVEL_WARNING, "%s", message);
357 #else
359 void
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 ? ": " : "",
367 message);
369 if (log_level & fatal) {
370 fflush (stdout);
371 fflush (stderr);
372 g_assert_abort ();
376 static void
377 default_stdout_handler (const gchar *string)
379 fprintf (stdout, "%s", string);
382 static void
383 default_stderr_handler (const gchar *string)
385 fprintf (stderr, "%s", string);
388 #endif
390 GLogFunc
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;
396 return old;
399 GPrintFunc
400 g_set_print_handler (GPrintFunc func)
402 GPrintFunc old = stdout_handler;
403 stdout_handler = func;
404 return old;
407 GPrintFunc
408 g_set_printerr_handler (GPrintFunc func)
410 GPrintFunc old = stderr_handler;
411 stderr_handler = func;
412 return old;