]> Dogcows Code - chaz/openbox/blob - openbox/debug.c
Merge branch 'backport' into work
[chaz/openbox] / 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 "gettext.h"
21 #include "obt/paths.h"
22
23 #include <glib.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <errno.h>
28
29 static gboolean enabled_types[OB_DEBUG_TYPE_NUM] = {FALSE};
30 static FILE *log_file = NULL;
31 static guint rr_handler_id = 0;
32 static guint obt_handler_id = 0;
33 static guint ob_handler_id = 0;
34
35 static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
36 const gchar *message, gpointer user_data);
37
38 void ob_debug_startup(void)
39 {
40 ObtPaths *p = obt_paths_new();
41 gchar *dir = g_build_filename(obt_paths_cache_home(p),
42 "openbox", NULL);
43
44 /* log messages to a log file! fancy, no? */
45 if (!obt_paths_mkdir_path(dir, 0777))
46 g_message(_("Unable to make directory '%s': %s"),
47 dir, g_strerror(errno));
48 else {
49 gchar *name = g_build_filename(obt_paths_cache_home(p),
50 "openbox", "openbox.log", NULL);
51 /* unlink it before opening to remove competition */
52 unlink(name);
53 log_file = fopen(name, "w");
54 g_free(name);
55 }
56
57 rr_handler_id =
58 g_log_set_handler("ObRender", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
59 G_LOG_FLAG_RECURSION, log_handler, NULL);
60 obt_handler_id =
61 g_log_set_handler("Obt", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
62 G_LOG_FLAG_RECURSION, log_handler, NULL);
63 ob_handler_id =
64 g_log_set_handler("Openbox", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
65 G_LOG_FLAG_RECURSION, log_handler, NULL);
66
67 obt_paths_unref(p);
68 g_free(dir);
69 }
70
71 void ob_debug_shutdown(void)
72 {
73 g_log_remove_handler("ObRender", rr_handler_id);
74 g_log_remove_handler("Obt", obt_handler_id);
75 g_log_remove_handler("Openbox", ob_handler_id);
76
77 if (log_file) {
78 fclose(log_file);
79 log_file = NULL;
80 }
81 }
82
83 void ob_debug_enable(ObDebugType type, gboolean enable)
84 {
85 g_assert(type < OB_DEBUG_TYPE_NUM);
86 enabled_types[type] = enable;
87 }
88
89 static inline void log_print(FILE *out, const gchar* log_domain,
90 const gchar *level, const gchar *message)
91 {
92 fprintf(out, log_domain);
93 fprintf(out, "-");
94 fprintf(out, level);
95 fprintf(out, ": ");
96 fprintf(out, message);
97 fprintf(out, "\n");
98 fflush(out);
99 }
100
101 static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
102 const gchar *message, gpointer data)
103 {
104 FILE *out;
105 const gchar *level;
106
107 switch (log_level & G_LOG_LEVEL_MASK) {
108 case G_LOG_LEVEL_DEBUG: level = "Debug"; out = stdout; break;
109 case G_LOG_LEVEL_INFO: level = "Info"; out = stdout; break;
110 case G_LOG_LEVEL_MESSAGE: level = "Message"; out = stdout; break;
111 case G_LOG_LEVEL_WARNING: level = "Warning"; out = stderr; break;
112 case G_LOG_LEVEL_CRITICAL: level = "Critical"; out = stderr; break;
113 case G_LOG_LEVEL_ERROR: level = "Error"; out = stderr; break;
114 default: g_assert_not_reached(); /* invalid level.. */
115 }
116
117 log_print(out, log_domain, level, message);
118 if (log_file) log_print(log_file, log_domain, level, message);
119 }
120
121 static inline void log_argv(ObDebugType type,
122 const gchar *format, va_list args)
123 {
124 const gchar *prefix;
125 gchar *message;
126
127 g_assert(type < OB_DEBUG_TYPE_NUM);
128 if (!enabled_types[type]) return;
129
130 switch (type) {
131 case OB_DEBUG_FOCUS: prefix = "(FOCUS) "; break;
132 case OB_DEBUG_APP_BUGS: prefix = "(APPLICATION BUG) "; break;
133 case OB_DEBUG_SM: prefix = "(SESSION) "; break;
134 default: prefix = NULL; break;
135 }
136
137 message = g_strdup_vprintf(format, args);
138 if (prefix) {
139 gchar *a = message;
140 message = g_strconcat(prefix, message, NULL);
141 g_free(a);
142 }
143
144 g_debug(message);
145 g_free(message);
146 }
147
148 void ob_debug(const gchar *a, ...)
149 {
150 va_list vl;
151
152 va_start(vl, a);
153 log_argv(OB_DEBUG_NORMAL, a, vl);
154 va_end(vl);
155 }
156
157 void ob_debug_type(ObDebugType type, const gchar *a, ...)
158 {
159 va_list vl;
160
161 va_start(vl, a);
162 log_argv(type, a, vl);
163 va_end(vl);
164 }
This page took 0.043701 seconds and 5 git commands to generate.