[threading] Introduce mono_thread_info_is_live and move all thread state testing...
[mono.git] / mono / utils / mono-logger.c
index bfe84f04f7d1d5bd92c60e5a6d1a284fa3ff7c93..a6be2717e5832dd07bf50c881218d5c1b20def75 100644 (file)
@@ -16,6 +16,7 @@ static MonoTraceMask current_mask             = MONO_TRACE_ALL;
 
 static const char      *mono_log_domain        = "Mono";
 static GQueue          *level_stack            = NULL;
+static MonoPrintCallback print_callback, printerr_callback;
 
 /**
  * mono_trace_init:
@@ -254,3 +255,87 @@ mono_trace_is_traced (GLogLevelFlags level, MonoTraceMask mask)
 {
        return (level <= current_level && mask & current_mask);
 }
+
+static MonoLogCallback log_callback;
+
+static const char*
+log_level_get_name (GLogLevelFlags log_level)
+{
+       switch (log_level & G_LOG_LEVEL_MASK) {
+       case G_LOG_LEVEL_ERROR: return "error";
+       case G_LOG_LEVEL_CRITICAL: return "critical";
+       case G_LOG_LEVEL_WARNING: return "warning";
+       case G_LOG_LEVEL_MESSAGE: return "message";
+       case G_LOG_LEVEL_INFO: return "info";
+       case G_LOG_LEVEL_DEBUG: return "debug";
+       default: return "unknown";
+       }
+}
+
+static void
+log_adapter (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
+{
+       log_callback (log_domain, log_level_get_name (log_level), message, log_level & G_LOG_LEVEL_ERROR, user_data);
+}
+
+/**
+ * mono_trace_set_log_handler:
+ *
+ *  @callback The callback that will replace the default logging handler
+ *  @user_data Argument passed to @callback
+ *
+ * The log handler replaces the default runtime logger. All logging requests with be routed to it.
+ * If the fatal argument in the callback is true, the callback must abort the current process. The runtime expects that
+ * execution will not resume after a fatal error.
+ */
+void
+mono_trace_set_log_handler (MonoLogCallback callback, void *user_data)
+{
+       g_assert (callback);
+       log_callback = callback;
+       g_log_set_default_handler (log_adapter, user_data);
+}
+
+static void
+print_handler (const char *string)
+{
+       print_callback (string, TRUE);
+}
+
+static void
+printerr_handler (const char *string)
+{
+       printerr_callback (string, FALSE);
+}
+
+/**
+ * mono_trace_set_print_handler:
+ *
+ * @callback The callback that will replace the default runtime behavior for stdout output.
+ *
+ * The print handler replaces the default runtime stdout output handler. This is used by free form output done by the runtime.
+ *
+ */
+void
+mono_trace_set_print_handler (MonoPrintCallback callback)
+{
+       g_assert (callback);
+       print_callback = callback;
+       g_set_print_handler (print_handler);
+}
+
+/**
+ * mono_trace_set_printerr_handler:
+ *
+ * @callback The callback that will replace the default runtime behavior for stderr output.
+ *
+ * The print handler replaces the default runtime stderr output handler. This is used by free form output done by the runtime.
+ *
+ */
+void
+mono_trace_set_printerr_handler (MonoPrintCallback callback)
+{
+       g_assert (callback);
+       printerr_callback = callback;
+       g_set_print_handler (printerr_handler);
+}