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
37 #include <pulse/utf8.h>
38 #include <pulse/xmalloc.h>
39 #include <pulse/util.h>
40 #include <pulse/timeval.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/rtclock.h>
45 #include <pulsecore/once.h>
49 #define ENV_LOGLEVEL "PULSE_LOG"
50 #define ENV_LOGMETA "PULSE_LOG_META"
51 #define ENV_LOGTIME "PULSE_LOG_TIME"
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
;
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
68 static const char level_to_char
[] = {
71 [PA_LOG_NOTICE
] = 'N',
76 void pa_log_set_ident(const char *p
) {
78 pa_xfree(log_ident_local
);
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
);
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())
92 pa_xfree(log_ident_local
);
95 void pa_log_set_maximal_level(pa_log_level_t l
) {
96 pa_assert(l
< PA_LOG_LEVEL_MAX
);
101 void pa_log_set_target(pa_log_target_t t
, pa_log_func_t func
) {
102 pa_assert(t
== PA_LOG_USER
|| !func
);
105 user_log_func
= func
;
108 void pa_log_levelv_meta(
109 pa_log_level_t level
,
118 int saved_errno
= errno
;
120 /* We don't use dynamic memory allocation here to minimize the hit
122 char text
[1024], location
[128], timestamp
[32];
124 pa_assert(level
< PA_LOG_LEVEL_MAX
);
127 if ((e
= getenv(ENV_LOGLEVEL
)))
128 maximal_level
= atoi(e
);
130 if (level
> maximal_level
) {
135 pa_vsnprintf(text
, sizeof(text
), format
, ap
);
137 if (getenv(ENV_LOGMETA
) && file
&& line
> 0 && func
)
138 pa_snprintf(location
, sizeof(location
), "[%s:%i %s()] ", file
, line
, func
);
140 pa_snprintf(location
, sizeof(location
), "%s: ", pa_path_get_filename(file
));
144 if (getenv(ENV_LOGTIME
)) {
145 static pa_usec_t start
, last
;
148 u
= pa_rtclock_usec();
158 /* This is not thread safe, but this is a debugging tool only
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));
171 if (!pa_utf8_valid(text
))
172 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
174 for (t
= text
; t
; t
= n
) {
175 if ((n
= strchr(t
, '\n'))) {
183 switch (log_target
) {
184 case PA_LOG_STDERR
: {
185 const char *prefix
= "", *suffix
= "";
189 /* Yes indeed. Useless, but fun! */
190 if (isatty(STDERR_FILENO
)) {
191 if (level
<= PA_LOG_ERROR
) {
192 prefix
= "\x1B[1;31m";
194 } else if (level
<= PA_LOG_WARN
) {
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
);
205 fprintf(stderr
, "%s%c: %s%s%s%s\n", timestamp
, level_to_char
[level
], location
, prefix
, t
, suffix
);
207 fprintf(stderr
, "%s%c: %s%s%s%s\n", timestamp
, level_to_char
[level
], location
, prefix
, local_t
, suffix
);
215 case PA_LOG_SYSLOG
: {
218 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
220 local_t
= pa_utf8_to_locale(t
);
222 syslog(level_to_syslog
[level
], "%s%s%s", timestamp
, location
, t
);
224 syslog(level_to_syslog
[level
], "%s%s%s", timestamp
, location
, local_t
);
236 pa_snprintf(x
, sizeof(x
), "%s%s%s", timestamp
, location
, t
);
237 user_log_func(level
, x
);
251 void pa_log_level_meta(
252 pa_log_level_t level
,
256 const char *format
, ...) {
259 va_start(ap
, format
);
260 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
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
);
268 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
271 va_start(ap
, format
);
272 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);