Merge pull request #1066 from esdrubal/bug19313
[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  * Copyright 2011 Xamarin Inc.
9  *
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:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
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.
28  */
29 #include <config.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <glib.h>
33
34 #include "vasprintf.h"
35
36 /* The current fatal levels, error is always fatal */
37 static GLogLevelFlags fatal = G_LOG_LEVEL_ERROR;
38
39 #if PLATFORM_ANDROID
40 #include <android/log.h>
41
42 static android_LogPriority
43 to_android_priority (GLogLevelFlags log_level)
44 {
45         switch (log_level & G_LOG_LEVEL_MASK)
46         {
47                 case G_LOG_LEVEL_ERROR:     return ANDROID_LOG_FATAL;
48                 case G_LOG_LEVEL_CRITICAL:  return ANDROID_LOG_ERROR;
49                 case G_LOG_LEVEL_WARNING:   return ANDROID_LOG_WARN;
50                 case G_LOG_LEVEL_MESSAGE:   return ANDROID_LOG_INFO;
51                 case G_LOG_LEVEL_INFO:      return ANDROID_LOG_DEBUG;
52                 case G_LOG_LEVEL_DEBUG:     return ANDROID_LOG_VERBOSE;
53         }
54         return ANDROID_LOG_UNKNOWN;
55 }
56
57 static void 
58 out_vfprintf (FILE *ignore, const gchar *format, va_list args)
59 {
60         /* TODO: provide a proper app name */
61         __android_log_vprint (ANDROID_LOG_ERROR, "mono", format, args);
62 }
63 #elif MONOTOUCH && defined(__arm__)
64 #include <asl.h>
65
66 static int
67 to_asl_priority (GLogLevelFlags log_level)
68 {
69         switch (log_level & G_LOG_LEVEL_MASK)
70         {
71                 case G_LOG_LEVEL_ERROR:     return ASL_LEVEL_CRIT;
72                 case G_LOG_LEVEL_CRITICAL:  return ASL_LEVEL_ERR;
73                 case G_LOG_LEVEL_WARNING:   return ASL_LEVEL_WARNING;
74                 case G_LOG_LEVEL_MESSAGE:   return ASL_LEVEL_NOTICE;
75                 case G_LOG_LEVEL_INFO:      return ASL_LEVEL_INFO;
76                 case G_LOG_LEVEL_DEBUG:     return ASL_LEVEL_DEBUG;
77         }
78         return ASL_LEVEL_ERR;
79 }
80
81 static void
82 out_vfprintf (FILE *ignore, const gchar *format, va_list args)
83 {
84         asl_vlog (NULL, NULL, ASL_LEVEL_WARNING, format, args);
85 }
86
87 #else
88 static void 
89 out_vfprintf (FILE *file, const gchar *format, va_list args)
90 {
91         vfprintf (file, format, args);
92 }
93 #endif
94
95 void
96 g_print (const gchar *format, ...)
97 {
98         va_list args;
99
100         va_start (args, format);
101
102         out_vfprintf (stdout, format, args);
103
104         va_end (args);
105 }
106
107 void
108 g_printerr (const gchar *format, ...)
109 {
110         va_list args;
111
112         va_start (args, format);
113
114         out_vfprintf (stderr, format, args);
115
116         va_end (args);
117 }
118
119 GLogLevelFlags
120 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
121 {
122         GLogLevelFlags old_fatal = fatal;
123
124         fatal |= fatal_mask;
125         
126         return old_fatal;
127 }
128
129 GLogLevelFlags
130 g_log_set_fatal_mask (const gchar *log_domain, GLogLevelFlags fatal_mask)
131 {
132         /*
133          * Mono does not use a G_LOG_DOMAIN currently, so we just assume things are fatal
134          * if we decide to set G_LOG_DOMAIN (we probably should) we should implement
135          * this.
136          */
137         return fatal_mask;
138 }
139
140 void
141 g_logv (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
142 {
143 #if PLATFORM_ANDROID
144         __android_log_vprint (to_android_priority (log_level), log_domain, format, args);
145 #elif MONOTOUCH && defined(__arm__)
146         asl_vlog (NULL, NULL, to_asl_priority (log_level), format, args);
147 #else
148         char *msg;
149         
150         if (vasprintf (&msg, format, args) < 0)
151                 return;
152
153 #ifdef G_OS_WIN32
154         printf ("%s%s%s\n",
155             log_domain != NULL ? log_domain : "",
156             log_domain != NULL ? ": " : "",
157             msg);
158 #else
159 #if MONOTOUCH
160         FILE *target = stderr;
161 #else
162         FILE *target = stdout;
163 #endif
164         
165         fprintf (target, "%s%s%s\n",
166                 log_domain != NULL ? log_domain : "",
167                 log_domain != NULL ? ": " : "",
168                 msg);
169 #endif
170         free (msg);
171         if (log_level & fatal){
172                 fflush (stdout);
173                 fflush (stderr);
174         }
175 #endif
176         if (log_level & fatal){
177                 abort ();
178         }
179 }
180
181 void
182 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
183 {
184         va_list args;
185
186         va_start (args, format);
187         g_logv (log_domain, log_level, format, args);
188         va_end (args);
189 }
190
191 void
192 g_assertion_message (const gchar *format, ...)
193 {
194         va_list args;
195
196         va_start (args, format);
197         g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
198         va_end (args);
199         abort ();
200 }
201