2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
33 #ifdef HAVE_EXECINFO_H
41 #include <pulse/utf8.h>
42 #include <pulse/xmalloc.h>
43 #include <pulse/util.h>
44 #include <pulse/timeval.h>
46 #include <pulsecore/macro.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/rtclock.h>
49 #include <pulsecore/once.h>
50 #include <pulsecore/ratelimit.h>
54 #define ENV_LOGLEVEL "PULSE_LOG"
55 #define ENV_LOGMETA "PULSE_LOG_META"
56 #define ENV_LOGTIME "PULSE_LOG_TIME"
57 #define ENV_LOGBACKTRACE "PULSE_LOG_BACKTRACE"
59 static char *log_ident
= NULL
, *log_ident_local
= NULL
;
60 static pa_log_target_t log_target
= PA_LOG_STDERR
;
61 static pa_log_func_t user_log_func
= NULL
;
62 static pa_log_level_t maximal_level
= PA_LOG_ERROR
;
63 static unsigned show_backtrace
= 0;
64 static pa_bool_t show_meta
= FALSE
;
65 static pa_bool_t show_time
= FALSE
;
68 static const int level_to_syslog
[] = {
69 [PA_LOG_ERROR
] = LOG_ERR
,
70 [PA_LOG_WARN
] = LOG_WARNING
,
71 [PA_LOG_NOTICE
] = LOG_NOTICE
,
72 [PA_LOG_INFO
] = LOG_INFO
,
73 [PA_LOG_DEBUG
] = LOG_DEBUG
77 static const char level_to_char
[] = {
80 [PA_LOG_NOTICE
] = 'N',
85 void pa_log_set_ident(const char *p
) {
87 pa_xfree(log_ident_local
);
89 log_ident
= pa_xstrdup(p
);
90 if (!(log_ident_local
= pa_utf8_to_locale(log_ident
)))
91 log_ident_local
= pa_xstrdup(log_ident
);
94 /* To make valgrind shut up. */
95 static void ident_destructor(void) PA_GCC_DESTRUCTOR
;
96 static void ident_destructor(void) {
97 if (!pa_in_valgrind())
101 pa_xfree(log_ident_local
);
104 void pa_log_set_maximal_level(pa_log_level_t l
) {
105 pa_assert(l
< PA_LOG_LEVEL_MAX
);
110 void pa_log_set_target(pa_log_target_t t
, pa_log_func_t func
) {
111 pa_assert(t
== PA_LOG_USER
|| !func
);
114 user_log_func
= func
;
117 void pa_log_set_show_meta(pa_bool_t b
) {
121 void pa_log_set_show_time(pa_bool_t b
) {
125 void pa_log_set_show_backtrace(unsigned nlevels
) {
126 show_backtrace
= nlevels
;
129 #ifdef HAVE_EXECINFO_H
131 static char* get_backtrace(unsigned show_nframes
) {
134 char **symbols
, *e
, *r
;
138 if (show_nframes
<= 0)
141 n_frames
= backtrace(trace
, PA_ELEMENTSOF(trace
));
146 symbols
= backtrace_symbols(trace
, n_frames
);
151 n
= PA_MIN((unsigned) n_frames
, show_nframes
);
155 for (j
= 0; j
< n
; j
++) {
158 a
+= strlen(symbols
[j
]);
161 r
= pa_xnew(char, a
);
166 for (j
= 0; j
< n
; j
++) {
172 strcpy(e
, symbols
[j
]);
173 e
+= strlen(symbols
[j
]);
185 void pa_log_levelv_meta(
186 pa_log_level_t level
,
195 int saved_errno
= errno
;
198 #ifdef HAVE_EXECINFO_H
202 /* We don't use dynamic memory allocation here to minimize the hit
204 char text
[4096], location
[128], timestamp
[32];
206 pa_assert(level
< PA_LOG_LEVEL_MAX
);
211 if (PA_UNLIKELY((e
= getenv(ENV_LOGLEVEL
)))) {
212 pa_log_level_t eml
= (pa_log_level_t
) atoi(e
);
218 if (PA_LIKELY(level
> ml
)) {
223 pa_vsnprintf(text
, sizeof(text
), format
, ap
);
225 if ((show_meta
|| getenv(ENV_LOGMETA
)) && file
&& line
> 0 && func
)
226 pa_snprintf(location
, sizeof(location
), "[%s:%i %s()] ", file
, line
, func
);
228 pa_snprintf(location
, sizeof(location
), "%s: ", pa_path_get_filename(file
));
232 if (show_time
|| getenv(ENV_LOGTIME
)) {
233 static pa_usec_t start
, last
;
236 u
= pa_rtclock_usec();
246 /* This is not thread safe, but this is a debugging tool only
250 pa_snprintf(timestamp
, sizeof(timestamp
), "(%4llu.%03llu|%4llu.%03llu) ",
251 (unsigned long long) (a
/ PA_USEC_PER_SEC
),
252 (unsigned long long) (((a
/ PA_USEC_PER_MSEC
)) % 1000),
253 (unsigned long long) (r
/ PA_USEC_PER_SEC
),
254 (unsigned long long) (((r
/ PA_USEC_PER_MSEC
)) % 1000));
259 #ifdef HAVE_EXECINFO_H
260 show_bt
= show_backtrace
;
262 if ((e
= getenv(ENV_LOGBACKTRACE
))) {
263 unsigned ebt
= (unsigned) atoi(e
);
269 bt
= get_backtrace(show_bt
);
272 if (!pa_utf8_valid(text
))
273 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
275 for (t
= text
; t
; t
= n
) {
276 if ((n
= strchr(t
, '\n'))) {
281 /* We ignore strings only made out of whitespace */
282 if (t
[strspn(t
, "\t ")] == 0)
285 switch (log_target
) {
286 case PA_LOG_STDERR
: {
287 const char *prefix
= "", *suffix
= "", *grey
= "";
291 /* Yes indeed. Useless, but fun! */
292 if (isatty(STDERR_FILENO
)) {
293 if (level
<= PA_LOG_ERROR
)
294 prefix
= "\x1B[1;31m";
295 else if (level
<= PA_LOG_WARN
)
301 if (grey
[0] || prefix
[0])
306 /* We shouldn't be using dynamic allocation here to
307 * minimize the hit in RT threads */
308 local_t
= pa_utf8_to_locale(t
);
310 fprintf(stderr
, "%s%c: %s%s%s%s%s%s\n", timestamp
, level_to_char
[level
], location
, prefix
, t
, grey
, pa_strempty(bt
), suffix
);
312 fprintf(stderr
, "%s%c: %s%s%s%s%s%s\n", timestamp
, level_to_char
[level
], location
, prefix
, local_t
, grey
, pa_strempty(bt
), suffix
);
320 case PA_LOG_SYSLOG
: {
323 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
325 local_t
= pa_utf8_to_locale(t
);
327 syslog(level_to_syslog
[level
], "%s%s%s%s", timestamp
, location
, t
, pa_strempty(bt
));
329 syslog(level_to_syslog
[level
], "%s%s%s%s", timestamp
, location
, local_t
, pa_strempty(bt
));
341 pa_snprintf(x
, sizeof(x
), "%s%s%s", timestamp
, location
, t
);
342 user_log_func(level
, x
);
357 void pa_log_level_meta(
358 pa_log_level_t level
,
362 const char *format
, ...) {
365 va_start(ap
, format
);
366 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
370 void pa_log_levelv(pa_log_level_t level
, const char *format
, va_list ap
) {
371 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
374 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
377 va_start(ap
, format
);
378 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
382 pa_bool_t
pa_log_ratelimit(void) {
383 /* Not more than 10 messages every 5s */
384 static PA_DEFINE_RATELIMIT(ratelimit
, 5 * PA_USEC_PER_SEC
, 10);
386 return pa_ratelimit_test(&ratelimit
);