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