+
+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);
+}