1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 debug.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
23 #include "obt/paths.h"
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;
41 static GList
*prompt_queue
= NULL
;
42 static gboolean allow_prompts
= TRUE
;
44 static void log_handler(const gchar
*log_domain
, GLogLevelFlags log_level
,
45 const gchar
*message
, gpointer user_data
);
46 static void prompt_handler(const gchar
*log_domain
, GLogLevelFlags log_level
,
47 const gchar
*message
, gpointer user_data
);
49 void ob_debug_startup(void)
51 ObtPaths
*p
= obt_paths_new();
52 gchar
*dir
= g_build_filename(obt_paths_cache_home(p
),
55 /* log messages to a log file! fancy, no? */
56 if (!obt_paths_mkdir_path(dir
, 0777))
57 g_message(_("Unable to make directory '%s': %s"),
58 dir
, g_strerror(errno
));
60 gchar
*name
= g_build_filename(obt_paths_cache_home(p
),
61 "openbox", "openbox.log", NULL
);
62 /* unlink it before opening to remove competition */
64 log_file
= fopen(name
, "w");
69 g_log_set_handler("ObRender", G_LOG_LEVEL_MASK
| G_LOG_FLAG_FATAL
|
70 G_LOG_FLAG_RECURSION
, log_handler
, NULL
);
72 g_log_set_handler("Obt", G_LOG_LEVEL_MASK
| G_LOG_FLAG_FATAL
|
73 G_LOG_FLAG_RECURSION
, log_handler
, NULL
);
75 g_log_set_handler("Openbox", G_LOG_LEVEL_MASK
| G_LOG_FLAG_FATAL
|
76 G_LOG_FLAG_RECURSION
, log_handler
, NULL
);
77 ob_handler_prompt_id
=
78 g_log_set_handler("Openbox", G_LOG_LEVEL_MASK
& ~G_LOG_LEVEL_DEBUG
,
79 prompt_handler
, NULL
);
85 void ob_debug_shutdown(void)
87 g_log_remove_handler("ObRender", rr_handler_id
);
88 g_log_remove_handler("Obt", obt_handler_id
);
89 g_log_remove_handler("Openbox", ob_handler_id
);
90 g_log_remove_handler("Openbox", ob_handler_prompt_id
);
98 void ob_debug_enable(ObDebugType type
, gboolean enable
)
100 g_assert(type
< OB_DEBUG_TYPE_NUM
);
101 enabled_types
[type
] = enable
;
104 static inline void log_print(FILE *out
, const gchar
* log_domain
,
105 const gchar
*level
, const gchar
*message
)
107 fprintf(out
, "%s", log_domain
);
109 fprintf(out
, "%s", level
);
111 fprintf(out
, "%s", message
);
116 static void log_handler(const gchar
*log_domain
, GLogLevelFlags log_level
,
117 const gchar
*message
, gpointer data
)
122 switch (log_level
& G_LOG_LEVEL_MASK
) {
123 case G_LOG_LEVEL_DEBUG
: level
= "Debug"; out
= stdout
; break;
124 case G_LOG_LEVEL_INFO
: level
= "Info"; out
= stdout
; break;
125 case G_LOG_LEVEL_MESSAGE
: level
= "Message"; out
= stdout
; break;
126 case G_LOG_LEVEL_WARNING
: level
= "Warning"; out
= stderr
; break;
127 case G_LOG_LEVEL_CRITICAL
: level
= "Critical"; out
= stderr
; break;
128 case G_LOG_LEVEL_ERROR
: level
= "Error"; out
= stderr
; break;
129 default: g_assert_not_reached(); /* invalid level.. */
132 log_print(out
, log_domain
, level
, message
);
133 if (log_file
) log_print(log_file
, log_domain
, level
, message
);
136 static void prompt_handler(const gchar
*log_domain
, GLogLevelFlags log_level
,
137 const gchar
*message
, gpointer data
)
139 if (ob_state() == OB_STATE_RUNNING
&& allow_prompts
)
140 prompt_queue
= g_list_prepend(prompt_queue
, g_strdup(message
));
142 log_handler(log_domain
, log_level
, message
, data
);
145 static inline void log_argv(ObDebugType type
,
146 const gchar
*format
, va_list args
)
151 g_assert(type
< OB_DEBUG_TYPE_NUM
);
152 if (!enabled_types
[type
]) return;
155 case OB_DEBUG_FOCUS
: prefix
= "(FOCUS) "; break;
156 case OB_DEBUG_APP_BUGS
: prefix
= "(APPLICATION BUG) "; break;
157 case OB_DEBUG_SM
: prefix
= "(SESSION) "; break;
158 default: prefix
= NULL
; break;
161 message
= g_strdup_vprintf(format
, args
);
164 message
= g_strconcat(prefix
, message
, NULL
);
168 g_debug("%s", message
);
172 void ob_debug(const gchar
*a
, ...)
177 log_argv(OB_DEBUG_NORMAL
, a
, vl
);
181 void ob_debug_type(ObDebugType type
, const gchar
*a
, ...)
186 log_argv(type
, a
, vl
);
190 void ob_debug_show_prompts(void)
193 allow_prompts
= FALSE
; /* avoid recursive prompts */
194 while (prompt_queue
) {
195 prompt_show_message(prompt_queue
->data
, "Openbox", _("Close"));
196 g_free(prompt_queue
->data
);
197 prompt_queue
= g_list_delete_link(prompt_queue
, prompt_queue
);
199 allow_prompts
= TRUE
;