4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
39 #include <pulse/utf8.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
48 #define ENV_LOGLEVEL "PULSE_LOG"
49 #define ENV_LOGMETA "PULSE_LOG_META"
51 static char *log_ident
= NULL
, *log_ident_local
= NULL
;
52 static pa_log_target_t log_target
= PA_LOG_STDERR
;
53 static void (*user_log_func
)(pa_log_level_t l
, const char *s
) = NULL
;
54 static pa_log_level_t maximal_level
= PA_LOG_NOTICE
;
57 static const int level_to_syslog
[] = {
58 [PA_LOG_ERROR
] = LOG_ERR
,
59 [PA_LOG_WARN
] = LOG_WARNING
,
60 [PA_LOG_NOTICE
] = LOG_NOTICE
,
61 [PA_LOG_INFO
] = LOG_INFO
,
62 [PA_LOG_DEBUG
] = LOG_DEBUG
66 static const char level_to_char
[] = {
69 [PA_LOG_NOTICE
] = 'N',
74 void pa_log_set_ident(const char *p
) {
76 pa_xfree(log_ident_local
);
78 log_ident
= pa_xstrdup(p
);
79 if (!(log_ident_local
= pa_utf8_to_locale(log_ident
)))
80 log_ident_local
= pa_xstrdup(log_ident
);
83 /* To make valgrind shut up. */
84 static void ident_destructor(void) PA_GCC_DESTRUCTOR
;
85 static void ident_destructor(void) {
87 pa_xfree(log_ident_local
);
90 void pa_log_set_maximal_level(pa_log_level_t l
) {
91 pa_assert(l
< PA_LOG_LEVEL_MAX
);
96 void pa_log_set_target(pa_log_target_t t
, void (*func
)(pa_log_level_t l
, const char*s
)) {
97 pa_assert(t
== PA_LOG_USER
|| !func
);
100 user_log_func
= func
;
103 void pa_log_levelv_meta(
104 pa_log_level_t level
,
112 char *text
, *t
, *n
, *location
;
113 int saved_errno
= errno
;
115 pa_assert(level
< PA_LOG_LEVEL_MAX
);
118 if ((e
= getenv(ENV_LOGLEVEL
)))
119 maximal_level
= atoi(e
);
121 if (level
> maximal_level
) {
126 text
= pa_vsprintf_malloc(format
, ap
);
128 if (getenv(ENV_LOGMETA
) && file
&& line
> 0 && func
)
129 location
= pa_sprintf_malloc("[%s:%i %s()] ", file
, line
, func
);
131 location
= pa_sprintf_malloc("%s: ", pa_path_get_filename(file
));
133 location
= pa_xstrdup("");
135 if (!pa_utf8_valid(text
))
136 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
138 for (t
= text
; t
; t
= n
) {
139 if ((n
= strchr(t
, '\n'))) {
147 switch (log_target
) {
148 case PA_LOG_STDERR
: {
149 const char *prefix
= "", *suffix
= "";
153 /* Yes indeed. Useless, but fun! */
154 if (isatty(STDERR_FILENO
)) {
155 if (level
<= PA_LOG_ERROR
) {
156 prefix
= "\x1B[1;31m";
158 } else if (level
<= PA_LOG_WARN
) {
165 local_t
= pa_utf8_to_locale(t
);
167 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, t
, suffix
);
169 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, local_t
, suffix
);
177 case PA_LOG_SYSLOG
: {
180 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
182 local_t
= pa_utf8_to_locale(t
);
184 syslog(level_to_syslog
[level
], "%s%s", location
, t
);
186 syslog(level_to_syslog
[level
], "%s%s", location
, local_t
);
198 x
= pa_sprintf_malloc("%s%s", location
, t
);
199 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
);