Merge branch 'backport' into work
[dana/openbox.git] / openbox / debug.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    debug.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5
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.
10
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.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "debug.h"
20 #include "prompt.h"
21 #include "openbox.h"
22 #include "gettext.h"
23 #include "obt/paths.h"
24
25 #include <glib.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #ifdef HAVE_UNISTD_H
32 #  include <unistd.h>
33 #endif
34
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
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);
46
47 void ob_debug_startup(void)
48 {
49     ObtPaths *p = obt_paths_new();
50     gchar *dir = g_build_filename(obt_paths_cache_home(p),
51                                   "openbox", NULL);
52
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));
57     else {
58         gchar *name = g_build_filename(obt_paths_cache_home(p),
59                                        "openbox", "openbox.log", NULL);
60         /* unlink it before opening to remove competition */
61         unlink(name);
62         log_file = fopen(name, "w");
63         g_free(name);
64     }
65
66     rr_handler_id =
67         g_log_set_handler("ObRender", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
68                           G_LOG_FLAG_RECURSION, log_handler, NULL);
69     obt_handler_id =
70         g_log_set_handler("Obt", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
71                           G_LOG_FLAG_RECURSION, log_handler, NULL);
72     ob_handler_id =
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);
78
79     obt_paths_unref(p);
80     g_free(dir);
81 }
82
83 void ob_debug_shutdown(void)
84 {
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);
89
90     if (log_file) {
91         fclose(log_file);
92         log_file = NULL;
93     }
94 }
95
96 void ob_debug_enable(ObDebugType type, gboolean enable)
97 {
98     g_assert(type < OB_DEBUG_TYPE_NUM);
99     enabled_types[type] = enable;
100 }
101
102 static inline void log_print(FILE *out, const gchar* log_domain,
103                              const gchar *level, const gchar *message)
104 {
105     fprintf(out, log_domain);
106     fprintf(out, "-");
107     fprintf(out, level);
108     fprintf(out, ": ");
109     fprintf(out, message);
110     fprintf(out, "\n");
111     fflush(out);
112 }
113
114 static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
115                         const gchar *message, gpointer data)
116 {
117     FILE *out;
118     const gchar *level;
119
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.. */
128     }
129
130     log_print(out, log_domain, level, message);
131     if (log_file) log_print(log_file, log_domain, level, message);
132 }
133
134 static void prompt_handler(const gchar *log_domain, GLogLevelFlags log_level,
135                            const gchar *message, gpointer data)
136 {
137     if (ob_state() == OB_STATE_RUNNING)
138         prompt_show_message(message, _("Openbox"), _("Close"));
139 }
140
141 static inline void log_argv(ObDebugType type,
142                             const gchar *format, va_list args)
143 {
144     const gchar *prefix;
145     gchar *message;
146
147     g_assert(type < OB_DEBUG_TYPE_NUM);
148     if (!enabled_types[type]) return;
149
150     switch (type) {
151     case OB_DEBUG_FOCUS:    prefix = "(FOCUS) ";           break;
152     case OB_DEBUG_APP_BUGS: prefix = "(APPLICATION BUG) "; break;
153     case OB_DEBUG_SM:       prefix = "(SESSION) ";         break;
154     default:                prefix = NULL;                 break;
155     }
156
157     message = g_strdup_vprintf(format, args);
158     if (prefix) {
159         gchar *a = message;
160         message = g_strconcat(prefix, message, NULL);
161         g_free(a);
162     }
163
164     g_debug(message);
165     g_free(message);
166 }
167
168 void ob_debug(const gchar *a, ...)
169 {
170     va_list vl;
171
172     va_start(vl, a);
173     log_argv(OB_DEBUG_NORMAL, a, vl);
174     va_end(vl);
175 }
176
177 void ob_debug_type(ObDebugType type, const gchar *a, ...)
178 {
179     va_list vl;
180
181     va_start(vl, a);
182     log_argv(type, a, vl);
183     va_end(vl);
184 }