4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
39 #include <pulse/utf8.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/core-util.h>
48 #define ENV_LOGLEVEL "PULSE_LOG"
49 #define ENV_LOGMETA "PULSE_LOG_META"
51 static char *log_ident = NULL, *log_ident_local = NULL;
52 static pa_log_target_t log_target = PA_LOG_STDERR;
53 static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL;
54 static pa_log_level_t maximal_level = PA_LOG_NOTICE;
57 static const int level_to_syslog[] = {
58 [PA_LOG_ERROR] = LOG_ERR,
59 [PA_LOG_WARN] = LOG_WARNING,
60 [PA_LOG_NOTICE] = LOG_NOTICE,
61 [PA_LOG_INFO] = LOG_INFO,
62 [PA_LOG_DEBUG] = LOG_DEBUG
66 static const char level_to_char[] = {
69 [PA_LOG_NOTICE] = 'N',
74 void pa_log_set_ident(const char *p) {
76 pa_xfree(log_ident_local);
78 log_ident = pa_xstrdup(p);
79 if (!(log_ident_local = pa_utf8_to_locale(log_ident)))
80 log_ident_local = pa_xstrdup(log_ident);
83 /* To make valgrind shut up. */
84 static void ident_destructor(void) PA_GCC_DESTRUCTOR;
85 static void ident_destructor(void) {
87 pa_xfree(log_ident_local);
90 void pa_log_set_maximal_level(pa_log_level_t l) {
91 pa_assert(l < PA_LOG_LEVEL_MAX);
96 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
97 pa_assert(t == PA_LOG_USER || !func);
100 user_log_func = func;
103 void pa_log_levelv_meta(
104 pa_log_level_t level,
113 int saved_errno = errno;
115 /* We don't use dynamic memory allocation here to minimize the hit
117 char text[1024], location[128];
119 pa_assert(level < PA_LOG_LEVEL_MAX);
122 if ((e = getenv(ENV_LOGLEVEL)))
123 maximal_level = atoi(e);
125 if (level > maximal_level) {
130 pa_vsnprintf(text, sizeof(text), format, ap);
132 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
133 pa_snprintf(location, sizeof(location), "[%s:%i %s()] ", file, line, func);
135 pa_snprintf(location, sizeof(location), "%s: ", pa_path_get_filename(file));
139 if (!pa_utf8_valid(text))
140 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
142 for (t = text; t; t = n) {
143 if ((n = strchr(t, '\n'))) {
151 switch (log_target) {
152 case PA_LOG_STDERR: {
153 const char *prefix = "", *suffix = "";
157 /* Yes indeed. Useless, but fun! */
158 if (isatty(STDERR_FILENO)) {
159 if (level <= PA_LOG_ERROR) {
160 prefix = "\x1B[1;31m";
162 } else if (level <= PA_LOG_WARN) {
169 /* We shouldn't be using dynamic allocation here to
170 * minimize the hit in RT threads */
171 local_t = pa_utf8_to_locale(t);
173 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, t, suffix);
175 fprintf(stderr, "%c: %s%s%s%s\n", level_to_char[level], location, prefix, local_t, suffix);
183 case PA_LOG_SYSLOG: {
186 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
188 local_t = pa_utf8_to_locale(t);
190 syslog(level_to_syslog[level], "%s%s", location, t);
192 syslog(level_to_syslog[level], "%s%s", location, local_t);
204 pa_snprintf(x, sizeof(x), "%s%s", location, t);
205 user_log_func(level, x);
219 void pa_log_level_meta(
220 pa_log_level_t level,
224 const char *format, ...) {
227 va_start(ap, format);
228 pa_log_levelv_meta(level, file, line, func, format, ap);
232 void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
233 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);
236 void pa_log_level(pa_log_level_t level, const char *format, ...) {
239 va_start(ap, format);
240 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);