]> code.delx.au - pulseaudio/blob - src/pulsecore/log.c
Merge dead branch 'lennart'
[pulseaudio] / src / pulsecore / log.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #ifdef HAVE_SYSLOG_H
34 #include <syslog.h>
35 #endif
36
37 #include <pulse/utf8.h>
38 #include <pulse/xmalloc.h>
39 #include <pulse/util.h>
40
41 #include <pulsecore/macro.h>
42 #include <pulsecore/core-util.h>
43
44 #include "log.h"
45
46 #define ENV_LOGLEVEL "PULSE_LOG"
47 #define ENV_LOGMETA "PULSE_LOG_META"
48
49 static char *log_ident = NULL, *log_ident_local = NULL;
50 static pa_log_target_t log_target = PA_LOG_STDERR;
51 static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL;
52 static pa_log_level_t maximal_level = PA_LOG_NOTICE;
53
54 #ifdef HAVE_SYSLOG_H
55 static const int level_to_syslog[] = {
56 [PA_LOG_ERROR] = LOG_ERR,
57 [PA_LOG_WARN] = LOG_WARNING,
58 [PA_LOG_NOTICE] = LOG_NOTICE,
59 [PA_LOG_INFO] = LOG_INFO,
60 [PA_LOG_DEBUG] = LOG_DEBUG
61 };
62 #endif
63
64 static const char level_to_char[] = {
65 [PA_LOG_ERROR] = 'E',
66 [PA_LOG_WARN] = 'W',
67 [PA_LOG_NOTICE] = 'N',
68 [PA_LOG_INFO] = 'I',
69 [PA_LOG_DEBUG] = 'D'
70 };
71
72 void pa_log_set_ident(const char *p) {
73 pa_xfree(log_ident);
74 pa_xfree(log_ident_local);
75
76 log_ident = pa_xstrdup(p);
77 if (!(log_ident_local = pa_utf8_to_locale(log_ident)))
78 log_ident_local = pa_xstrdup(log_ident);
79 }
80
81 /* To make valgrind shut up. */
82 static void ident_destructor(void) PA_GCC_DESTRUCTOR;
83 static void ident_destructor(void) {
84 pa_xfree(log_ident);
85 pa_xfree(log_ident_local);
86 }
87
88 void pa_log_set_maximal_level(pa_log_level_t l) {
89 pa_assert(l < PA_LOG_LEVEL_MAX);
90
91 maximal_level = l;
92 }
93
94 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
95 pa_assert(t == PA_LOG_USER || !func);
96
97 log_target = t;
98 user_log_func = func;
99 }
100
101 void pa_log_levelv_meta(
102 pa_log_level_t level,
103 const char*file,
104 int line,
105 const char *func,
106 const char *format,
107 va_list ap) {
108
109 const char *e;
110 char *t, *n;
111 int saved_errno = errno;
112
113 /* We don't use dynamic memory allocation here to minimize the hit
114 * in RT threads */
115 char text[1024], location[128];
116
117 pa_assert(level < PA_LOG_LEVEL_MAX);
118 pa_assert(format);
119
120 if ((e = getenv(ENV_LOGLEVEL)))
121 maximal_level = atoi(e);
122
123 if (level > maximal_level) {
124 errno = saved_errno;
125 return;
126 }
127
128 pa_vsnprintf(text, sizeof(text), format, ap);
129
130 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
131 pa_snprintf(location, sizeof(location), "[%s:%i %s()] ", file, line, func);
132 else if (file)
133 pa_snprintf(location, sizeof(location), "%s: ", pa_path_get_filename(file));
134 else
135 location[0] = 0;
136
137 if (!pa_utf8_valid(text))
138 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
139
140 for (t = text; t; t = n) {
141 if ((n = strchr(t, '\n'))) {
142 *n = 0;
143 n++;
144 }
145
146 if (!*t)
147 continue;
148
149 switch (log_target) {
150 case PA_LOG_STDERR: {
151 const char *prefix = "", *suffix = "";
152 char *local_t;
153
154 #ifndef OS_IS_WIN32
155 /* Yes indeed. Useless, but fun! */
156 if (isatty(STDERR_FILENO)) {
157 if (level <= PA_LOG_ERROR) {
158 prefix = "\x1B[1;31m";
159 suffix = "\x1B[0m";
160 } else if (level <= PA_LOG_WARN) {
161 prefix = "\x1B[1m";
162 suffix = "\x1B[0m";
163 }
164 }
165 #endif
166
167 /* We shouldn't be using dynamic allocation here to
168 * minimize the hit in RT threads */
169 local_t = pa_utf8_to_locale(t);
170 if (!local_t)
171 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
172 else {
173 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
174 pa_xfree(local_t);
175 }
176
177 break;
178 }
179
180 #ifdef HAVE_SYSLOG_H
181 case PA_LOG_SYSLOG: {
182 char *local_t;
183
184 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
185
186 local_t = pa_utf8_to_locale(t);
187 if (!local_t)
188 syslog(level_to_syslog[level], "%s%s", location, t);
189 else {
190 syslog(level_to_syslog[level], "%s%s", location, local_t);
191 pa_xfree(local_t);
192 }
193
194 closelog();
195 break;
196 }
197 #endif
198
199 case PA_LOG_USER: {
200 char x[1024];
201
202 pa_snprintf(x, sizeof(x), "%s%s", location, t);
203 user_log_func(level, x);
204
205 break;
206 }
207
208 case PA_LOG_NULL:
209 default:
210 break;
211 }
212 }
213
214 errno = saved_errno;
215 }
216
217 void pa_log_level_meta(
218 pa_log_level_t level,
219 const char*file,
220 int line,
221 const char *func,
222 const char *format, ...) {
223
224 va_list ap;
225 va_start(ap, format);
226 pa_log_levelv_meta(level, file, line, func, format, ap);
227 va_end(ap);
228 }
229
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);
232 }
233
234 void pa_log_level(pa_log_level_t level, const char *format, ...) {
235 va_list ap;
236
237 va_start(ap, format);
238 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
239 va_end(ap);
240 }