4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <polyp/xmalloc.h>
38 #include <polypcore/util.h>
42 #define ENV_LOGLEVEL "POLYP_LOG"
44 static char *log_ident = NULL;
45 static pa_log_target_t log_target = PA_LOG_STDERR;
46 static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL;
47 static pa_log_level_t maximal_level = PA_LOG_NOTICE;
50 static const int level_to_syslog[] = {
51 [PA_LOG_ERROR] = LOG_ERR,
52 [PA_LOG_WARN] = LOG_WARNING,
53 [PA_LOG_NOTICE] = LOG_NOTICE,
54 [PA_LOG_INFO] = LOG_INFO,
55 [PA_LOG_DEBUG] = LOG_DEBUG
59 void pa_log_set_ident(const char *p) {
63 log_ident = pa_xstrdup(p);
66 void pa_log_set_maximal_level(pa_log_level_t l) {
67 assert(l < PA_LOG_LEVEL_MAX);
71 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
72 assert(t == PA_LOG_USER || !func);
77 void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
81 assert(level < PA_LOG_LEVEL_MAX);
83 if ((e = getenv(ENV_LOGLEVEL)))
84 maximal_level = atoi(e);
86 if (level > maximal_level)
89 text = pa_vsprintf_malloc(format, ap);
91 for (t = text; t; t = n) {
92 if ((n = strchr(t, '\n'))) {
100 switch (log_target) {
101 case PA_LOG_STDERR: {
102 const char *prefix = "", *suffix = "";
105 /* Yes indeed. Useless, but fun! */
106 if (isatty(STDERR_FILENO)) {
107 if (level <= PA_LOG_ERROR) {
108 prefix = "\x1B[1;31m";
110 } else if (level <= PA_LOG_WARN) {
117 fprintf(stderr, "%s%s%s\n", prefix, t, suffix);
123 openlog(log_ident ? log_ident : "???", LOG_PID, LOG_USER);
124 syslog(level_to_syslog[level], "%s", t);
130 user_log_func(level, t);
143 void pa_log_level(pa_log_level_t level, const char *format, ...) {
145 va_start(ap, format);
146 pa_log_levelv(level, format, ap);
150 void pa_log_debug(const char *format, ...) {
152 va_start(ap, format);
153 pa_log_levelv(PA_LOG_DEBUG, format, ap);
157 void pa_log_info(const char *format, ...) {
159 va_start(ap, format);
160 pa_log_levelv(PA_LOG_INFO, format, ap);
164 void pa_log_notice(const char *format, ...) {
166 va_start(ap, format);
167 pa_log_levelv(PA_LOG_INFO, format, ap);
171 void pa_log_warn(const char *format, ...) {
173 va_start(ap, format);
174 pa_log_levelv(PA_LOG_WARN, format, ap);
178 void pa_log_error(const char *format, ...) {
180 va_start(ap, format);
181 pa_log_levelv(PA_LOG_ERROR, format, ap);