]> code.delx.au - pulseaudio/blob - src/polypcore/log.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / log.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29
30 #ifdef HAVE_SYSLOG_H
31 #include <syslog.h>
32 #endif
33
34 #include <polypcore/xmalloc.h>
35 #include <polypcore/util.h>
36
37 #include "log.h"
38
39 #define ENV_LOGLEVEL "POLYP_LOG"
40
41 static char *log_ident = NULL;
42 static pa_log_target_t log_target = PA_LOG_STDERR;
43 static void (*user_log_func)(pa_log_level_t l, const char *s) = NULL;
44 static pa_log_level_t maximal_level = PA_LOG_NOTICE;
45
46 #ifdef HAVE_SYSLOG_H
47 static const int level_to_syslog[] = {
48 [PA_LOG_ERROR] = LOG_ERR,
49 [PA_LOG_WARN] = LOG_WARNING,
50 [PA_LOG_NOTICE] = LOG_NOTICE,
51 [PA_LOG_INFO] = LOG_INFO,
52 [PA_LOG_DEBUG] = LOG_DEBUG
53 };
54 #endif
55
56 void pa_log_set_ident(const char *p) {
57 if (log_ident)
58 pa_xfree(log_ident);
59
60 log_ident = pa_xstrdup(p);
61 }
62
63 void pa_log_set_maximal_level(pa_log_level_t l) {
64 assert(l < PA_LOG_LEVEL_MAX);
65 maximal_level = l;
66 }
67
68 void pa_log_set_target(pa_log_target_t t, void (*func)(pa_log_level_t l, const char*s)) {
69 assert(t == PA_LOG_USER || !func);
70 log_target = t;
71 user_log_func = func;
72 }
73
74 void pa_log_levelv(pa_log_level_t level, const char *format, va_list ap) {
75 const char *e;
76 assert(level < PA_LOG_LEVEL_MAX);
77
78 if ((e = getenv(ENV_LOGLEVEL)))
79 maximal_level = atoi(e);
80
81 if (level > maximal_level)
82 return;
83
84 switch (log_target) {
85 case PA_LOG_STDERR:
86 vfprintf(stderr, format, ap);
87 break;
88
89 #ifdef HAVE_SYSLOG_H
90 case PA_LOG_SYSLOG:
91 openlog(log_ident ? log_ident : "???", LOG_PID, LOG_USER);
92 vsyslog(level_to_syslog[level], format, ap);
93 closelog();
94 break;
95 #endif
96
97 case PA_LOG_USER: {
98 char *t = pa_vsprintf_malloc(format, ap);
99 assert(user_log_func);
100 user_log_func(level, t);
101 pa_xfree(t);
102 }
103
104 case PA_LOG_NULL:
105 default:
106 break;
107 }
108
109 }
110
111 void pa_log_level(pa_log_level_t level, const char *format, ...) {
112 va_list ap;
113 va_start(ap, format);
114 pa_log_levelv(level, format, ap);
115 va_end(ap);
116 }
117
118 void pa_log_debug(const char *format, ...) {
119 va_list ap;
120 va_start(ap, format);
121 pa_log_levelv(PA_LOG_DEBUG, format, ap);
122 va_end(ap);
123 }
124
125 void pa_log_info(const char *format, ...) {
126 va_list ap;
127 va_start(ap, format);
128 pa_log_levelv(PA_LOG_INFO, format, ap);
129 va_end(ap);
130 }
131
132 void pa_log_notice(const char *format, ...) {
133 va_list ap;
134 va_start(ap, format);
135 pa_log_levelv(PA_LOG_INFO, format, ap);
136 va_end(ap);
137 }
138
139 void pa_log_warn(const char *format, ...) {
140 va_list ap;
141 va_start(ap, format);
142 pa_log_levelv(PA_LOG_WARN, format, ap);
143 va_end(ap);
144 }
145
146 void pa_log_error(const char *format, ...) {
147 va_list ap;
148 va_start(ap, format);
149 pa_log_levelv(PA_LOG_ERROR, format, ap);
150 va_end(ap);
151 }