+static inline void log_print(FILE *out, const gchar* log_domain,
+ const gchar *level, const gchar *message)
+{
+ fprintf(out, "%s", log_domain);
+ fprintf(out, "-");
+ fprintf(out, "%s", level);
+ fprintf(out, ": ");
+ fprintf(out, "%s", message);
+ fprintf(out, "\n");
+ fflush(out);
+}
+
+static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
+ const gchar *message, gpointer data)
+{
+ FILE *out;
+ const gchar *level;
+
+ switch (log_level & G_LOG_LEVEL_MASK) {
+ case G_LOG_LEVEL_DEBUG: level = "Debug"; out = stdout; break;
+ case G_LOG_LEVEL_INFO: level = "Info"; out = stdout; break;
+ case G_LOG_LEVEL_MESSAGE: level = "Message"; out = stdout; break;
+ case G_LOG_LEVEL_WARNING: level = "Warning"; out = stderr; break;
+ case G_LOG_LEVEL_CRITICAL: level = "Critical"; out = stderr; break;
+ case G_LOG_LEVEL_ERROR: level = "Error"; out = stderr; break;
+ default: g_assert_not_reached(); /* invalid level.. */
+ }
+
+ log_print(out, log_domain, level, message);
+ if (log_file) log_print(log_file, log_domain, level, message);
+}
+
+static void prompt_handler(const gchar *log_domain, GLogLevelFlags log_level,
+ const gchar *message, gpointer data)
+{
+ if (ob_state() == OB_STATE_RUNNING && allow_prompts)
+ prompt_queue = g_list_prepend(prompt_queue, g_strdup(message));
+ else
+ log_handler(log_domain, log_level, message, data);
+}
+
+static inline void log_argv(ObDebugType type,
+ const gchar *format, va_list args)
+{
+ const gchar *prefix;
+ gchar *message;
+
+ g_assert(type < OB_DEBUG_TYPE_NUM);
+ if (!enabled_types[type]) return;
+
+ switch (type) {
+ case OB_DEBUG_FOCUS: prefix = "(FOCUS) "; break;
+ case OB_DEBUG_APP_BUGS: prefix = "(APPLICATION BUG) "; break;
+ case OB_DEBUG_SM: prefix = "(SESSION) "; break;
+ default: prefix = NULL; break;
+ }
+
+ message = g_strdup_vprintf(format, args);
+ if (prefix) {
+ gchar *a = message;
+ message = g_strconcat(prefix, message, NULL);
+ g_free(a);
+ }
+
+ g_debug("%s", message);
+ g_free(message);
+}
+
+void ob_debug(const gchar *a, ...)
+{
+ va_list vl;
+
+ va_start(vl, a);
+ log_argv(OB_DEBUG_NORMAL, a, vl);
+ va_end(vl);
+}
+