4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <pulse/utf8.h>
37 #include <pulse/xmalloc.h>
38 #include <pulse/util.h>
40 #include <pulsecore/core-util.h>
44 #define ENV_LOGLEVEL "PULSE_LOG"
45 #define ENV_LOGMETA "PULSE_LOG_META"
47 static char *log_ident
= NULL
, *log_ident_local
= NULL
;
48 static pa_log_target_t log_target
= PA_LOG_STDERR
;
49 static void (*user_log_func
)(pa_log_level_t l
, const char *s
) = NULL
;
50 static pa_log_level_t maximal_level
= PA_LOG_NOTICE
;
53 static const int level_to_syslog
[] = {
54 [PA_LOG_ERROR
] = LOG_ERR
,
55 [PA_LOG_WARN
] = LOG_WARNING
,
56 [PA_LOG_NOTICE
] = LOG_NOTICE
,
57 [PA_LOG_INFO
] = LOG_INFO
,
58 [PA_LOG_DEBUG
] = LOG_DEBUG
62 void pa_log_set_ident(const char *p
) {
66 pa_xfree(log_ident_local
);
68 log_ident
= pa_xstrdup(p
);
69 log_ident_local
= pa_utf8_to_locale(log_ident
);
71 log_ident_local
= pa_xstrdup(log_ident
);
74 void pa_log_set_maximal_level(pa_log_level_t l
) {
75 assert(l
< PA_LOG_LEVEL_MAX
);
79 void pa_log_set_target(pa_log_target_t t
, void (*func
)(pa_log_level_t l
, const char*s
)) {
80 assert(t
== PA_LOG_USER
|| !func
);
85 void pa_log_levelv_meta(
94 char *text
, *t
, *n
, *location
;
96 assert(level
< PA_LOG_LEVEL_MAX
);
99 if ((e
= getenv(ENV_LOGLEVEL
)))
100 maximal_level
= atoi(e
);
102 if (level
> maximal_level
)
105 text
= pa_vsprintf_malloc(format
, ap
);
107 if (getenv(ENV_LOGMETA
) && file
&& line
> 0 && func
)
108 location
= pa_sprintf_malloc("[%s:%i %s()] ", file
, line
, func
);
110 location
= pa_sprintf_malloc("%s: ", pa_path_get_filename(file
));
112 location
= pa_xstrdup("");
114 if (!pa_utf8_valid(text
))
115 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
117 for (t
= text
; t
; t
= n
) {
118 if ((n
= strchr(t
, '\n'))) {
126 switch (log_target
) {
127 case PA_LOG_STDERR
: {
128 const char *prefix
= "", *suffix
= "";
132 /* Yes indeed. Useless, but fun! */
133 if (isatty(STDERR_FILENO
)) {
134 if (level
<= PA_LOG_ERROR
) {
135 prefix
= "\x1B[1;31m";
137 } else if (level
<= PA_LOG_WARN
) {
144 local_t
= pa_utf8_to_locale(t
);
146 fprintf(stderr
, "%s%s%s%s\n", location
, prefix
, t
, suffix
);
148 fprintf(stderr
, "%s%s%s%s\n", location
, prefix
, local_t
, suffix
);
156 case PA_LOG_SYSLOG
: {
159 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
161 local_t
= pa_utf8_to_locale(t
);
163 syslog(level_to_syslog
[level
], "%s%s", location
, t
);
165 syslog(level_to_syslog
[level
], "%s%s", location
, local_t
);
177 x
= pa_sprintf_malloc("%s%s", location
, t
);
178 user_log_func(level
, x
);
194 void pa_log_level_meta(
195 pa_log_level_t level
,
199 const char *format
, ...) {
202 va_start(ap
, format
);
203 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
207 void pa_log_levelv(pa_log_level_t level
, const char *format
, va_list ap
) {
208 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
211 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
213 va_start(ap
, format
);
214 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);