]> code.delx.au - pulseaudio/blob - src/pulsecore/log.c
A lot of more work to get the lock-free stuff in place
[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 <assert.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <string.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/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 void pa_log_set_maximal_level(pa_log_level_t l) {
83 assert(l < PA_LOG_LEVEL_MAX);
84 maximal_level = l;
85 }
86
87 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
88 assert(t == PA_LOG_USER || !func);
89 log_target = t;
90 user_log_func = func;
91 }
92
93 void pa_log_levelv_meta(
94 pa_log_level_t level,
95 const char*file,
96 int line,
97 const char *func,
98 const char *format,
99 va_list ap) {
100
101 const char *e;
102 char *text, *t, *n, *location;
103
104 assert(level < PA_LOG_LEVEL_MAX);
105 assert(format);
106
107 if ((e = getenv(ENV_LOGLEVEL)))
108 maximal_level = atoi(e);
109
110 if (level > maximal_level)
111 return;
112
113 text = pa_vsprintf_malloc(format, ap);
114
115 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
116 location = pa_sprintf_malloc("[%s:%i %s()] ", file, line, func);
117 else if (file)
118 location = pa_sprintf_malloc("%s: ", pa_path_get_filename(file));
119 else
120 location = pa_xstrdup("");
121
122 if (!pa_utf8_valid(text))
123 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
124
125 for (t = text; t; t = n) {
126 if ((n = strchr(t, '\n'))) {
127 *n = 0;
128 n++;
129 }
130
131 if (!*t)
132 continue;
133
134 switch (log_target) {
135 case PA_LOG_STDERR: {
136 const char *prefix = "", *suffix = "";
137 char *local_t;
138
139 #ifndef OS_IS_WIN32
140 /* Yes indeed. Useless, but fun! */
141 if (isatty(STDERR_FILENO)) {
142 if (level <= PA_LOG_ERROR) {
143 prefix = "\x1B[1;31m";
144 suffix = "\x1B[0m";
145 } else if (level <= PA_LOG_WARN) {
146 prefix = "\x1B[1m";
147 suffix = "\x1B[0m";
148 }
149 }
150 #endif
151
152 local_t = pa_utf8_to_locale(t);
153 if (!local_t)
154 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
155 else {
156 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
157 pa_xfree(local_t);
158 }
159
160 break;
161 }
162
163 #ifdef HAVE_SYSLOG_H
164 case PA_LOG_SYSLOG: {
165 char *local_t;
166
167 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
168
169 local_t = pa_utf8_to_locale(t);
170 if (!local_t)
171 syslog(level_to_syslog[level], "%s%s", location, t);
172 else {
173 syslog(level_to_syslog[level], "%s%s", location, local_t);
174 pa_xfree(local_t);
175 }
176
177 closelog();
178 break;
179 }
180 #endif
181
182 case PA_LOG_USER: {
183 char *x;
184
185 x = pa_sprintf_malloc("%s%s", location, t);
186 user_log_func(level, x);
187 pa_xfree(x);
188
189 break;
190 }
191
192 case PA_LOG_NULL:
193 default:
194 break;
195 }
196 }
197
198 pa_xfree(text);
199 pa_xfree(location);
200 }
201
202 void pa_log_level_meta(
203 pa_log_level_t level,
204 const char*file,
205 int line,
206 const char *func,
207 const char *format, ...) {
208
209 va_list ap;
210 va_start(ap, format);
211 pa_log_levelv_meta(level, file, line, func, format, ap);
212 va_end(ap);
213 }
214
215 void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
216 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
217 }
218
219 void pa_log_level(pa_log_level_t level, const char *format, ...) {
220 va_list ap;
221 va_start(ap, format);
222 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
223 va_end(ap);
224 }