]> code.delx.au - pulseaudio/blob - src/pulsecore/log.c
really create glitch-free branch
[pulseaudio] / src / pulsecore / log.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYSLOG_H
35 #include <syslog.h>
36 #endif
37
38 #include <pulse/utf8.h>
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
41
42 #include <pulsecore/macro.h>
43 #include <pulsecore/core-util.h>
44
45 #include "log.h"
46
47 #define ENV_LOGLEVEL "PULSE_LOG"
48 #define ENV_LOGMETA "PULSE_LOG_META"
49
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;
54
55 #ifdef HAVE_SYSLOG_H
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
62 };
63 #endif
64
65 static const char level_to_char[] = {
66 [PA_LOG_ERROR] = 'E',
67 [PA_LOG_WARN] = 'W',
68 [PA_LOG_NOTICE] = 'N',
69 [PA_LOG_INFO] = 'I',
70 [PA_LOG_DEBUG] = 'D'
71 };
72
73 void pa_log_set_ident(const char *p) {
74 pa_xfree(log_ident);
75 pa_xfree(log_ident_local);
76
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);
80 }
81
82 /* To make valgrind shut up. */
83 static void ident_destructor(void) PA_GCC_DESTRUCTOR;
84 static void ident_destructor(void) {
85 pa_xfree(log_ident);
86 pa_xfree(log_ident_local);
87 }
88
89 void pa_log_set_maximal_level(pa_log_level_t l) {
90 pa_assert(l < PA_LOG_LEVEL_MAX);
91
92 maximal_level = l;
93 }
94
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);
97
98 log_target = t;
99 user_log_func = func;
100 }
101
102 void pa_log_levelv_meta(
103 pa_log_level_t level,
104 const char*file,
105 int line,
106 const char *func,
107 const char *format,
108 va_list ap) {
109
110 const char *e;
111 char *text, *t, *n, *location;
112
113 pa_assert(level < PA_LOG_LEVEL_MAX);
114 pa_assert(format);
115
116 if ((e = getenv(ENV_LOGLEVEL)))
117 maximal_level = atoi(e);
118
119 if (level > maximal_level)
120 return;
121
122 text = pa_vsprintf_malloc(format, ap);
123
124 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
125 location = pa_sprintf_malloc("[%s:%i %s()] ", file, line, func);
126 else if (file)
127 location = pa_sprintf_malloc("%s: ", pa_path_get_filename(file));
128 else
129 location = pa_xstrdup("");
130
131 if (!pa_utf8_valid(text))
132 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
133
134 for (t = text; t; t = n) {
135 if ((n = strchr(t, '\n'))) {
136 *n = 0;
137 n++;
138 }
139
140 if (!*t)
141 continue;
142
143 switch (log_target) {
144 case PA_LOG_STDERR: {
145 const char *prefix = "", *suffix = "";
146 char *local_t;
147
148 #ifndef OS_IS_WIN32
149 /* Yes indeed. Useless, but fun! */
150 if (isatty(STDERR_FILENO)) {
151 if (level <= PA_LOG_ERROR) {
152 prefix = "\x1B[1;31m";
153 suffix = "\x1B[0m";
154 } else if (level <= PA_LOG_WARN) {
155 prefix = "\x1B[1m";
156 suffix = "\x1B[0m";
157 }
158 }
159 #endif
160
161 local_t = pa_utf8_to_locale(t);
162 if (!local_t)
163 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
164 else {
165 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
166 pa_xfree(local_t);
167 }
168
169 break;
170 }
171
172 #ifdef HAVE_SYSLOG_H
173 case PA_LOG_SYSLOG: {
174 char *local_t;
175
176 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
177
178 local_t = pa_utf8_to_locale(t);
179 if (!local_t)
180 syslog(level_to_syslog[level], "%s%s", location, t);
181 else {
182 syslog(level_to_syslog[level], "%s%s", location, local_t);
183 pa_xfree(local_t);
184 }
185
186 closelog();
187 break;
188 }
189 #endif
190
191 case PA_LOG_USER: {
192 char *x;
193
194 x = pa_sprintf_malloc("%s%s", location, t);
195 user_log_func(level, x);
196 pa_xfree(x);
197
198 break;
199 }
200
201 case PA_LOG_NULL:
202 default:
203 break;
204 }
205 }
206
207 pa_xfree(text);
208 pa_xfree(location);
209 }
210
211 void pa_log_level_meta(
212 pa_log_level_t level,
213 const char*file,
214 int line,
215 const char *func,
216 const char *format, ...) {
217
218 va_list ap;
219 va_start(ap, format);
220 pa_log_levelv_meta(level, file, line, func, format, ap);
221 va_end(ap);
222 }
223
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);
226 }
227
228 void pa_log_level(pa_log_level_t level, const char *format, ...) {
229 va_list ap;
230
231 va_start(ap, format);
232 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
233 va_end(ap);
234 }