Add copyright notices to all relevant files. (based on svn log)
[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                 char *local_t;
133
134 #ifndef OS_IS_WIN32
135                 /* Yes indeed. Useless, but fun! */
136                 if (isatty(STDERR_FILENO)) {
137                     if (level <= PA_LOG_ERROR) {
138                         prefix = "\x1B[1;31m";
139                         suffix = "\x1B[0m";
140                     } else if (level <= PA_LOG_WARN) {
141                         prefix = "\x1B[1m";
142                         suffix = "\x1B[0m";
143                     }
144                 }
145 #endif
146
147                 local_t = pa_utf8_to_locale(t);
148                 if (!local_t)
149                     fprintf(stderr, "%s%s%s%s\n", location, prefix, t, suffix);
150                 else {
151                     fprintf(stderr, "%s%s%s%s\n", location, prefix, local_t, suffix);
152                     pa_xfree(local_t);
153                 }
154
155                 break;
156             }
157
158 #ifdef HAVE_SYSLOG_H
159             case PA_LOG_SYSLOG: {
160                 char *local_t;
161
162                 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
163
164                 local_t = pa_utf8_to_locale(t);
165                 if (!local_t)
166                     syslog(level_to_syslog[level], "%s%s", location, t);
167                 else {
168                     syslog(level_to_syslog[level], "%s%s", location, local_t);
169                     pa_xfree(local_t);
170                 }
171
172                 closelog();
173                 break;
174             }
175 #endif
176
177             case PA_LOG_USER: {
178                 char *x;
179
180                 x = pa_sprintf_malloc("%s%s", location, t);
181                 user_log_func(level, x);
182                 pa_xfree(x);
183
184                 break;
185             }
186
187             case PA_LOG_NULL:
188             default:
189                 break;
190         }
191     }
192
193     pa_xfree(text);
194     pa_xfree(location);
195 }
196
197 void pa_log_level_meta(
198         pa_log_level_t level,
199         const char*file,
200         int line,
201         const char *func,
202         const char *format, ...) {
203
204     va_list ap;
205     va_start(ap, format);
206     pa_log_levelv_meta(level, file, line, func, format, ap);
207     va_end(ap);
208 }
209
210 void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
211     pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
212 }
213
214 void pa_log_level(pa_log_level_t level, const char *format, ...) {
215     va_list ap;
216     va_start(ap, format);
217     pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
218     va_end(ap);
219 }