summaryrefslogtreecommitdiff
path: root/src/cd-debug.c
blob: 9f436f0b7bf5932d35d18fc4a0961c08887125f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2010-2012 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>
#include <glib/gi18n.h>
#include <unistd.h>
#include <stdio.h>
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif

#include <cd-debug.h>

static gboolean _verbose = FALSE;
static gboolean _console = FALSE;

gboolean
cd_debug_is_verbose (void)
{
	/* local first */
	if (_verbose)
		return TRUE;

	/* fall back to env variable */
	if (g_getenv ("VERBOSE") != NULL)
		return TRUE;
	return FALSE;
}


static void
cd_debug_ignore_cb (const gchar *log_domain,
		    GLogLevelFlags log_level,
		    const gchar *message,
		    gpointer user_data)
{
	/* syslog */
	switch (log_level) {
	case G_LOG_LEVEL_INFO:
#ifdef HAVE_SYSLOG_H
		syslog (LOG_INFO, "%s", message);
#else
		g_print ("%s\n", message);
#endif
		break;
	case G_LOG_LEVEL_CRITICAL:
	case G_LOG_LEVEL_ERROR:
	case G_LOG_LEVEL_WARNING:
#ifdef HAVE_SYSLOG_H
		syslog (LOG_WARNING, "%s", message);
#else
		g_print ("%s\n", message);
#endif
		break;
	default:
		break;
	}
}

static void
cd_debug_handler_cb (const gchar *log_domain,
		     GLogLevelFlags log_level,
		     const gchar *message,
		     gpointer user_data)
{
	gchar str_time[255];
	time_t the_time;

	/* syslog */
	switch (log_level) {
	case G_LOG_LEVEL_INFO:
#ifdef HAVE_SYSLOG_H
		syslog (LOG_INFO, "%s", message);
#else
		g_print ("%s\n", message);
#endif
		break;
	case G_LOG_LEVEL_CRITICAL:
	case G_LOG_LEVEL_ERROR:
	case G_LOG_LEVEL_WARNING:
#ifdef HAVE_SYSLOG_H
		syslog (LOG_WARNING, "%s", message);
#else
		g_print ("%s\n", message);
#endif
		break;
	default:
		break;
	}

	/* time header */
	time (&the_time);
	strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));

	/* no color please, we're British */
	if (!_console) {
		if (log_level == G_LOG_LEVEL_DEBUG) {
			g_print ("%s\t%s\n",
				 str_time, message);
		} else {
			g_print ("***\n%s\t%s\n***\n",
				 str_time, message);
		}
		return;
	}

	/* critical is also in red */
	if (log_level == G_LOG_LEVEL_CRITICAL ||
	    log_level == G_LOG_LEVEL_ERROR) {
		g_print ("%c[%dm%s\t", 0x1B, 32, str_time);
		g_print ("%c[%dm%s\n%c[%dm", 0x1B, 31, message, 0x1B, 0);
	} else {
		/* debug in blue */
		g_print ("%c[%dm%s\t", 0x1B, 32, str_time);
		g_print ("%c[%dm%s\n%c[%dm", 0x1B, 34, message, 0x1B, 0);
	}
}

static gboolean
cd_debug_pre_parse_hook (GOptionContext *context,
			 GOptionGroup *group,
			 gpointer data,
			 GError **error)
{
	const GOptionEntry main_entries[] = {
		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &_verbose,
		  /* TRANSLATORS: turn on all debugging */
		  N_("Show debugging information for all files"), NULL },
		{ NULL}
	};

	/* add main entry */
	g_option_context_add_main_entries (context, main_entries, NULL);
	return TRUE;
}

void
cd_debug_destroy (void)
{
#ifdef HAVE_SYSLOG_H
	closelog ();
#endif
}

void
cd_debug_setup (gboolean enabled)
{
	if (enabled) {
		g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR |
					    G_LOG_LEVEL_CRITICAL);
		g_log_set_handler (G_LOG_DOMAIN,
				   G_LOG_LEVEL_ERROR |
				   G_LOG_LEVEL_CRITICAL |
				   G_LOG_LEVEL_DEBUG |
				   G_LOG_LEVEL_WARNING,
				   cd_debug_handler_cb, NULL);
	} else {
		/* hide all debugging */
		g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
				   cd_debug_ignore_cb, NULL);
	}

#ifdef HAVE_SYSLOG_H
	openlog ("colord", LOG_CONS, LOG_DAEMON);
#endif
	/* are we on an actual TTY? */
	_console = (isatty (fileno (stdout)) == 1);
}

static gboolean
cd_debug_post_parse_hook (GOptionContext *context,
			  GOptionGroup *group,
			  gpointer data,
			  GError **error)
{
	/* verbose? */
	cd_debug_setup (_verbose);
	g_debug ("Verbose debugging %s (on console %i)",
		 _verbose ? "enabled" : "disabled", _console);
	return TRUE;
}

GOptionGroup *
cd_debug_get_option_group (void)
{
	GOptionGroup *group;
	group = g_option_group_new ("debug",
				    _("Debugging Options"),
				    _("Show debugging options"),
				    NULL, NULL);
	g_option_group_set_parse_hooks (group,
					cd_debug_pre_parse_hook,
					cd_debug_post_parse_hook);
	return group;
}