+#include <errno.h>
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+static gboolean enabled_types[OB_DEBUG_TYPE_NUM] = {FALSE};
+static FILE *log_file = NULL;
+static guint rr_handler_id = 0;
+static guint obt_handler_id = 0;
+static guint ob_handler_id = 0;
+static guint ob_handler_prompt_id = 0;
+static GList *prompt_queue = NULL;
+static gboolean allow_prompts = TRUE;
+
+static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
+ const gchar *message, gpointer user_data);
+static void prompt_handler(const gchar *log_domain, GLogLevelFlags log_level,
+ const gchar *message, gpointer user_data);
+
+void ob_debug_startup(void)
+{
+ ObtPaths *p = obt_paths_new();
+ gchar *dir = g_build_filename(obt_paths_cache_home(p),
+ "openbox", NULL);
+
+ /* log messages to a log file! fancy, no? */
+ if (!obt_paths_mkdir_path(dir, 0777))
+ g_message(_("Unable to make directory '%s': %s"),
+ dir, g_strerror(errno));
+ else {
+ gchar *name = g_build_filename(obt_paths_cache_home(p),
+ "openbox", "openbox.log", NULL);
+ /* unlink it before opening to remove competition */
+ unlink(name);
+ log_file = fopen(name, "w");
+ g_free(name);
+ }
+
+ rr_handler_id =
+ g_log_set_handler("ObRender", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
+ G_LOG_FLAG_RECURSION, log_handler, NULL);
+ obt_handler_id =
+ g_log_set_handler("Obt", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
+ G_LOG_FLAG_RECURSION, log_handler, NULL);
+ ob_handler_id =
+ g_log_set_handler("Openbox", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
+ G_LOG_FLAG_RECURSION, log_handler, NULL);
+ ob_handler_prompt_id =
+ g_log_set_handler("Openbox", G_LOG_LEVEL_MASK & ~G_LOG_LEVEL_DEBUG,
+ prompt_handler, NULL);
+
+ obt_paths_unref(p);
+ g_free(dir);
+}
+
+void ob_debug_shutdown(void)
+{
+ g_log_remove_handler("ObRender", rr_handler_id);
+ g_log_remove_handler("Obt", obt_handler_id);
+ g_log_remove_handler("Openbox", ob_handler_id);
+ g_log_remove_handler("Openbox", ob_handler_prompt_id);
+
+ if (log_file) {
+ fclose(log_file);
+ log_file = NULL;
+ }
+}
+
+void ob_debug_enable(ObDebugType type, gboolean enable)
+{
+ g_assert(type < OB_DEBUG_TYPE_NUM);
+ enabled_types[type] = enable;
+}
+
+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);
+}