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