23
#include "obt/paths.h"
22
26
#include <stdlib.h>
23
27
#include <stdarg.h>
28
void ob_debug_show_output(gboolean enable)
33
void ob_debug(const gchar *a, ...)
38
fprintf(stderr, "DEBUG: ");
40
vfprintf(stderr, a, vl);
45
static gboolean enabled_types[OB_DEBUG_TYPE_NUM] = {FALSE};
35
static gboolean enabled_types[OB_DEBUG_TYPE_NUM] = {FALSE};
36
static FILE *log_file = NULL;
37
static guint rr_handler_id = 0;
38
static guint obt_handler_id = 0;
39
static guint ob_handler_id = 0;
40
static guint ob_handler_prompt_id = 0;
42
static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
43
const gchar *message, gpointer user_data);
44
static void prompt_handler(const gchar *log_domain, GLogLevelFlags log_level,
45
const gchar *message, gpointer user_data);
47
void ob_debug_startup(void)
49
ObtPaths *p = obt_paths_new();
50
gchar *dir = g_build_filename(obt_paths_cache_home(p),
53
/* log messages to a log file! fancy, no? */
54
if (!obt_paths_mkdir_path(dir, 0777))
55
g_message(_("Unable to make directory '%s': %s"),
56
dir, g_strerror(errno));
58
gchar *name = g_build_filename(obt_paths_cache_home(p),
59
"openbox", "openbox.log", NULL);
60
/* unlink it before opening to remove competition */
62
log_file = fopen(name, "w");
67
g_log_set_handler("ObRender", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
68
G_LOG_FLAG_RECURSION, log_handler, NULL);
70
g_log_set_handler("Obt", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
71
G_LOG_FLAG_RECURSION, log_handler, NULL);
73
g_log_set_handler("Openbox", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
74
G_LOG_FLAG_RECURSION, log_handler, NULL);
75
ob_handler_prompt_id =
76
g_log_set_handler("Openbox", G_LOG_LEVEL_MASK & ~G_LOG_LEVEL_DEBUG,
77
prompt_handler, NULL);
83
void ob_debug_shutdown(void)
85
g_log_remove_handler("ObRender", rr_handler_id);
86
g_log_remove_handler("Obt", obt_handler_id);
87
g_log_remove_handler("Openbox", ob_handler_id);
88
g_log_remove_handler("Openbox", ob_handler_prompt_id);
47
96
void ob_debug_enable(ObDebugType type, gboolean enable)
50
99
enabled_types[type] = enable;
102
static inline void log_print(FILE *out, const gchar* log_domain,
103
const gchar *level, const gchar *message)
105
fprintf(out, "%s", log_domain);
107
fprintf(out, "%s", level);
109
fprintf(out, "%s", message);
114
static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
115
const gchar *message, gpointer data)
120
switch (log_level & G_LOG_LEVEL_MASK) {
121
case G_LOG_LEVEL_DEBUG: level = "Debug"; out = stdout; break;
122
case G_LOG_LEVEL_INFO: level = "Info"; out = stdout; break;
123
case G_LOG_LEVEL_MESSAGE: level = "Message"; out = stdout; break;
124
case G_LOG_LEVEL_WARNING: level = "Warning"; out = stderr; break;
125
case G_LOG_LEVEL_CRITICAL: level = "Critical"; out = stderr; break;
126
case G_LOG_LEVEL_ERROR: level = "Error"; out = stderr; break;
127
default: g_assert_not_reached(); /* invalid level.. */
130
log_print(out, log_domain, level, message);
131
if (log_file) log_print(log_file, log_domain, level, message);
134
static void prompt_handler(const gchar *log_domain, GLogLevelFlags log_level,
135
const gchar *message, gpointer data)
137
if (ob_state() == OB_STATE_RUNNING)
138
prompt_show_message(message, "Openbox", _("Close"));
140
log_handler(log_domain, log_level, message, data);
143
static inline void log_argv(ObDebugType type,
144
const gchar *format, va_list args)
149
g_assert(type < OB_DEBUG_TYPE_NUM);
150
if (!enabled_types[type]) return;
153
case OB_DEBUG_FOCUS: prefix = "(FOCUS) "; break;
154
case OB_DEBUG_APP_BUGS: prefix = "(APPLICATION BUG) "; break;
155
case OB_DEBUG_SM: prefix = "(SESSION) "; break;
156
default: prefix = NULL; break;
159
message = g_strdup_vprintf(format, args);
162
message = g_strconcat(prefix, message, NULL);
166
g_debug("%s", message);
170
void ob_debug(const gchar *a, ...)
175
log_argv(OB_DEBUG_NORMAL, a, vl);
53
179
void ob_debug_type(ObDebugType type, const gchar *a, ...)
57
g_assert(type < OB_DEBUG_TYPE_NUM);
59
if (show && enabled_types[type]) {
62
fprintf(stderr, "FOCUS: ");
64
case OB_DEBUG_APP_BUGS:
65
fprintf(stderr, "APPLICATION BUG: ");
68
fprintf(stderr, "SESSION: ");
71
g_assert_not_reached();
75
vfprintf(stderr, a, vl);
184
log_argv(type, a, vl);