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>
41 #include <pulsecore/macro.h>
42 #include <pulsecore/core-util.h>
46 #define ENV_LOGLEVEL "PULSE_LOG"
47 #define ENV_LOGMETA "PULSE_LOG_META"
49 static char *log_ident
= NULL
, *log_ident_local
= NULL
;
50 static pa_log_target_t log_target
= PA_LOG_STDERR
;
51 static void (*user_log_func
)(pa_log_level_t l
, const char *s
) = NULL
;
52 static pa_log_level_t maximal_level
= PA_LOG_NOTICE
;
55 static const int level_to_syslog
[] = {
56 [PA_LOG_ERROR
] = LOG_ERR
,
57 [PA_LOG_WARN
] = LOG_WARNING
,
58 [PA_LOG_NOTICE
] = LOG_NOTICE
,
59 [PA_LOG_INFO
] = LOG_INFO
,
60 [PA_LOG_DEBUG
] = LOG_DEBUG
64 static const char level_to_char
[] = {
67 [PA_LOG_NOTICE
] = 'N',
72 void pa_log_set_ident(const char *p
) {
74 pa_xfree(log_ident_local
);
76 log_ident
= pa_xstrdup(p
);
77 if (!(log_ident_local
= pa_utf8_to_locale(log_ident
)))
78 log_ident_local
= pa_xstrdup(log_ident
);
81 /* To make valgrind shut up. */
82 static void ident_destructor(void) PA_GCC_DESTRUCTOR
;
83 static void ident_destructor(void) {
85 pa_xfree(log_ident_local
);
88 void pa_log_set_maximal_level(pa_log_level_t l
) {
89 pa_assert(l
< PA_LOG_LEVEL_MAX
);
94 void pa_log_set_target(pa_log_target_t t
, void (*func
)(pa_log_level_t l
, const char*s
)) {
95 pa_assert(t
== PA_LOG_USER
|| !func
);
101 void pa_log_levelv_meta(
102 pa_log_level_t level
,
111 int saved_errno
= errno
;
113 /* We don't use dynamic memory allocation here to minimize the hit
115 char text
[1024], location
[128];
117 pa_assert(level
< PA_LOG_LEVEL_MAX
);
120 if ((e
= getenv(ENV_LOGLEVEL
)))
121 maximal_level
= atoi(e
);
123 if (level
> maximal_level
) {
128 pa_vsnprintf(text
, sizeof(text
), format
, ap
);
130 if (getenv(ENV_LOGMETA
) && file
&& line
> 0 && func
)
131 pa_snprintf(location
, sizeof(location
), "[%s:%i %s()] ", file
, line
, func
);
133 pa_snprintf(location
, sizeof(location
), "%s: ", pa_path_get_filename(file
));
137 if (!pa_utf8_valid(text
))
138 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
140 for (t
= text
; t
; t
= n
) {
141 if ((n
= strchr(t
, '\n'))) {
149 switch (log_target
) {
150 case PA_LOG_STDERR
: {
151 const char *prefix
= "", *suffix
= "";
155 /* Yes indeed. Useless, but fun! */
156 if (isatty(STDERR_FILENO
)) {
157 if (level
<= PA_LOG_ERROR
) {
158 prefix
= "\x1B[1;31m";
160 } else if (level
<= PA_LOG_WARN
) {
167 /* We shouldn't be using dynamic allocation here to
168 * minimize the hit in RT threads */
169 local_t
= pa_utf8_to_locale(t
);
171 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, t
, suffix
);
173 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, local_t
, suffix
);
181 case PA_LOG_SYSLOG
: {
184 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
186 local_t
= pa_utf8_to_locale(t
);
188 syslog(level_to_syslog
[level
], "%s%s", location
, t
);
190 syslog(level_to_syslog
[level
], "%s%s", location
, local_t
);
202 pa_snprintf(x
, sizeof(x
), "%s%s", location
, t
);
203 user_log_func(level
, x
);
217 void pa_log_level_meta(
218 pa_log_level_t level
,
222 const char *format
, ...) {
225 va_start(ap
, format
);
226 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
230 void pa_log_levelv(pa_log_level_t level
, const char *format
, va_list ap
) {
231 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
234 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
237 va_start(ap
, format
);
238 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);