1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
3
* Copyright (C) 2007-2011 David Zeuthen <zeuthen@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <glib/gi18n-lib.h>
24
#include <sys/types.h>
25
#include <sys/syscall.h>
29
#include "udiskslogging.h"
32
* SECTION:udiskslogging
34
* @short_description: Logging Routines
39
G_LOCK_DEFINE_STATIC (log_lock);
41
/* ---------------------------------------------------------------------------------------------------- */
66
static gboolean _color_stdin_is_tty = FALSE;
67
static gboolean _color_initialized = FALSE;
72
if (_color_initialized)
74
_color_initialized = TRUE;
75
_color_stdin_is_tty = (isatty (STDIN_FILENO) != 0 && isatty (STDOUT_FILENO) != 0);
79
_color_get (_Color color)
85
if (!_color_stdin_is_tty)
91
case _COLOR_RESET: str="\x1b[0m"; break;
92
case _COLOR_BOLD_ON: str="\x1b[1m"; break;
93
case _COLOR_INVERSE_ON: str="\x1b[7m"; break;
94
case _COLOR_BOLD_OFF: str="\x1b[22m"; break;
95
case _COLOR_FG_BLACK: str="\x1b[30m"; break;
96
case _COLOR_FG_RED: str="\x1b[31m"; break;
97
case _COLOR_FG_GREEN: str="\x1b[32m"; break;
98
case _COLOR_FG_YELLOW: str="\x1b[33m"; break;
99
case _COLOR_FG_BLUE: str="\x1b[34m"; break;
100
case _COLOR_FG_MAGENTA: str="\x1b[35m"; break;
101
case _COLOR_FG_CYAN: str="\x1b[36m"; break;
102
case _COLOR_FG_WHITE: str="\x1b[37m"; break;
103
case _COLOR_BG_RED: str="\x1b[41m"; break;
104
case _COLOR_BG_GREEN: str="\x1b[42m"; break;
105
case _COLOR_BG_YELLOW: str="\x1b[43m"; break;
106
case _COLOR_BG_BLUE: str="\x1b[44m"; break;
107
case _COLOR_BG_MAGENTA: str="\x1b[45m"; break;
108
case _COLOR_BG_CYAN: str="\x1b[46m"; break;
109
case _COLOR_BG_WHITE: str="\x1b[47m"; break;
111
g_assert_not_reached ();
117
/* ---------------------------------------------------------------------------------------------------- */
121
* @level: A #UDisksLogLevel.
122
* @function: Pass #G_STRFUNC here.
123
* @location: Pass #G_STRLOC here.
124
* @format: printf()-style format.
125
* @...: Arguments for format.
127
* Low-level logging function used by udisks_debug() and other macros.
130
udisks_log (UDisksLogLevel level,
131
const gchar *function,
132
const gchar *location,
142
const gchar *level_str;
143
const gchar *level_color_str;
144
gint syslog_priority;
145
static gboolean have_called_openlog = FALSE;
148
va_start (var_args, format);
149
message = g_strdup_vprintf (format, var_args);
154
if (!have_called_openlog)
157
LOG_CONS|LOG_NDELAY|LOG_PID,
159
have_called_openlog = TRUE;
162
g_get_current_time (&now);
163
now_time = (time_t) now.tv_sec;
164
now_tm = localtime (&now_time);
165
strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
169
case UDISKS_LOG_LEVEL_DEBUG:
170
level_str = "[DEBUG]";
171
syslog_priority = LOG_DEBUG;
172
level_color_str = _color_get (_COLOR_FG_BLUE);
175
case UDISKS_LOG_LEVEL_INFO:
176
level_str = "[INFO]";
177
syslog_priority = LOG_INFO;
178
level_color_str = _color_get (_COLOR_FG_CYAN);
181
case UDISKS_LOG_LEVEL_NOTICE:
182
level_str = "[NOTICE]";
183
syslog_priority = LOG_NOTICE;
184
level_color_str = _color_get (_COLOR_FG_CYAN);
187
case UDISKS_LOG_LEVEL_WARNING:
188
level_str = "[WARNING]";
189
syslog_priority = LOG_WARNING;
190
level_color_str = _color_get (_COLOR_FG_YELLOW);
193
case UDISKS_LOG_LEVEL_ERROR:
194
level_str = "[ERROR]";
195
syslog_priority = LOG_ERR;
196
level_color_str = _color_get (_COLOR_FG_RED);
200
g_assert_not_reached ();
204
thread_str = g_strdup_printf ("%d", (gint) syscall (SYS_gettid));
205
g_print ("%s%s%s.%03d:%s%s%s[%s]%s:%s%s%s:%s %s %s[%s, %s()]%s\n",
206
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_YELLOW), time_buf, (gint) now.tv_usec / 1000, _color_get (_COLOR_RESET),
207
_color_get (_COLOR_FG_MAGENTA), _color_get (_COLOR_BOLD_ON), thread_str, _color_get (_COLOR_RESET),
208
level_color_str, _color_get (_COLOR_BOLD_ON), level_str, _color_get (_COLOR_RESET),
210
_color_get (_COLOR_FG_BLACK), location, function, _color_get (_COLOR_RESET));
211
if (level >= UDISKS_LOG_LEVEL_NOTICE)
212
syslog (syslog_priority, "%s", message);