New tests.
[mono.git] / eglib / src / goutput.c
1 /*
2  * Output and debugging functions
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@novell.com)
6  *
7  * (C) 2006 Novell, Inc.
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <glib.h>
30
31 /* The current fatal levels, error is always fatal */
32 static GLogLevelFlags fatal = G_LOG_LEVEL_ERROR;
33
34 #if PLATFORM_ANDROID
35 #include <android/log.h>
36
37 static android_LogPriority
38 to_android_priority (GLogLevelFlags log_level)
39 {
40         switch (log_level & G_LOG_LEVEL_MASK)
41         {
42                 case G_LOG_LEVEL_ERROR:     return ANDROID_LOG_FATAL;
43                 case G_LOG_LEVEL_CRITICAL:  return ANDROID_LOG_ERROR;
44                 case G_LOG_LEVEL_WARNING:   return ANDROID_LOG_WARN;
45                 case G_LOG_LEVEL_MESSAGE:   return ANDROID_LOG_INFO;
46                 case G_LOG_LEVEL_INFO:      return ANDROID_LOG_DEBUG;
47                 case G_LOG_LEVEL_DEBUG:     return ANDROID_LOG_VERBOSE;
48         }
49         return ANDROID_LOG_UNKNOWN;
50 }
51
52 static void 
53 out_vfprintf (FILE *ignore, const gchar *format, va_list args)
54 {
55         /* TODO: provide a proper app name */
56         __android_log_vprint (ANDROID_LOG_ERROR, "mono", format, args);
57 }
58 #else
59 static void 
60 out_vfprintf (FILE *file, const gchar *format, va_list args)
61 {
62         vfprintf (file, format, args);
63 }
64 #endif
65
66 void
67 g_print (const gchar *format, ...)
68 {
69         va_list args;
70
71         va_start (args, format);
72
73         out_vfprintf (stdout, format, args);
74
75         va_end (args);
76 }
77
78 void
79 g_printerr (const gchar *format, ...)
80 {
81         va_list args;
82
83         va_start (args, format);
84
85         out_vfprintf (stderr, format, args);
86
87         va_end (args);
88 }
89
90 GLogLevelFlags
91 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
92 {
93         GLogLevelFlags old_fatal = fatal;
94
95         fatal |= fatal_mask;
96         
97         return old_fatal;
98 }
99
100 GLogLevelFlags
101 g_log_set_fatal_mask (const gchar *log_domain, GLogLevelFlags fatal_mask)
102 {
103         /*
104          * Mono does not use a G_LOG_DOMAIN currently, so we just assume things are fatal
105          * if we decide to set G_LOG_DOMAIN (we probably should) we should implement
106          * this.
107          */
108         return fatal_mask;
109 }
110
111 void
112 g_logv (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
113 {
114         char *msg;
115         
116         vasprintf (&msg, format, args);
117 #if PLATFORM_ANDROID
118         __android_log_print (to_android_priority (log_level), 
119                 /* TODO: provide a proper app name */
120                 "mono", "%s%s%s",
121                 log_domain != NULL ? log_domain : "",
122                 log_domain != NULL ? ": " : "",
123                 msg);
124 #else
125         printf ("%s%s%s",
126                 log_domain != NULL ? log_domain : "",
127                 log_domain != NULL ? ": " : "",
128                 msg);
129 #endif
130         free (msg);
131         if (log_level & fatal){
132                 fflush (stdout);
133                 fflush (stderr);
134                 abort ();
135         }
136 }
137
138 void
139 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
140 {
141         va_list args;
142
143         va_start (args, format);
144         g_logv (log_domain, log_level, format, args);
145         va_end (args);
146 }
147
148 void
149 g_assertion_message (const gchar *format, ...)
150 {
151         va_list args;
152
153         va_start (args, format);
154         g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
155         va_end (args);
156 }
157