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
38 #include <pulse/utf8.h>
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
47 #define ENV_LOGLEVEL "PULSE_LOG"
48 #define ENV_LOGMETA "PULSE_LOG_META"
50 static char *log_ident
= NULL
, *log_ident_local
= NULL
;
51 static pa_log_target_t log_target
= PA_LOG_STDERR
;
52 static void (*user_log_func
)(pa_log_level_t l
, const char *s
) = NULL
;
53 static pa_log_level_t maximal_level
= PA_LOG_NOTICE
;
56 static const int level_to_syslog
[] = {
57 [PA_LOG_ERROR
] = LOG_ERR
,
58 [PA_LOG_WARN
] = LOG_WARNING
,
59 [PA_LOG_NOTICE
] = LOG_NOTICE
,
60 [PA_LOG_INFO
] = LOG_INFO
,
61 [PA_LOG_DEBUG
] = LOG_DEBUG
65 static const char level_to_char
[] = {
68 [PA_LOG_NOTICE
] = 'N',
73 void pa_log_set_ident(const char *p
) {
75 pa_xfree(log_ident_local
);
77 log_ident
= pa_xstrdup(p
);
78 if (!(log_ident_local
= pa_utf8_to_locale(log_ident
)))
79 log_ident_local
= pa_xstrdup(log_ident
);
82 /* To make valgrind shut up. */
83 static void ident_destructor(void) PA_GCC_DESTRUCTOR
;
84 static void ident_destructor(void) {
86 pa_xfree(log_ident_local
);
89 void pa_log_set_maximal_level(pa_log_level_t l
) {
90 pa_assert(l
< PA_LOG_LEVEL_MAX
);
95 void pa_log_set_target(pa_log_target_t t
, void (*func
)(pa_log_level_t l
, const char*s
)) {
96 pa_assert(t
== PA_LOG_USER
|| !func
);
102 void pa_log_levelv_meta(
103 pa_log_level_t level
,
111 char *text
, *t
, *n
, *location
;
113 pa_assert(level
< PA_LOG_LEVEL_MAX
);
116 if ((e
= getenv(ENV_LOGLEVEL
)))
117 maximal_level
= atoi(e
);
119 if (level
> maximal_level
)
122 text
= pa_vsprintf_malloc(format
, ap
);
124 if (getenv(ENV_LOGMETA
) && file
&& line
> 0 && func
)
125 location
= pa_sprintf_malloc("[%s:%i %s()] ", file
, line
, func
);
127 location
= pa_sprintf_malloc("%s: ", pa_path_get_filename(file
));
129 location
= pa_xstrdup("");
131 if (!pa_utf8_valid(text
))
132 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
134 for (t
= text
; t
; t
= n
) {
135 if ((n
= strchr(t
, '\n'))) {
143 switch (log_target
) {
144 case PA_LOG_STDERR
: {
145 const char *prefix
= "", *suffix
= "";
149 /* Yes indeed. Useless, but fun! */
150 if (isatty(STDERR_FILENO
)) {
151 if (level
<= PA_LOG_ERROR
) {
152 prefix
= "\x1B[1;31m";
154 } else if (level
<= PA_LOG_WARN
) {
161 local_t
= pa_utf8_to_locale(t
);
163 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, t
, suffix
);
165 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, local_t
, suffix
);
173 case PA_LOG_SYSLOG
: {
176 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
178 local_t
= pa_utf8_to_locale(t
);
180 syslog(level_to_syslog
[level
], "%s%s", location
, t
);
182 syslog(level_to_syslog
[level
], "%s%s", location
, local_t
);
194 x
= pa_sprintf_malloc("%s%s", location
, t
);
195 user_log_func(level
, x
);
211 void pa_log_level_meta(
212 pa_log_level_t level
,
216 const char *format
, ...) {
219 va_start(ap
, format
);
220 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
224 void pa_log_levelv(pa_log_level_t level
, const char *format
, va_list ap
) {
225 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
228 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
231 va_start(ap
, format
);
232 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);