]> code.delx.au - pulseaudio/blob - src/pulsecore/log.c
save and restore errno in log functions
[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 #include <errno.h>
34
35 #ifdef HAVE_SYSLOG_H
36 #include <syslog.h>
37 #endif
38
39 #include <pulse/utf8.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
42
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
45
46 #include "log.h"
47
48 #define ENV_LOGLEVEL "PULSE_LOG"
49 #define ENV_LOGMETA "PULSE_LOG_META"
50
51 static char *log_ident = NULL, *log_ident_local = NULL;
52 static pa_log_target_t log_target = PA_LOG_STDERR;
53 static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL;
54 static pa_log_level_t maximal_level = PA_LOG_NOTICE;
55
56 #ifdef HAVE_SYSLOG_H
57 static const int level_to_syslog[] = {
58 [PA_LOG_ERROR] = LOG_ERR,
59 [PA_LOG_WARN] = LOG_WARNING,
60 [PA_LOG_NOTICE] = LOG_NOTICE,
61 [PA_LOG_INFO] = LOG_INFO,
62 [PA_LOG_DEBUG] = LOG_DEBUG
63 };
64 #endif
65
66 static const char level_to_char[] = {
67 [PA_LOG_ERROR] = 'E',
68 [PA_LOG_WARN] = 'W',
69 [PA_LOG_NOTICE] = 'N',
70 [PA_LOG_INFO] = 'I',
71 [PA_LOG_DEBUG] = 'D'
72 };
73
74 void pa_log_set_ident(const char *p) {
75 pa_xfree(log_ident);
76 pa_xfree(log_ident_local);
77
78 log_ident = pa_xstrdup(p);
79 if (!(log_ident_local = pa_utf8_to_locale(log_ident)))
80 log_ident_local = pa_xstrdup(log_ident);
81 }
82
83 /* To make valgrind shut up. */
84 static void ident_destructor(void) PA_GCC_DESTRUCTOR;
85 static void ident_destructor(void) {
86 pa_xfree(log_ident);
87 pa_xfree(log_ident_local);
88 }
89
90 void pa_log_set_maximal_level(pa_log_level_t l) {
91 pa_assert(l < PA_LOG_LEVEL_MAX);
92
93 maximal_level = l;
94 }
95
96 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
97 pa_assert(t == PA_LOG_USER || !func);
98
99 log_target = t;
100 user_log_func = func;
101 }
102
103 void pa_log_levelv_meta(
104 pa_log_level_t level,
105 const char*file,
106 int line,
107 const char *func,
108 const char *format,
109 va_list ap) {
110
111 const char *e;
112 char *text, *t, *n, *location;
113 int saved_errno = errno;
114
115 pa_assert(level < PA_LOG_LEVEL_MAX);
116 pa_assert(format);
117
118 if ((e = getenv(ENV_LOGLEVEL)))
119 maximal_level = atoi(e);
120
121 if (level > maximal_level) {
122 errno = saved_errno;
123 return;
124 }
125
126 text = pa_vsprintf_malloc(format, ap);
127
128 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
129 location = pa_sprintf_malloc("[%s:%i %s()] ", file, line, func);
130 else if (file)
131 location = pa_sprintf_malloc("%s: ", pa_path_get_filename(file));
132 else
133 location = pa_xstrdup("");
134
135 if (!pa_utf8_valid(text))
136 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
137
138 for (t = text; t; t = n) {
139 if ((n = strchr(t, '\n'))) {
140 *n = 0;
141 n++;
142 }
143
144 if (!*t)
145 continue;
146
147 switch (log_target) {
148 case PA_LOG_STDERR: {
149 const char *prefix = "", *suffix = "";
150 char *local_t;
151
152 #ifndef OS_IS_WIN32
153 /* Yes indeed. Useless, but fun! */
154 if (isatty(STDERR_FILENO)) {
155 if (level <= PA_LOG_ERROR) {
156 prefix = "\x1B[1;31m";
157 suffix = "\x1B[0m";
158 } else if (level <= PA_LOG_WARN) {
159 prefix = "\x1B[1m";
160 suffix = "\x1B[0m";
161 }
162 }
163 #endif
164
165 local_t = pa_utf8_to_locale(t);
166 if (!local_t)
167 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
168 else {
169 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
170 pa_xfree(local_t);
171 }
172
173 break;
174 }
175
176 #ifdef HAVE_SYSLOG_H
177 case PA_LOG_SYSLOG: {
178 char *local_t;
179
180 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
181
182 local_t = pa_utf8_to_locale(t);
183 if (!local_t)
184 syslog(level_to_syslog[level], "%s%s", location, t);
185 else {
186 syslog(level_to_syslog[level], "%s%s", location, local_t);
187 pa_xfree(local_t);
188 }
189
190 closelog();
191 break;
192 }
193 #endif
194
195 case PA_LOG_USER: {
196 char *x;
197
198 x = pa_sprintf_malloc("%s%s", location, t);
199 user_log_func(level, x);
200 pa_xfree(x);
201
202 break;
203 }
204
205 case PA_LOG_NULL:
206 default:
207 break;
208 }
209 }
210
211 pa_xfree(text);
212 pa_xfree(location);
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 }