6 #include "mono-logger.h"
13 static GLogLevelFlags current_level
= G_LOG_LEVEL_ERROR
;
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_stack
== NULL
)
91 if(level
<= current_level
&& mask
& current_mask
)
92 g_logv (mono_log_domain
, level
, format
, args
);
96 * mono_trace_set_level:
98 * @level: Verbose level to set
100 * Sets the current logging level. Every subsequent call to
101 * mono_trace will check the visibility of a message against this
105 mono_trace_set_level (GLogLevelFlags level
)
107 if(level_stack
== NULL
)
110 current_level
= level
;
114 * mono_trace_set_mask:
116 * @mask: Mask of visible message types.
118 * Sets the current logging level. Every subsequent call to
119 * mono_trace will check the visibility of a message against this
123 mono_trace_set_mask (MonoTraceMask mask
)
125 if(level_stack
== NULL
)
134 * @level: Verbose level to set
135 * @mask: Mask of visible message types.
137 * Saves the current values of level and mask then calls mono_trace_set
138 * with the specified new values.
141 mono_trace_push (GLogLevelFlags level
, MonoTraceMask mask
)
143 if(level_stack
== NULL
)
144 g_error(G_GNUC_PRETTY_FUNCTION
": cannot use mono_trace_push without calling mono_trace_init first.");
146 MonoLogLevelEntry
*entry
= g_malloc(sizeof(MonoLogLevelEntry
));
147 entry
->level
= current_level
;
148 entry
->mask
= current_mask
;
150 g_queue_push_head (level_stack
, (gpointer
)entry
);
152 /* Set the new level and mask
154 current_level
= level
;
162 * Restores level and mask values saved from a previous call to mono_trace_push.
165 mono_trace_pop (void)
167 if(level_stack
== NULL
)
168 g_error(G_GNUC_PRETTY_FUNCTION
": cannot use mono_trace_pop without calling mono_trace_init first.");
170 if(!g_queue_is_empty (level_stack
)) {
171 MonoLogLevelEntry
*entry
= (MonoLogLevelEntry
*)g_queue_pop_head (level_stack
);
173 /* Restore previous level and mask
175 current_level
= entry
->level
;
176 current_mask
= entry
->mask
;
185 mono_trace_set_level_string (const char *value
)
188 const char *valid_vals
[] = {"error", "critical", "warning", "message", "info", "debug", NULL
};
189 const GLogLevelFlags valid_ids
[] = {G_LOG_LEVEL_ERROR
, G_LOG_LEVEL_CRITICAL
, G_LOG_LEVEL_WARNING
,
190 G_LOG_LEVEL_MESSAGE
, G_LOG_LEVEL_INFO
, G_LOG_LEVEL_DEBUG
};
195 while(valid_vals
[i
]) {
196 if(!strcmp(valid_vals
[i
], value
)){
197 mono_trace_set_level(valid_ids
[i
]);
204 g_print("Unknown trace loglevel: %s\n", value
);
208 mono_trace_set_mask_string (char *value
)
214 const char *valid_flags
[] = {"asm", "type", "dll", "gc", "cfg", "aot", "all", NULL
};
215 const MonoTraceMask valid_masks
[] = {MONO_TRACE_ASSEMBLY
, MONO_TRACE_TYPE
, MONO_TRACE_DLLIMPORT
,
216 MONO_TRACE_GC
, MONO_TRACE_CONFIG
, MONO_TRACE_AOT
, MONO_TRACE_ALL
};
221 tok
= strtok (value
, ",");
227 for (i
= 0; valid_flags
[i
]; i
++) {
228 if (strcmp (tok
, valid_flags
[i
]) == 0) {
229 flags
|= valid_masks
[i
];
234 g_print("Unknown trace flag: %s\n", tok
);
236 tok
= strtok (NULL
, ",");
240 mono_trace_set_mask (flags
);
244 * mono_trace_is_traced:
246 * Returns whenever a message with @level and @mask will be printed or not.
249 mono_trace_is_traced (GLogLevelFlags level
, MonoTraceMask mask
)
251 return (level
<= current_level
&& mask
& current_mask
);