openbox/debug.c (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- debug.c for the Openbox window manager Copyright (c) 2003-2007 Dana Jansens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. See the COPYING file for a copy of the GNU General Public License. */ #include "debug.h" #include "gettext.h" #include "obt/paths.h" #include <glib.h> #include <stdlib.h> #include <stdarg.h> #include <stdio.h> #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 void log_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); 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); 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, log_domain); fprintf(out, "-"); fprintf(out, level); fprintf(out, ": "); fprintf(out, 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 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(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); } void ob_debug_type(ObDebugType type, const gchar *a, ...) { va_list vl; va_start(vl, a); log_argv(type, a, vl); va_end(vl); } |