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