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/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
) {
77 pa_xfree(log_ident_local
);
79 log_ident
= pa_xstrdup(p
);
80 log_ident_local
= pa_utf8_to_locale(log_ident
);
82 log_ident_local
= pa_xstrdup(log_ident
);
85 void pa_log_set_maximal_level(pa_log_level_t l
) {
86 assert(l
< PA_LOG_LEVEL_MAX
);
90 void pa_log_set_target(pa_log_target_t t
, void (*func
)(pa_log_level_t l
, const char*s
)) {
91 assert(t
== PA_LOG_USER
|| !func
);
96 void pa_log_levelv_meta(
105 char *text
, *t
, *n
, *location
;
107 assert(level
< PA_LOG_LEVEL_MAX
);
110 if ((e
= getenv(ENV_LOGLEVEL
)))
111 maximal_level
= atoi(e
);
113 if (level
> maximal_level
)
116 text
= pa_vsprintf_malloc(format
, ap
);
118 if (getenv(ENV_LOGMETA
) && file
&& line
> 0 && func
)
119 location
= pa_sprintf_malloc("[%s:%i %s()] ", file
, line
, func
);
121 location
= pa_sprintf_malloc("%s: ", pa_path_get_filename(file
));
123 location
= pa_xstrdup("");
125 if (!pa_utf8_valid(text
))
126 pa_log_level(level
, __FILE__
": invalid UTF-8 string following below:");
128 for (t
= text
; t
; t
= n
) {
129 if ((n
= strchr(t
, '\n'))) {
137 switch (log_target
) {
138 case PA_LOG_STDERR
: {
139 const char *prefix
= "", *suffix
= "";
143 /* Yes indeed. Useless, but fun! */
144 if (isatty(STDERR_FILENO
)) {
145 if (level
<= PA_LOG_ERROR
) {
146 prefix
= "\x1B[1;31m";
148 } else if (level
<= PA_LOG_WARN
) {
155 local_t
= pa_utf8_to_locale(t
);
157 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, t
, suffix
);
159 fprintf(stderr
, "%c: %s%s%s%s\n", level_to_char
[level
], location
, prefix
, local_t
, suffix
);
167 case PA_LOG_SYSLOG
: {
170 openlog(log_ident_local
? log_ident_local
: "???", LOG_PID
, LOG_USER
);
172 local_t
= pa_utf8_to_locale(t
);
174 syslog(level_to_syslog
[level
], "%s%s", location
, t
);
176 syslog(level_to_syslog
[level
], "%s%s", location
, local_t
);
188 x
= pa_sprintf_malloc("%s%s", location
, t
);
189 user_log_func(level
, x
);
205 void pa_log_level_meta(
206 pa_log_level_t level
,
210 const char *format
, ...) {
213 va_start(ap
, format
);
214 pa_log_levelv_meta(level
, file
, line
, func
, format
, ap
);
218 void pa_log_levelv(pa_log_level_t level
, const char *format
, va_list ap
) {
219 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);
222 void pa_log_level(pa_log_level_t level
, const char *format
, ...) {
224 va_start(ap
, format
);
225 pa_log_levelv_meta(level
, NULL
, 0, NULL
, format
, ap
);