6 #include "mono-logger.h"
13 static GLogLevelFlags current_level = G_LOG_LEVEL_INFO;
14 static MonoTraceMask current_mask = MONO_TRACE_ALL;
16 static const char *mono_log_domain = "Mono";
17 static GQueue *level_stack = NULL;
22 * Initializes the mono tracer.
25 mono_trace_init (void)
27 if(level_stack == NULL) {
28 level_stack = g_queue_new();
30 mono_trace_set_mask_string(getenv("MONO_LOG_MASK"));
31 mono_trace_set_level_string(getenv("MONO_LOG_LEVEL"));
38 * Releases the mono tracer.
41 mono_trace_cleanup (void)
43 if(level_stack != NULL) {
44 while(!g_queue_is_empty (level_stack)) {
45 g_free (g_queue_pop_head (level_stack));
48 g_queue_free (level_stack);
56 * @level: Verbose level of the specified message
57 * @mask: Type of the specified message
59 * Traces a new message, depending on the current logging level
63 mono_trace(GLogLevelFlags level, MonoTraceMask mask, const char *format, ...)
65 if(level_stack == NULL)
68 if(level <= current_level && mask & current_mask) {
70 va_start (args, format);
71 g_logv (mono_log_domain, level, format, args);
79 * @level: Verbose level of the specified message
80 * @mask: Type of the specified message
82 * Traces a new message, depending on the current logging level
86 mono_tracev (GLogLevelFlags level, MonoTraceMask mask, const char *format, va_list args)
88 if(level <= current_level && mask & current_mask)
89 g_logv (mono_log_domain, level, format, args);
93 * mono_trace_set_level:
95 * @level: Verbose level to set
97 * Sets the current logging level. Every subsequent call to
98 * mono_trace will check the visibility of a message against this
102 mono_trace_set_level (GLogLevelFlags level)
104 if(level_stack == NULL)
107 current_level = level;
111 * mono_trace_set_mask:
113 * @mask: Mask of visible message types.
115 * Sets the current logging level. Every subsequent call to
116 * mono_trace will check the visibility of a message against this
120 mono_trace_set_mask (MonoTraceMask mask)
122 if(level_stack == NULL)
131 * @level: Verbose level to set
132 * @mask: Mask of visible message types.
134 * Saves the current values of level and mask then calls mono_trace_set
135 * with the specified new values.
138 mono_trace_push (GLogLevelFlags level, MonoTraceMask mask)
140 if(level_stack == NULL)
141 g_error(G_GNUC_PRETTY_FUNCTION ": cannot use mono_trace_push without calling mono_trace_init first.");
143 MonoLogLevelEntry *entry = g_malloc(sizeof(MonoLogLevelEntry));
144 entry->level = current_level;
145 entry->mask = current_mask;
147 g_queue_push_head (level_stack, (gpointer)entry);
149 /* Set the new level and mask
151 current_level = level;
159 * Restores level and mask values saved from a previous call to mono_trace_push.
162 mono_trace_pop (void)
164 if(level_stack == NULL)
165 g_error(G_GNUC_PRETTY_FUNCTION ": cannot use mono_trace_pop without calling mono_trace_init first.");
167 if(!g_queue_is_empty (level_stack)) {
168 MonoLogLevelEntry *entry = (MonoLogLevelEntry*)g_queue_pop_head (level_stack);
170 /* Restore previous level and mask
172 current_level = entry->level;
173 current_mask = entry->mask;
182 mono_trace_set_level_string (const char *value)
185 const char *valid_vals[] = {"error", "critical", "warning", "message", "info", "debug", NULL};
186 const GLogLevelFlags valid_ids[] = {G_LOG_LEVEL_ERROR, G_LOG_LEVEL_CRITICAL, G_LOG_LEVEL_WARNING,
187 G_LOG_LEVEL_MESSAGE, G_LOG_LEVEL_INFO, G_LOG_LEVEL_DEBUG };
192 while(valid_vals[i]) {
193 if(!strcmp(valid_vals[i], value)){
194 mono_trace_set_level(valid_ids[i]);
201 g_print("Unknown trace loglevel: %s\n", value);
205 mono_trace_set_mask_string (char *value)
211 const char *valid_flags[] = {"asm", "type", "dll", "gc", "all", NULL};
212 const MonoTraceMask valid_masks[] = {MONO_TRACE_ASSEMBLY, MONO_TRACE_TYPE, MONO_TRACE_DLLIMPORT,
213 MONO_TRACE_GC, MONO_TRACE_ALL };
218 tok = strtok (value, ",");
224 for (i = 0; valid_flags[i]; i++) {
225 if (strcmp (tok, valid_flags[i]) == 0) {
226 flags |= valid_masks[i];
231 g_print("Unknown trace flag: %s\n", tok);
233 tok = strtok (NULL, ",");
237 mono_trace_set_mask (flags);