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/core-util.h>
47 #define ENV_LOGLEVEL "PULSE_LOG"
48 #define ENV_LOGMETA "PULSE_LOG_META"
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;
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
65 void pa_log_set_ident(const char *p) {
69 pa_xfree(log_ident_local);
71 log_ident = pa_xstrdup(p);
72 log_ident_local = pa_utf8_to_locale(log_ident);
74 log_ident_local = pa_xstrdup(log_ident);
77 void pa_log_set_maximal_level(pa_log_level_t l) {
78 assert(l < PA_LOG_LEVEL_MAX);
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);
88 void pa_log_levelv_meta(
97 char *text, *t, *n, *location;
99 assert(level < PA_LOG_LEVEL_MAX);
102 if ((e = getenv(ENV_LOGLEVEL)))
103 maximal_level = atoi(e);
105 if (level > maximal_level)
108 text = pa_vsprintf_malloc(format, ap);
110 if (getenv(ENV_LOGMETA) && file && line > 0 && func)
111 location = pa_sprintf_malloc("[%s:%i %s()] ", file, line, func);
113 location = pa_sprintf_malloc("%s: ", pa_path_get_filename(file));
115 location = pa_xstrdup("");
117 if (!pa_utf8_valid(text))
118 pa_log_level(level, __FILE__": invalid UTF-8 string following below:");
120 for (t = text; t; t = n) {
121 if ((n = strchr(t, '\n'))) {
129 switch (log_target) {
130 case PA_LOG_STDERR: {
131 const char *prefix = "", *suffix = "";
135 /* Yes indeed. Useless, but fun! */
136 if (isatty(STDERR_FILENO)) {
137 if (level <= PA_LOG_ERROR) {
138 prefix = "\x1B[1;31m";
140 } else if (level <= PA_LOG_WARN) {
147 local_t = pa_utf8_to_locale(t);
149 fprintf(stderr, "%s%s%s%s\n", location, prefix, t, suffix);
151 fprintf(stderr, "%s%s%s%s\n", location, prefix, local_t, suffix);
159 case PA_LOG_SYSLOG: {
162 openlog(log_ident_local ? log_ident_local : "???", LOG_PID, LOG_USER);
164 local_t = pa_utf8_to_locale(t);
166 syslog(level_to_syslog[level], "%s%s", location, t);
168 syslog(level_to_syslog[level], "%s%s", location, local_t);
180 x = pa_sprintf_malloc("%s%s", location, t);
181 user_log_func(level, x);
197 void pa_log_level_meta(
198 pa_log_level_t level,
202 const char *format, ...) {
205 va_start(ap, format);
206 pa_log_levelv_meta(level, file, line, func, format, ap);
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);
214 void pa_log_level(pa_log_level_t level, const char *format, ...) {
216 va_start(ap, format);
217 pa_log_levelv_meta(level, NULL, 0, NULL, format, ap);