Debian Codegraph @

lib/packagekit-glib2/pk-debug.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <glib/gi18n.h>
25 #include <unistd.h>
26 #include <stdio.h>
27
28 #include <pk-debug.h>
29
30 static gboolean _verbose = FALSE;
31 static gboolean _console = FALSE;
32
33 /**
34 * pk_debug_is_verbose:
35 *
36 * Check if verbose debugging is enabled.
37 *
38 * Returns: %TRUE if we have debugging enabled
39 **/
40 gboolean
41 pk_debug_is_verbose (void)
42 {
43 /* local first */
44 if (_verbose)
45 return TRUE;
46
47 /* fall back to env variable */
48 if (g_getenv ("VERBOSE") != NULL)
49 return TRUE;
50 return FALSE;
51 }
52
53
54 /*
55 * pk_debug_ignore_cb:
56 **/
57 static void
58 pk_debug_ignore_cb (const gchar *log_domain, GLogLevelFlags log_level,
59 const gchar *message, gpointer user_data)
60 {
61 }
62
63 #define CONSOLE_RESET 0
64 #define CONSOLE_BLACK 30
65 #define CONSOLE_RED 31
66 #define CONSOLE_GREEN 32
67 #define CONSOLE_YELLOW 33
68 #define CONSOLE_BLUE 34
69 #define CONSOLE_MAGENTA 35
70 #define CONSOLE_CYAN 36
71 #define CONSOLE_WHITE 37
72
73 #define PK_DEBUG_LOG_DOMAIN_LENGTH 20
74
75 /*
76 * pk_debug_handler_cb:
77 **/
78 static void
79 pk_debug_handler_cb (const gchar *log_domain, GLogLevelFlags log_level,
80 const gchar *message, gpointer user_data)
81 {
82 gchar str_time[255];
83 time_t the_time;
84 static GMutex mutex;
85
86 /* lock */
87 g_mutex_lock (&mutex);
88
89 /* time header */
90 time (&the_time);
91 strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
92
93 /* no color please, we're British */
94 if (!_console) {
95 if (log_level == G_LOG_LEVEL_DEBUG) {
96 g_print ("%s\t%s\t%s\n", str_time, log_domain, message);
97 } else {
98 g_print ("***\n%s\t%s\t%s\n***\n", str_time, log_domain, message);
99 }
100 goto out;
101 }
102
103 /* time in green */
104 g_print ("%c[%dm%s\t", 0x1B, CONSOLE_GREEN, str_time);
105
106 /* log domain in either blue */
107 if (g_strcmp0 (log_domain, G_LOG_DOMAIN) == 0)
108 g_print ("%c[%dm%s%c[%dm", 0x1B, CONSOLE_BLUE, log_domain, 0x1B, CONSOLE_RESET);
109 else
110 g_print ("%c[%dm%s%c[%dm", 0x1B, CONSOLE_CYAN, log_domain, 0x1B, CONSOLE_RESET);
111
112 /* pad with spaces */
113 if (log_domain != NULL) {
114 guint len = strlen (log_domain);
115 for (guint i = len; i < PK_DEBUG_LOG_DOMAIN_LENGTH; i++)
116 g_print (" ");
117 }
118
119 /* critical is also in red */
120 if (log_level == G_LOG_LEVEL_CRITICAL ||
121 log_level == G_LOG_LEVEL_WARNING ||
122 log_level == G_LOG_LEVEL_ERROR) {
123 g_print ("%c[%dm%s\n%c[%dm", 0x1B, CONSOLE_RED, message, 0x1B, CONSOLE_RESET);
124 } else {
125 /* debug in blue */
126 g_print ("%c[%dm%s\n%c[%dm", 0x1B, CONSOLE_BLUE, message, 0x1B, CONSOLE_RESET);
127 }
128 out:
129 /* unlock */
130 g_mutex_unlock (&mutex);
131 }
132
133 /*
134 * pk_debug_pre_parse_hook:
135 */
136 static gboolean
137 pk_debug_pre_parse_hook (GOptionContext *context, GOptionGroup *group, gpointer data, GError **error)
138 {
139 const GOptionEntry main_entries[] = {
140 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &_verbose,
141 /* TRANSLATORS: turn on all debugging */
142 N_("Show debugging information for all files"), NULL },
143 G_OPTION_ENTRY_NULL
144 };
145
146 /* add main entry */
147 g_option_context_add_main_entries (context, main_entries, NULL);
148 return TRUE;
149 }
150
151 /**
152 * pk_debug_add_log_domain:
153 * @log_domain: a log domain
154 *
155 * Add a log domain to the debug output.
156 */
157 void
158 pk_debug_add_log_domain (const gchar *log_domain)
159 {
160 if (_verbose) {
161 g_log_set_fatal_mask (log_domain, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
162 g_log_set_handler (log_domain,
163 G_LOG_LEVEL_ERROR |
164 G_LOG_LEVEL_CRITICAL |
165 G_LOG_LEVEL_DEBUG |
166 G_LOG_LEVEL_WARNING,
167 pk_debug_handler_cb, NULL);
168 } else {
169 /* hide all debugging */
170 g_log_set_handler (log_domain,
171 G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_WARNING,
172 pk_debug_ignore_cb, NULL);
173 }
174 }
175
176 /**
177 * pk_debug_set_verbose:
178 * @verbose: %TRUE if verbose debugging should be enabled.
179 *
180 * Set if verbose debugging is enabled.
181 */
182 void
183 pk_debug_set_verbose (gboolean verbose)
184 {
185 _verbose = verbose;
186 _console = (isatty (fileno (stdout)) == 1);
187 }
188
189 /*
190 * pk_debug_post_parse_hook:
191 */
192 static gboolean
193 pk_debug_post_parse_hook (GOptionContext *context, GOptionGroup *group, gpointer data, GError **error)
194 {
195 /* verbose? */
196 pk_debug_add_log_domain (G_LOG_DOMAIN);
197 _console = (isatty (fileno (stdout)) == 1);
198 g_debug ("Verbose debugging %s (on console %i)", _verbose ? "enabled" : "disabled", _console);
199 return TRUE;
200 }
201
202 /**
203 * pk_debug_get_option_group: (skip)
204 *
205 * Returns a #GOptionGroup for the commandline arguments recognized
206 * by debugging. You should add this group to your #GOptionContext
207 * with g_option_context_add_group(), if you are using
208 * g_option_context_parse() to parse your commandline arguments.
209 *
210 * Returns: a #GOptionGroup for the commandline arguments
211 */
212 GOptionGroup *
213 pk_debug_get_option_group (void)
214 {
215 GOptionGroup *group;
216 group = g_option_group_new ("debug", _("Debugging Options"), _("Show debugging options"), NULL, NULL);
217 g_option_group_set_parse_hooks (group, pk_debug_pre_parse_hook, pk_debug_post_parse_hook);
218 return group;
219 }
220