remove GenericVectorTests from rsp (#17366)
[mono-project.git] / mono / eglib / goutput.c
blob58ac92e3d3f16d1d3dbce69df6fd0aa8f9b4915c
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 void
61 g_printv (const gchar *format, va_list args)
63 char *msg;
65 if (g_vasprintf (&msg, format, args) < 0)
66 return;
68 if (!stdout_handler)
69 stdout_handler = default_stdout_handler;
71 stdout_handler (msg);
72 g_free (msg);
75 void
76 g_print (const gchar *format, ...)
78 va_list args;
79 va_start (args, format);
80 g_printv (format, args);
81 va_end (args);
84 void
85 g_printerr (const gchar *format, ...)
87 char *msg;
88 va_list args;
90 va_start (args, format);
91 if (g_vasprintf (&msg, format, args) < 0) {
92 va_end (args);
93 return;
95 va_end (args);
97 if (!stderr_handler)
98 stderr_handler = default_stderr_handler;
100 stderr_handler (msg);
101 g_free (msg);
104 GLogLevelFlags
105 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
107 GLogLevelFlags old_fatal = fatal;
109 fatal |= fatal_mask;
111 return old_fatal;
114 GLogLevelFlags
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
120 * this.
122 return fatal_mask;
125 #define g_logstr monoeg_g_logstr
126 #define g_logv_nofree monoeg_g_logv_nofree
128 static void
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);
137 static gchar*
138 g_logv_nofree (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
140 char *msg;
142 if (internal_abort_func) {
143 g_async_safe_vprintf (format, args);
144 return NULL;
145 } else if (g_vasprintf (&msg, format, args) < 0) {
146 return NULL;
149 g_logstr (log_domain, log_level, msg);
150 return msg;
153 void
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));
159 void
160 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
162 va_list args;
164 va_start (args, format);
165 g_logv (log_domain, log_level, format, args);
166 va_end (args);
169 static char *failure_assertion = NULL;
171 const char *
172 g_get_assertion_message (void)
174 return failure_assertion;
177 void
178 g_assertion_message (const gchar *format, ...)
180 va_list args;
182 va_start (args, format);
184 failure_assertion = g_logv_nofree (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
186 va_end (args);
187 exit (0);
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.
192 void
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.
200 void
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.
208 void
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);
214 #if HOST_ANDROID
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
234 static void
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);
248 static void
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);
257 return;
260 for (log_message_p = log_message; log_message_p < log_message + log_message_len;) {
261 const gchar *p = strstr (log_message_p, "\n");
262 if (p == NULL) {
263 /* There is no more "\n". */
264 android_log_line (log_priority, log_domain, log_message_p, LOG_MESSAGE_MAX_LEN - 1);
265 break;
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;
279 void
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)
284 g_assert_abort ();
287 static void
288 default_stdout_handler (const gchar *message)
290 /* TODO: provide a proper app name */
291 android_log (ANDROID_LOG_ERROR, "mono", message);
294 static void
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)
303 #include <asl.h>
305 static int
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;
320 void
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)
325 g_assert_abort ();
328 static void
329 default_stdout_handler (const gchar *message)
331 asl_log (NULL, NULL, ASL_LEVEL_WARNING, "%s", message);
334 static void
335 default_stderr_handler (const gchar *message)
337 asl_log (NULL, NULL, ASL_LEVEL_WARNING, "%s", message);
340 #else
342 void
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 ? ": " : "",
350 message);
352 if (log_level & fatal) {
353 fflush (stdout);
354 fflush (stderr);
355 g_assert_abort ();
359 static void
360 default_stdout_handler (const gchar *string)
362 fprintf (stdout, "%s", string);
365 static void
366 default_stderr_handler (const gchar *string)
368 fprintf (stderr, "%s", string);
371 #endif
373 GLogFunc
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;
379 return old;
382 GPrintFunc
383 g_set_print_handler (GPrintFunc func)
385 GPrintFunc old = stdout_handler;
386 stdout_handler = func;
387 return old;
390 GPrintFunc
391 g_set_printerr_handler (GPrintFunc func)
393 GPrintFunc old = stderr_handler;
394 stderr_handler = func;
395 return old;