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>
53 #define ENV_LOGLEVEL "PULSE_LOG"
54 #define ENV_LOGMETA "PULSE_LOG_META"
55 #define ENV_LOGTIME "PULSE_LOG_TIME"
56 #define ENV_LOGBACKTRACE "PULSE_LOG_BACKTRACE"
58 static char *log_ident
= NULL
, *log_ident_local
= NULL
;
59 static pa_log_target_t log_target
= PA_LOG_STDERR
;
60 static pa_log_func_t user_log_func
= NULL
;
61 static pa_log_level_t maximal_level
= PA_LOG_ERROR
;
62 static unsigned show_backtrace
= 0;
63 static pa_bool_t show_meta
= FALSE
;
64 static pa_bool_t show_time
= FALSE
;
67 static const int level_to_syslog
[] = {
68 [PA_LOG_ERROR
] = LOG_ERR
,
69 [PA_LOG_WARN
] = LOG_WARNING
,
70 [PA_LOG_NOTICE
] = LOG_NOTICE
,
71 [PA_LOG_INFO
] = LOG_INFO
,
72 [PA_LOG_DEBUG
] = LOG_DEBUG
76 static const char level_to_char
[] = {
79 [PA_LOG_NOTICE
] = 'N',
84 void pa_log_set_ident(const char *p
) {
86 pa_xfree(log_ident_local
);
88 log_ident
= pa_xstrdup(p
);
89 if (!(log_ident_local
= pa_utf8_to_locale(log_ident
)))
90 log_ident_local
= pa_xstrdup(log_ident
);
93 /* To make valgrind shut up. */
94 static void ident_destructor(void) PA_GCC_DESTRUCTOR
;
95 static void ident_destructor(void) {
96 if (!pa_in_valgrind())
100 pa_xfree(log_ident_local
);
103 void pa_log_set_maximal_level(pa_log_level_t l
) {
104 pa_assert(l
< PA_LOG_LEVEL_MAX
);
109 void pa_log_set_target(pa_log_target_t t
, pa_log_func_t func
) {
110 pa_assert(t
== PA_LOG_USER
|| !func
);
113 user_log_func
= func
;
116 void pa_log_set_show_meta(pa_bool_t b
) {
120 void pa_log_set_show_time(pa_bool_t b
) {
124 void pa_log_set_show_backtrace(unsigned nlevels
) {
125 show_backtrace
= nlevels
;
128 #ifdef HAVE_EXECINFO_H
130 static char* get_backtrace(unsigned show_nframes
) {
133 char **symbols
, *e
, *r
;
137 if (show_nframes
<= 0)
140 n_frames
= backtrace(trace
, PA_ELEMENTSOF(trace
));
145 symbols
= backtrace_symbols(trace
, n_frames
);
150 n
= PA_MIN((unsigned) n_frames
, show_nframes
);
154 for (j
= 0; j
< n
; j
++) {
157 a
+= strlen(symbols
[j
]);
160 r
= pa_xnew(char, a
);
165 for (j
= 0; j
< n
; j
++) {
171 strcpy(e
, symbols
[j
]);
172 e
+= strlen(symbols
[j
]);
184 void pa_log_levelv_meta(
185 pa_log_level_t level
,
194 int saved_errno
= errno
;
197 #ifdef HAVE_EXECINFO_H
201 /* We don't use dynamic memory allocation here to minimize the hit
203 char text
[4096], location
[128], timestamp
[32];
205 pa_assert(level
< PA_LOG_LEVEL_MAX
);
210 if (PA_UNLIKELY((e
= getenv(ENV_LOGLEVEL
)))) {
211 pa_log_level_t eml
= (pa_log_level_t
) atoi(e
);
217 if (PA_LIKELY(level
> ml
)) {
222 pa_vsnprintf(text
, sizeof(text
), format
, ap
);
224 if ((show_meta
|| getenv(ENV_LOGMETA
)) && file
&& line
> 0 && func
)
225 pa_snprintf(location
, sizeof(location
), "[%s:%i %s()] ", file
, line
, func
);
227 pa_snprintf(location
, sizeof(location
), "%s: ", pa_path_get_filename(file
));
231 if (show_time
|| getenv(ENV_LOGTIME
)) {
232 static pa_usec_t start
, last
;
235 u
= pa_rtclock_usec();
245 /* This is not thread safe, but this is a debugging tool only
249 pa_snprintf(timestamp
, sizeof(timestamp
), "(%4llu.%03llu|%4llu.%03llu) ",
250 (unsigned long long) (a
/ PA_USEC_PER_SEC
),
251 (unsigned long long) (((a
/ PA_USEC_PER_MSEC
)) % 1000),
252 (unsigned long long) (r
/ PA_USEC_PER_SEC
),
253 (unsigned long long) (((r
/ PA_USEC_PER_MSEC
)) % 1000));
258 #ifdef HAVE_EXECINFO_H
259 show_bt
= show_backtrace
;
261 if ((e
= getenv(ENV_LOGBACKTRACE
))) {
262 unsigned ebt
= (unsigned) atoi(e
);
268 bt
= get_backtrace(show_bt
);
271 if (!pa_utf8_valid(text
))
272 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
274 for (t
= text
; t
; t
= n
) {
275 if ((n
= strchr(t
, '\n'))) {
283 switch (log_target
) {
284 case PA_LOG_STDERR
: {
285 const char *prefix
= "", *suffix
= "", *grey
= "";
289 /* Yes indeed. Useless, but fun! */
290 if (isatty(STDERR_FILENO
)) {
291 if (level
<= PA_LOG_ERROR
)
292 prefix
= "\x1B[1;31m";
293 else if (level
<= PA_LOG_WARN
)
299 if (grey
[0] || prefix
[0])
304 /* We shouldn't be using dynamic allocation here to
305 * minimize the hit in RT threads */
306 local_t
= pa_utf8_to_locale(t
);
308 fprintf(stderr
, "%s%c: %s%s%s%s%s%s\n", timestamp
, level_to_char
[level
], location
, prefix
, t
, grey
, pa_strempty(bt
), suffix
);
310 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
);
318 case PA_LOG_SYSLOG
: {
321 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
323 local_t
= pa_utf8_to_locale(t
);
325 syslog(level_to_syslog
[level
], "%s%s%s%s", timestamp
, location
, t
, pa_strempty(bt
));
327 syslog(level_to_syslog
[level
], "%s%s%s%s", timestamp
, location
, local_t
, pa_strempty(bt
));
339 pa_snprintf(x
, sizeof(x
), "%s%s%s", timestamp
, location
, t
);
340 user_log_func(level
, x
);
354 void pa_log_level_meta(
355 pa_log_level_t level
,
359 const char *format
, ...) {
362 va_start(ap
, format
);
363 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
367 void pa_log_levelv(pa_log_level_t level
, const char *format
, va_list ap
) {
368 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
371 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
374 va_start(ap
, format
);
375 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);