]> code.delx.au - pulseaudio/blob - src/pulsecore/log.c
merge glitch-free branch back into trunk
[pulseaudio] / src / pulsecore / log.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #ifdef HAVE_SYSLOG_H
36 #include <syslog.h>
37 #endif
38
39 #include <pulse/utf8.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
42
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
45
46 #include "log.h"
47
48 #define ENV_LOGLEVEL "PULSE_LOG"
49 #define ENV_LOGMETA "PULSE_LOG_META"
50
51 static char *log_ident = NULL, *log_ident_local = NULL;
52 static pa_log_target_t log_target = PA_LOG_STDERR;
53 static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL;
54 static pa_log_level_t maximal_level = PA_LOG_NOTICE;
55
56 #ifdef HAVE_SYSLOG_H
57 static const int level_to_syslog[] = {
58 [PA_LOG_ERROR] = LOG_ERR,
59 [PA_LOG_WARN] = LOG_WARNING,
60 [PA_LOG_NOTICE] = LOG_NOTICE,
61 [PA_LOG_INFO] = LOG_INFO,
62 [PA_LOG_DEBUG] = LOG_DEBUG
63 };
64 #endif
65
66 static const char level_to_char[] = {
67 [PA_LOG_ERROR] = 'E',
68 [PA_LOG_WARN] = 'W',
69 [PA_LOG_NOTICE] = 'N',
70 [PA_LOG_INFO] = 'I',
71 [PA_LOG_DEBUG] = 'D'
72 };
73
74 void pa_log_set_ident(const char *p) {
75 pa_xfree(log_ident);
76 pa_xfree(log_ident_local);
77
78 log_ident = pa_xstrdup(p);
79 if (!(log_ident_local = pa_utf8_to_locale(log_ident)))
80 log_ident_local = pa_xstrdup(log_ident);
81 }
82
83 /* To make valgrind shut up. */
84 static void ident_destructor(void) PA_GCC_DESTRUCTOR;
85 static void ident_destructor(void) {
86 pa_xfree(log_ident);
87 pa_xfree(log_ident_local);
88 }
89
90 void pa_log_set_maximal_level(pa_log_level_t l) {
91 pa_assert(l < PA_LOG_LEVEL_MAX);
92
93 maximal_level = l;
94 }
95
96 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
97 pa_assert(t == PA_LOG_USER || !func);
98
99 log_target = t;
100 user_log_func = func;
101 }
102
103 void pa_log_levelv_meta(
104 pa_log_level_t level,
105 const char*file,
106 int line,
107 const char *func,
108 const char *format,
109 va_list ap) {
110
111 const char *e;
112 char *t, *n;
113 int saved_errno = errno;
114
115 /* We don't use dynamic memory allocation here to minimize the hit
116 * in RT threads */
117 char text[1024], location[128];
118
119 pa_assert(level < PA_LOG_LEVEL_MAX);
120 pa_assert(format);
121
122 if ((e = getenv(ENV_LOGLEVEL)))
123 maximal_level = atoi(e);
124
125 if (level > maximal_level) {
126 errno = saved_errno;
127 return;
128 }
129
130 pa_vsnprintf(text, sizeof(text), format, ap);
131
132 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
133 pa_snprintf(location, sizeof(location), "[%s:%i %s()] ", file, line, func);
134 else if (file)
135 pa_snprintf(location, sizeof(location), "%s: ", pa_path_get_filename(file));
136 else
137 location[0] = 0;
138
139 if (!pa_utf8_valid(text))
140 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
141
142 for (t = text; t; t = n) {
143 if ((n = strchr(t, '\n'))) {
144 *n = 0;
145 n++;
146 }
147
148 if (!*t)
149 continue;
150
151 switch (log_target) {
152 case PA_LOG_STDERR: {
153 const char *prefix = "", *suffix = "";
154 char *local_t;
155
156 #ifndef OS_IS_WIN32
157 /* Yes indeed. Useless, but fun! */
158 if (isatty(STDERR_FILENO)) {
159 if (level <= PA_LOG_ERROR) {
160 prefix = "\x1B[1;31m";
161 suffix = "\x1B[0m";
162 } else if (level <= PA_LOG_WARN) {
163 prefix = "\x1B[1m";
164 suffix = "\x1B[0m";
165 }
166 }
167 #endif
168
169 /* We shouldn't be using dynamic allocation here to
170 * minimize the hit in RT threads */
171 local_t = pa_utf8_to_locale(t);
172 if (!local_t)
173 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
174 else {
175 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
176 pa_xfree(local_t);
177 }
178
179 break;
180 }
181
182 #ifdef HAVE_SYSLOG_H
183 case PA_LOG_SYSLOG: {
184 char *local_t;
185
186 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
187
188 local_t = pa_utf8_to_locale(t);
189 if (!local_t)
190 syslog(level_to_syslog[level], "%s%s", location, t);
191 else {
192 syslog(level_to_syslog[level], "%s%s", location, local_t);
193 pa_xfree(local_t);
194 }
195
196 closelog();
197 break;
198 }
199 #endif
200
201 case PA_LOG_USER: {
202 char x[1024];
203
204 pa_snprintf(x, sizeof(x), "%s%s", location, t);
205 user_log_func(level, x);
206
207 break;
208 }
209
210 case PA_LOG_NULL:
211 default:
212 break;
213 }
214 }
215
216 errno = saved_errno;
217 }
218
219 void pa_log_level_meta(
220 pa_log_level_t level,
221 const char*file,
222 int line,
223 const char *func,
224 const char *format, ...) {
225
226 va_list ap;
227 va_start(ap, format);
228 pa_log_levelv_meta(level, file, line, func, format, ap);
229 va_end(ap);
230 }
231
232 void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
233 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
234 }
235
236 void pa_log_level(pa_log_level_t level, const char *format, ...) {
237 va_list ap;
238
239 va_start(ap, format);
240 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
241 va_end(ap);
242 }