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