| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2010-2012 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 |
#include <glib/gi18n.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <stdio.h> |
| 26 |
#ifdef HAVE_SYSLOG_H |
| 27 |
#include <syslog.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <cd-debug.h> |
| 31 |
|
| 32 |
static gboolean _verbose = FALSE; |
| 33 |
static gboolean _console = FALSE; |
| 34 |
|
| 35 |
gboolean |
| 36 |
cd_debug_is_verbose (void) |
| 37 |
{ |
| 38 |
/* local first */ |
| 39 |
if (_verbose) |
| 40 |
return TRUE; |
| 41 |
|
| 42 |
/* fall back to env variable */ |
| 43 |
if (g_getenv ("VERBOSE") != NULL) |
| 44 |
return TRUE; |
| 45 |
return FALSE; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
static void |
| 50 |
cd_debug_ignore_cb (const gchar *log_domain, |
| 51 |
GLogLevelFlags log_level, |
| 52 |
const gchar *message, |
| 53 |
gpointer user_data) |
| 54 |
{ |
| 55 |
/* syslog */ |
| 56 |
switch (log_level) { |
| 57 |
case G_LOG_LEVEL_INFO: |
| 58 |
#ifdef HAVE_SYSLOG_H |
| 59 |
syslog (LOG_INFO, "%s", message); |
| 60 |
#else |
| 61 |
g_print ("%s\n", message); |
| 62 |
#endif |
| 63 |
break; |
| 64 |
case G_LOG_LEVEL_CRITICAL: |
| 65 |
case G_LOG_LEVEL_ERROR: |
| 66 |
case G_LOG_LEVEL_WARNING: |
| 67 |
#ifdef HAVE_SYSLOG_H |
| 68 |
syslog (LOG_WARNING, "%s", message); |
| 69 |
#else |
| 70 |
g_print ("%s\n", message); |
| 71 |
#endif |
| 72 |
break; |
| 73 |
default: |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
cd_debug_handler_cb (const gchar *log_domain, |
| 80 |
GLogLevelFlags log_level, |
| 81 |
const gchar *message, |
| 82 |
gpointer user_data) |
| 83 |
{ |
| 84 |
gchar str_time[255]; |
| 85 |
time_t the_time; |
| 86 |
|
| 87 |
/* syslog */ |
| 88 |
switch (log_level) { |
| 89 |
case G_LOG_LEVEL_INFO: |
| 90 |
#ifdef HAVE_SYSLOG_H |
| 91 |
syslog (LOG_INFO, "%s", message); |
| 92 |
#else |
| 93 |
g_print ("%s\n", message); |
| 94 |
#endif |
| 95 |
break; |
| 96 |
case G_LOG_LEVEL_CRITICAL: |
| 97 |
case G_LOG_LEVEL_ERROR: |
| 98 |
case G_LOG_LEVEL_WARNING: |
| 99 |
#ifdef HAVE_SYSLOG_H |
| 100 |
syslog (LOG_WARNING, "%s", message); |
| 101 |
#else |
| 102 |
g_print ("%s\n", message); |
| 103 |
#endif |
| 104 |
break; |
| 105 |
default: |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
/* time header */ |
| 110 |
time (&the_time); |
| 111 |
strftime (str_time, 254, "%H:%M:%S", localtime (&the_time)); |
| 112 |
|
| 113 |
/* no color please, we're British */ |
| 114 |
if (!_console) { |
| 115 |
if (log_level == G_LOG_LEVEL_DEBUG) { |
| 116 |
g_print ("%s\t%s\n", |
| 117 |
str_time, message); |
| 118 |
} else { |
| 119 |
g_print ("***\n%s\t%s\n***\n", |
| 120 |
str_time, message); |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
/* critical is also in red */ |
| 126 |
if (log_level == G_LOG_LEVEL_CRITICAL || |
| 127 |
log_level == G_LOG_LEVEL_ERROR) { |
| 128 |
g_print ("%c[%dm%s\t", 0x1B, 32, str_time); |
| 129 |
g_print ("%c[%dm%s\n%c[%dm", 0x1B, 31, message, 0x1B, 0); |
| 130 |
} else { |
| 131 |
/* debug in blue */ |
| 132 |
g_print ("%c[%dm%s\t", 0x1B, 32, str_time); |
| 133 |
g_print ("%c[%dm%s\n%c[%dm", 0x1B, 34, message, 0x1B, 0); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static gboolean |
| 138 |
cd_debug_pre_parse_hook (GOptionContext *context, |
| 139 |
GOptionGroup *group, |
| 140 |
gpointer data, |
| 141 |
GError **error) |
| 142 |
{ |
| 143 |
const GOptionEntry main_entries[] = { |
| 144 |
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &_verbose, |
| 145 |
/* TRANSLATORS: turn on all debugging */ |
| 146 |
N_("Show debugging information for all files"), NULL }, |
| 147 |
{ NULL} |
| 148 |
}; |
| 149 |
|
| 150 |
/* add main entry */ |
| 151 |
g_option_context_add_main_entries (context, main_entries, NULL); |
| 152 |
return TRUE; |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
cd_debug_destroy (void) |
| 157 |
{ |
| 158 |
#ifdef HAVE_SYSLOG_H |
| 159 |
closelog (); |
| 160 |
#endif |
| 161 |
} |
| 162 |
|
| 163 |
void |
| 164 |
cd_debug_setup (gboolean enabled) |
| 165 |
{ |
| 166 |
if (enabled) { |
| 167 |
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | |
| 168 |
G_LOG_LEVEL_CRITICAL); |
| 169 |
g_log_set_handler (G_LOG_DOMAIN, |
| 170 |
G_LOG_LEVEL_ERROR | |
| 171 |
G_LOG_LEVEL_CRITICAL | |
| 172 |
G_LOG_LEVEL_DEBUG | |
| 173 |
G_LOG_LEVEL_WARNING, |
| 174 |
cd_debug_handler_cb, NULL); |
| 175 |
} else { |
| 176 |
/* hide all debugging */ |
| 177 |
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, |
| 178 |
cd_debug_ignore_cb, NULL); |
| 179 |
} |
| 180 |
|
| 181 |
#ifdef HAVE_SYSLOG_H |
| 182 |
openlog ("colord", LOG_CONS, LOG_DAEMON); |
| 183 |
#endif |
| 184 |
/* are we on an actual TTY? */ |
| 185 |
_console = (isatty (fileno (stdout)) == 1); |
| 186 |
} |
| 187 |
|
| 188 |
static gboolean |
| 189 |
cd_debug_post_parse_hook (GOptionContext *context, |
| 190 |
GOptionGroup *group, |
| 191 |
gpointer data, |
| 192 |
GError **error) |
| 193 |
{ |
| 194 |
/* verbose? */ |
| 195 |
cd_debug_setup (_verbose); |
| 196 |
g_debug ("Verbose debugging %s (on console %i)", |
| 197 |
_verbose ? "enabled" : "disabled", _console); |
| 198 |
return TRUE; |
| 199 |
} |
| 200 |
|
| 201 |
GOptionGroup * |
| 202 |
cd_debug_get_option_group (void) |
| 203 |
{ |
| 204 |
GOptionGroup *group; |
| 205 |
group = g_option_group_new ("debug", |
| 206 |
_("Debugging Options"), |
| 207 |
_("Show debugging options"), |
| 208 |
NULL, NULL); |
| 209 |
g_option_group_set_parse_hooks (group, |
| 210 |
cd_debug_pre_parse_hook, |
| 211 |
cd_debug_post_parse_hook); |
| 212 |
return group; |
| 213 |
} |