1 /**
2  * \file
3  */
4 
5 #ifndef __MONO_LOGGER_INTERNAL_H__
6 #define __MONO_LOGGER_INTERNAL_H__
7 
8 #include <glib.h>
9 #include <mono/utils/mono-compiler.h>
10 #include "mono-logger.h"
11 
12 G_BEGIN_DECLS
13 
14 typedef enum {
15 	MONO_TRACE_ASSEMBLY           = 1 << 0,
16 	MONO_TRACE_TYPE               = 1 << 1,
17 	MONO_TRACE_DLLIMPORT          = 1 << 2,
18 	MONO_TRACE_GC                 = 1 << 3,
19 	MONO_TRACE_CONFIG             = 1 << 4,
20 	MONO_TRACE_AOT                = 1 << 5,
21 	MONO_TRACE_SECURITY           = 1 << 6,
22 	MONO_TRACE_THREADPOOL         = 1 << 7,
23 	MONO_TRACE_IO_SELECTOR        = 1 << 8,
24 	MONO_TRACE_IO_LAYER_PROCESS   = 1 << 9,
25 	MONO_TRACE_IO_LAYER_SOCKET    = 1 << 10,
26 	MONO_TRACE_IO_LAYER_FILE      = 1 << 11,
27 	MONO_TRACE_IO_LAYER_EVENT     = 1 << 12,
28 	MONO_TRACE_IO_LAYER_SEMAPHORE = 1 << 13,
29 	MONO_TRACE_IO_LAYER_MUTEX     = 1 << 14,
30 	MONO_TRACE_IO_LAYER_HANDLE    = 1 << 15,
31 } MonoTraceMask;
32 
33 MONO_API extern GLogLevelFlags mono_internal_current_level;
34 MONO_API extern MonoTraceMask mono_internal_current_mask;
35 
36 MONO_API void
37 mono_trace_init (void);
38 
39 void
40 mono_trace_cleanup (void);
41 
42 MONO_API void
43 mono_tracev_inner (GLogLevelFlags level, MonoTraceMask mask, const char *format, va_list args);
44 
45 void
46 mono_trace_set_level (GLogLevelFlags level);
47 
48 void
49 mono_trace_set_mask (MonoTraceMask mask);
50 
51 void
52 mono_trace_push (GLogLevelFlags level, MonoTraceMask mask);
53 
54 void
55 mono_trace_pop (void);
56 
57 gboolean
58 mono_trace_is_traced (GLogLevelFlags level, MonoTraceMask mask);
59 
60 G_GNUC_UNUSED static void
mono_tracev(GLogLevelFlags level,MonoTraceMask mask,const char * format,va_list args)61 mono_tracev (GLogLevelFlags level, MonoTraceMask mask, const char *format, va_list args)
62 {
63 	if(G_UNLIKELY (level <= mono_internal_current_level && (mask & mono_internal_current_mask)))
64 		mono_tracev_inner (level, mask, format, args);
65 }
66 
67 /**
68  * mono_trace:
69  *
70  *	@level: Verbose level of the specified message
71  *	@mask: Type of the specified message
72  *
73  * Traces a new message, depending on the current logging level
74  * and trace mask.
75  */
76 G_GNUC_UNUSED MONO_ATTR_FORMAT_PRINTF(3,4) static void
mono_trace(GLogLevelFlags level,MonoTraceMask mask,const char * format,...)77 mono_trace (GLogLevelFlags level, MonoTraceMask mask, const char *format, ...)
78 {
79 	if(G_UNLIKELY (level <= mono_internal_current_level && (mask & mono_internal_current_mask))) {
80 		va_list args;
81 		va_start (args, format);
82 		mono_tracev_inner (level, mask, format, args);
83 		va_end (args);
84 	}
85 }
86 
87 #ifdef G_HAVE_ISO_VARARGS
88 #define mono_trace_error(...)	mono_trace(G_LOG_LEVEL_ERROR, \
89 											__VA_ARGS__)
90 #define mono_trace_warning(...) mono_trace(G_LOG_LEVEL_WARNING, \
91 											__VA_ARGS__)
92 #define mono_trace_message(...) mono_trace(G_LOG_LEVEL_MESSAGE, \
93 											__VA_ARGS__)
94 #elif defined(G_HAVE_GNUC_VARARGS)
95 #define mono_trace_error(format...)	mono_trace(G_LOG_LEVEL_ERROR, \
96 											format)
97 #define mono_trace_warning(format...) mono_trace(G_LOG_LEVEL_WARNING, \
98 											format)
99 #define mono_trace_message(format...) mono_trace(G_LOG_LEVEL_MESSAGE, \
100 											format)
101 #else /* no varargs macros */
102 G_GNUC_UNUSED static void
mono_trace_error(MonoTraceMask mask,const char * format,...)103 mono_trace_error(MonoTraceMask mask, const char *format, ...)
104 {
105 	va_list args;
106 	va_start (args, format);
107 	mono_tracev(G_LOG_LEVEL_ERROR, mask, format, args);
108 	va_end (args);
109 }
110 
111 G_GNUC_UNUSED static void
mono_trace_warning(MonoTraceMask mask,const char * format,...)112 mono_trace_warning(MonoTraceMask mask, const char *format, ...)
113 {
114 	va_list args;
115 	va_start (args, format);
116 	mono_tracev(G_LOG_LEVEL_WARNING, mask, format, args);
117 	va_end (args);
118 }
119 
120 G_GNUC_UNUSED static void
mono_trace_message(MonoTraceMask mask,const char * format,...)121 mono_trace_message(MonoTraceMask mask, const char *format, ...)
122 {
123 	va_list args;
124 	va_start (args, format);
125 	mono_tracev(G_LOG_LEVEL_MESSAGE, mask, format, args);
126 	va_end (args);
127 }
128 
129 #endif /* !__GNUC__ */
130 
131 #if defined (HOST_ANDROID) || (defined (TARGET_IOS) && defined (TARGET_IOS))
132 
133 #define mono_gc_printf(gc_log_file, format, ...) g_log ("mono-gc", G_LOG_LEVEL_MESSAGE, format, ##__VA_ARGS__)
134 #define mono_runtime_printf(format, ...) g_log ("mono-rt", G_LOG_LEVEL_MESSAGE, format "\n", ##__VA_ARGS__)
135 #define mono_runtime_printf_err(format, ...) g_log ("mono-rt", G_LOG_LEVEL_CRITICAL, format "\n", ##__VA_ARGS__)
136 #define mono_profiler_printf(format, ...) g_log ("mono-prof", G_LOG_LEVEL_MESSAGE, format "\n", ##__VA_ARGS__)
137 #define mono_profiler_printf_err(format, ...) g_log ("mono-prof", G_LOG_LEVEL_CRITICAL, format "\n", ##__VA_ARGS__)
138 #define mono_runtime_stdout_fflush() do { } while (0)
139 
140 #else
141 
142 #define mono_gc_printf(gc_log_file, format, ...) do {	\
143 	fprintf (gc_log_file, format, ##__VA_ARGS__);	\
144 	fflush (gc_log_file);	\
145 } while (0)
146 
147 #define mono_runtime_printf(format, ...) fprintf (stdout, format "\n", ##__VA_ARGS__)
148 #define mono_runtime_printf_err(format, ...) fprintf (stderr, format "\n", ##__VA_ARGS__)
149 #define mono_profiler_printf(format, ...) fprintf (stdout, format "\n", ##__VA_ARGS__)
150 #define mono_profiler_printf_err(format, ...) fprintf (stderr, format "\n", ##__VA_ARGS__)
151 #define mono_runtime_stdout_fflush() do { fflush (stdout); } while (0)
152 
153 #endif
154 
155 /* Internal logging API */
156 typedef void (*MonoLoggerOpen) (const char *, void *);
157 typedef void (*MonoLoggerWrite) (const char *, GLogLevelFlags, mono_bool, const char *);
158 typedef void (*MonoLoggerClose) (void);
159 
160 typedef struct {
161 	MonoLoggerOpen 	opener;		/* Routine to open logging */
162 	MonoLoggerWrite	writer;		/* Routine to write log data */
163 	MonoLoggerClose closer; 	/* Routine to close logging */
164 	char		*dest;		/* Log destination */
165 	void		*user_data;	/* User data from legacy handler */
166 	mono_bool       header;		/* Whether we want pid/time/date in log message */
167 } MonoLogCallParm;
168 
169 void mono_trace_set_log_handler_internal (MonoLogCallParm *callback, void *user_data);
170 void mono_trace_set_logdest_string (const char *value);
171 void mono_trace_set_logheader_string (const char *value);
172 
173 void mono_log_open_syslog (const char *, void *);
174 void mono_log_write_syslog (const char *, GLogLevelFlags, mono_bool, const char *);
175 void mono_log_close_syslog (void);
176 
177 void mono_log_open_logfile (const char *, void *);
178 void mono_log_write_logfile (const char *, GLogLevelFlags, mono_bool, const char *);
179 void mono_log_close_logfile (void);
180 
181 #if HOST_ANDROID
182 void mono_log_open_logcat (const char *path, void *userData);
183 void mono_log_write_logcat (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message);
184 void mono_log_close_logcat (void);
185 #endif
186 
187 #if defined(HOST_IOS)
188 void mono_log_open_asl (const char *path, void *userData);
189 void mono_log_write_asl (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message);
190 void mono_log_close_asl (void);
191 
192 #endif
193 
194 G_END_DECLS
195 
196 #endif /* __MONO_LOGGER_INTERNAL_H__ */
197