do not check order sequence if option /order was not used
[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 #else
62 static void 
63 out_vfprintf (FILE *file, const gchar *format, va_list args)
64 {
65         vfprintf (file, format, args);
66 }
67 #endif
68
69 void
70 g_print (const gchar *format, ...)
71 {
72         va_list args;
73
74         va_start (args, format);
75
76         out_vfprintf (stdout, format, args);
77
78         va_end (args);
79 }
80
81 void
82 g_printerr (const gchar *format, ...)
83 {
84         va_list args;
85
86         va_start (args, format);
87
88         out_vfprintf (stderr, format, args);
89
90         va_end (args);
91 }
92
93 GLogLevelFlags
94 g_log_set_always_fatal (GLogLevelFlags fatal_mask)
95 {
96         GLogLevelFlags old_fatal = fatal;
97
98         fatal |= fatal_mask;
99         
100         return old_fatal;
101 }
102
103 GLogLevelFlags
104 g_log_set_fatal_mask (const gchar *log_domain, GLogLevelFlags fatal_mask)
105 {
106         /*
107          * Mono does not use a G_LOG_DOMAIN currently, so we just assume things are fatal
108          * if we decide to set G_LOG_DOMAIN (we probably should) we should implement
109          * this.
110          */
111         return fatal_mask;
112 }
113
114 void
115 g_logv (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args)
116 {
117 #if PLATFORM_ANDROID
118         __android_log_vprint (to_android_priority (log_level), log_domain, format, args);
119 #else
120         char *msg;
121         
122         if (vasprintf (&msg, format, args) < 0)
123                 return;
124         
125         printf ("%s%s%s\n",
126                 log_domain != NULL ? log_domain : "",
127                 log_domain != NULL ? ": " : "",
128                 msg);
129         free (msg);
130         if (log_level & fatal){
131                 fflush (stdout);
132                 fflush (stderr);
133         }
134 #endif
135         if (log_level & fatal){
136                 abort ();
137         }
138 }
139
140 void
141 g_log (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...)
142 {
143         va_list args;
144
145         va_start (args, format);
146         g_logv (log_domain, log_level, format, args);
147         va_end (args);
148 }
149
150 void
151 g_assertion_message (const gchar *format, ...)
152 {
153         va_list args;
154
155         va_start (args, format);
156         g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
157         va_end (args);
158         abort ();
159 }
160