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