~behda/+junk/udisks2.original

« back to all changes in this revision

Viewing changes to src/udiskslogging.c

  • Committer: behda
  • Date: 2014-05-24 15:15:11 UTC
  • Revision ID: pauvitk@gmail.com-20140524151511-3vtr0uubjewx3z2j
Initial commit of source code and Debian packaging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 
2
 *
 
3
 * Copyright (C) 2007-2011 David Zeuthen <zeuthen@gmail.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include <glib/gi18n-lib.h>
 
23
 
 
24
#include <sys/types.h>
 
25
#include <sys/syscall.h>
 
26
 
 
27
#include <syslog.h>
 
28
 
 
29
#include "udiskslogging.h"
 
30
 
 
31
/**
 
32
 * SECTION:udiskslogging
 
33
 * @title: Logging
 
34
 * @short_description: Logging Routines
 
35
 *
 
36
 * Logging routines.
 
37
 */
 
38
 
 
39
G_LOCK_DEFINE_STATIC (log_lock);
 
40
 
 
41
/* ---------------------------------------------------------------------------------------------------- */
 
42
 
 
43
typedef enum
 
44
{
 
45
  _COLOR_RESET,
 
46
  _COLOR_BOLD_ON,
 
47
  _COLOR_INVERSE_ON,
 
48
  _COLOR_BOLD_OFF,
 
49
  _COLOR_FG_BLACK,
 
50
  _COLOR_FG_RED,
 
51
  _COLOR_FG_GREEN,
 
52
  _COLOR_FG_YELLOW,
 
53
  _COLOR_FG_BLUE,
 
54
  _COLOR_FG_MAGENTA,
 
55
  _COLOR_FG_CYAN,
 
56
  _COLOR_FG_WHITE,
 
57
  _COLOR_BG_RED,
 
58
  _COLOR_BG_GREEN,
 
59
  _COLOR_BG_YELLOW,
 
60
  _COLOR_BG_BLUE,
 
61
  _COLOR_BG_MAGENTA,
 
62
  _COLOR_BG_CYAN,
 
63
  _COLOR_BG_WHITE
 
64
} _Color;
 
65
 
 
66
static gboolean _color_stdin_is_tty = FALSE;
 
67
static gboolean _color_initialized = FALSE;
 
68
 
 
69
static void
 
70
_color_init (void)
 
71
{
 
72
  if (_color_initialized)
 
73
    return;
 
74
  _color_initialized = TRUE;
 
75
  _color_stdin_is_tty = (isatty (STDIN_FILENO) != 0 && isatty (STDOUT_FILENO) != 0);
 
76
}
 
77
 
 
78
static const gchar *
 
79
_color_get (_Color color)
 
80
{
 
81
  const gchar *str;
 
82
 
 
83
  _color_init ();
 
84
 
 
85
  if (!_color_stdin_is_tty)
 
86
    return "";
 
87
 
 
88
  str = NULL;
 
89
  switch (color)
 
90
    {
 
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;
 
110
    default:
 
111
      g_assert_not_reached ();
 
112
      break;
 
113
    }
 
114
  return str;
 
115
}
 
116
 
 
117
/* ---------------------------------------------------------------------------------------------------- */
 
118
 
 
119
/**
 
120
 * udisks_log:
 
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.
 
126
 *
 
127
 * Low-level logging function used by udisks_debug() and other macros.
 
128
 */
 
129
void
 
130
udisks_log (UDisksLogLevel     level,
 
131
            const gchar       *function,
 
132
            const gchar       *location,
 
133
            const gchar       *format,
 
134
            ...)
 
135
{
 
136
  va_list var_args;
 
137
  gchar *message;
 
138
  GTimeVal now;
 
139
  time_t now_time;
 
140
  struct tm *now_tm;
 
141
  gchar time_buf[128];
 
142
  const gchar *level_str;
 
143
  const gchar *level_color_str;
 
144
  gint syslog_priority;
 
145
  static gboolean have_called_openlog = FALSE;
 
146
  gchar *thread_str;
 
147
 
 
148
  va_start (var_args, format);
 
149
  message = g_strdup_vprintf (format, var_args);
 
150
  va_end (var_args);
 
151
 
 
152
  G_LOCK (log_lock);
 
153
 
 
154
  if (!have_called_openlog)
 
155
    {
 
156
      openlog ("udisksd",
 
157
               LOG_CONS|LOG_NDELAY|LOG_PID,
 
158
               LOG_DAEMON);
 
159
      have_called_openlog = TRUE;
 
160
    }
 
161
 
 
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);
 
166
 
 
167
  switch (level)
 
168
    {
 
169
    case UDISKS_LOG_LEVEL_DEBUG:
 
170
      level_str = "[DEBUG]";
 
171
      syslog_priority = LOG_DEBUG;
 
172
      level_color_str = _color_get (_COLOR_FG_BLUE);
 
173
      break;
 
174
 
 
175
    case UDISKS_LOG_LEVEL_INFO:
 
176
      level_str = "[INFO]";
 
177
      syslog_priority = LOG_INFO;
 
178
      level_color_str = _color_get (_COLOR_FG_CYAN);
 
179
      break;
 
180
 
 
181
    case UDISKS_LOG_LEVEL_NOTICE:
 
182
      level_str = "[NOTICE]";
 
183
      syslog_priority = LOG_NOTICE;
 
184
      level_color_str = _color_get (_COLOR_FG_CYAN);
 
185
      break;
 
186
 
 
187
    case UDISKS_LOG_LEVEL_WARNING:
 
188
      level_str = "[WARNING]";
 
189
      syslog_priority = LOG_WARNING;
 
190
      level_color_str = _color_get (_COLOR_FG_YELLOW);
 
191
      break;
 
192
 
 
193
    case UDISKS_LOG_LEVEL_ERROR:
 
194
      level_str = "[ERROR]";
 
195
      syslog_priority = LOG_ERR;
 
196
      level_color_str = _color_get (_COLOR_FG_RED);
 
197
      break;
 
198
 
 
199
    default:
 
200
      g_assert_not_reached ();
 
201
      break;
 
202
    }
 
203
 
 
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),
 
209
           message,
 
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);
 
213
  g_free (message);
 
214
 
 
215
  G_UNLOCK (log_lock);
 
216
}