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