6 #include "mono-compiler.h"
7 #include "mono-logger-internals.h"
14 GLogLevelFlags mono_internal_current_level = INT_MAX;
15 MonoTraceMask mono_internal_current_mask = MONO_TRACE_ALL;
17 static GQueue *level_stack = NULL;
18 static const char *mono_log_domain = "Mono";
19 static MonoPrintCallback print_callback, printerr_callback;
21 static MonoLogCallback logCallback = {
30 * Initializes the mono tracer.
33 mono_trace_init (void)
35 if(level_stack == NULL) {
36 mono_internal_current_level = G_LOG_LEVEL_ERROR;
37 level_stack = g_queue_new();
39 mono_trace_set_mask_string(g_getenv("MONO_LOG_MASK"));
40 mono_trace_set_level_string(g_getenv("MONO_LOG_LEVEL"));
41 mono_trace_set_logdest_string(g_getenv("MONO_LOG_DEST"));
48 * Releases the mono tracer.
51 mono_trace_cleanup (void)
53 if(level_stack != NULL) {
54 while(!g_queue_is_empty (level_stack)) {
55 g_free (g_queue_pop_head (level_stack));
58 g_queue_free (level_stack);
66 * @level: Verbose level of the specified message
67 * @mask: Type of the specified message
69 * Traces a new message, depending on the current logging level
73 mono_tracev_inner (GLogLevelFlags level, MonoTraceMask mask, const char *format, va_list args)
75 if (level_stack == NULL) {
77 if(level > mono_internal_current_level || !(mask & mono_internal_current_mask))
81 if (logCallback.opener == NULL) {
82 logCallback.opener = mono_log_open_logfile;
83 logCallback.writer = mono_log_write_logfile;
84 logCallback.closer = mono_log_close_logfile;
85 logCallback.opener(NULL, NULL);
87 logCallback.writer(mono_log_domain, level, format, args);
91 * mono_trace_set_level:
93 * @level: Verbose level to set
95 * Sets the current logging level. Every subsequent call to
96 * mono_trace will check the visibility of a message against this
100 mono_trace_set_level (GLogLevelFlags level)
102 if(level_stack == NULL)
105 mono_internal_current_level = level;
109 * mono_trace_set_mask:
111 * @mask: Mask of visible message types.
113 * Sets the current logging level. Every subsequent call to
114 * mono_trace will check the visibility of a message against this
118 mono_trace_set_mask (MonoTraceMask mask)
120 if(level_stack == NULL)
123 mono_internal_current_mask = mask;
127 * mono_trace_set_logdest:
129 * @dest: Destination for logging
131 * Sets the current logging destination. This can be a file or, if supported,
135 mono_trace_set_logdest_string (const char *dest)
137 MonoLogCallback logger;
139 if(level_stack == NULL)
142 if ((dest == NULL) || (strcmp("syslog", dest) != 0)) {
143 logger.opener = mono_log_open_logfile;
144 logger.writer = mono_log_write_logfile;
145 logger.closer = mono_log_close_logfile;
146 mono_trace_set_log_handler(&logger, dest, NULL);
148 logger.opener = mono_log_open_syslog;
149 logger.writer = mono_log_write_syslog;
150 logger.closer = mono_log_close_syslog;
151 mono_trace_set_log_handler(&logger, mono_log_domain, NULL);
158 * @level: Verbose level to set
159 * @mask: Mask of visible message types.
161 * Saves the current values of level and mask then calls mono_trace_set
162 * with the specified new values.
165 mono_trace_push (GLogLevelFlags level, MonoTraceMask mask)
167 if(level_stack == NULL)
168 g_error("%s: cannot use mono_trace_push without calling mono_trace_init first.", __func__);
170 MonoLogLevelEntry *entry = (MonoLogLevelEntry *) g_malloc(sizeof(MonoLogLevelEntry));
171 entry->level = mono_internal_current_level;
172 entry->mask = mono_internal_current_mask;
174 g_queue_push_head (level_stack, (gpointer)entry);
176 /* Set the new level and mask
178 mono_internal_current_level = level;
179 mono_internal_current_mask = mask;
186 * Restores level and mask values saved from a previous call to mono_trace_push.
189 mono_trace_pop (void)
191 if(level_stack == NULL)
192 g_error("%s: cannot use mono_trace_pop without calling mono_trace_init first.", __func__);
194 if(!g_queue_is_empty (level_stack)) {
195 MonoLogLevelEntry *entry = (MonoLogLevelEntry*)g_queue_pop_head (level_stack);
197 /* Restore previous level and mask
199 mono_internal_current_level = entry->level;
200 mono_internal_current_mask = entry->mask;
209 mono_trace_set_level_string (const char *value)
212 const char *valid_vals[] = {"error", "critical", "warning", "message", "info", "debug", NULL};
213 const GLogLevelFlags valid_ids[] = {G_LOG_LEVEL_ERROR, G_LOG_LEVEL_CRITICAL, G_LOG_LEVEL_WARNING,
214 G_LOG_LEVEL_MESSAGE, G_LOG_LEVEL_INFO, G_LOG_LEVEL_DEBUG };
219 while(valid_vals[i]) {
220 if(!strcmp(valid_vals[i], value)){
221 mono_trace_set_level(valid_ids[i]);
228 g_print("Unknown trace loglevel: %s\n", value);
232 mono_trace_set_mask_string (const char *value)
238 const char *valid_flags[] = {"asm", "type", "dll", "gc", "cfg", "aot", "security", "threadpool", "io-threadpool", "io-layer", "all", NULL};
239 const MonoTraceMask valid_masks[] = {MONO_TRACE_ASSEMBLY, MONO_TRACE_TYPE, MONO_TRACE_DLLIMPORT,
240 MONO_TRACE_GC, MONO_TRACE_CONFIG, MONO_TRACE_AOT, MONO_TRACE_SECURITY,
241 MONO_TRACE_THREADPOOL, MONO_TRACE_IO_THREADPOOL, MONO_TRACE_IO_LAYER, MONO_TRACE_ALL };
253 for (i = 0; valid_flags[i]; i++) {
254 int len = strlen (valid_flags[i]);
255 if (strncmp (tok, valid_flags[i], len) == 0 && (tok[len] == 0 || tok[len] == ',')) {
256 flags |= valid_masks[i];
261 if (!valid_flags[i]) {
262 g_print("Unknown trace flag: %s\n", tok);
267 mono_trace_set_mask ((MonoTraceMask) flags);
271 * mono_trace_is_traced:
273 * Returns whenever a message with @level and @mask will be printed or not.
276 mono_trace_is_traced (GLogLevelFlags level, MonoTraceMask mask)
278 return (level <= mono_internal_current_level && mask & mono_internal_current_mask);
282 * mono_trace_set_log_handler:
284 * @callback The callback that will replace the default logging handler
285 * @user_data Argument passed to @callback
287 * The log handler replaces the default runtime logger. All logging requests with be routed to it.
288 * If the fatal argument in the callback is true, the callback must abort the current process. The runtime expects that
289 * execution will not resume after a fatal error.
292 mono_trace_set_log_handler (MonoLogCallback *callback, const char *dest, void *user_data)
295 logCallback.opener = callback->opener;
296 logCallback.writer = callback->writer;
297 logCallback.closer = callback->closer;
298 logCallback.opener(dest, user_data);
302 print_handler (const char *string)
304 print_callback (string, TRUE);
308 printerr_handler (const char *string)
310 printerr_callback (string, FALSE);
314 * mono_trace_set_print_handler:
316 * @callback The callback that will replace the default runtime behavior for stdout output.
318 * The print handler replaces the default runtime stdout output handler. This is used by free form output done by the runtime.
322 mono_trace_set_print_handler (MonoPrintCallback callback)
325 print_callback = callback;
326 g_set_print_handler (print_handler);
330 * mono_trace_set_printerr_handler:
332 * @callback The callback that will replace the default runtime behavior for stderr output.
334 * The print handler replaces the default runtime stderr output handler. This is used by free form output done by the runtime.
338 mono_trace_set_printerr_handler (MonoPrintCallback callback)
341 printerr_callback = callback;
342 g_set_printerr_handler (printerr_handler);