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