1 | //debug.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | static int roar_debug_stderr_mode = ROAR_DEBUG_MODE_SYSIO; |
---|
39 | static int roar_debug_stderr_fh = ROAR_STDERR; |
---|
40 | static struct roar_vio_calls * roar_debug_stderr_vio = NULL; |
---|
41 | |
---|
42 | void roar_debug_warn_sysio_real(char * func, char * newfunc, char * info) { |
---|
43 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
44 | |
---|
45 | if ( !roar_libroar_iswarn(config) ) |
---|
46 | return; |
---|
47 | |
---|
48 | if ( config->warnings.sysio == ROAR_WARNING_ALWAYS ) { |
---|
49 | if ( newfunc == NULL ) { |
---|
50 | ROAR_WARN("%s(*): This function is obsolete. %s", func, info == NULL ? "" : info); |
---|
51 | } else { |
---|
52 | ROAR_WARN("%s(*): This function is obsolete. Please use %s(...). %s", func, newfunc, info == NULL ? "" : info); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | void roar_debug_warn_obsolete_real(char * func, char * newfunc, char * info) { |
---|
58 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
59 | |
---|
60 | if ( !roar_libroar_iswarn(config) ) |
---|
61 | return; |
---|
62 | |
---|
63 | if ( config->warnings.obsolete == ROAR_WARNING_ALWAYS ) { |
---|
64 | if ( newfunc == NULL ) { |
---|
65 | ROAR_WARN("%s(*): This function is obsolete. %s", func, info == NULL ? "" : info); |
---|
66 | } else { |
---|
67 | ROAR_WARN("%s(*): This function is obsolete. Please use %s(...). %s", func, newfunc, info == NULL ? "" : info); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | void roar_debug_set_stderr_fh(int fh) { |
---|
73 | roar_debug_stderr_fh = fh; |
---|
74 | } |
---|
75 | |
---|
76 | void roar_debug_set_stderr_vio(struct roar_vio_calls * vio) { |
---|
77 | roar_debug_stderr_vio = vio; |
---|
78 | } |
---|
79 | |
---|
80 | void roar_debug_set_stderr_mode(int mode) { |
---|
81 | roar_debug_stderr_mode = mode; |
---|
82 | } |
---|
83 | |
---|
84 | struct roar_vio_calls * roar_debug_get_stderr(void) { |
---|
85 | static struct roar_vio_calls STDERR; |
---|
86 | |
---|
87 | switch (roar_debug_stderr_mode) { |
---|
88 | case ROAR_DEBUG_MODE_SYSIO: |
---|
89 | if ( roar_debug_stderr_fh == -1 ) |
---|
90 | return NULL; |
---|
91 | |
---|
92 | roar_vio_open_fh(&STDERR, roar_debug_stderr_fh); |
---|
93 | |
---|
94 | return &STDERR; |
---|
95 | break; |
---|
96 | case ROAR_DEBUG_MODE_VIO: |
---|
97 | return roar_debug_stderr_vio; |
---|
98 | break; |
---|
99 | default: |
---|
100 | return NULL; |
---|
101 | break; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | void roar_debug_msg_simple(const char *format, ...) { |
---|
106 | struct roar_vio_calls * vio; |
---|
107 | va_list ap; |
---|
108 | int ret; |
---|
109 | char buf[8192]; |
---|
110 | #ifdef ROAR_HAVE_SYSLOG |
---|
111 | size_t len; |
---|
112 | #endif |
---|
113 | |
---|
114 | vio = roar_debug_get_stderr(); |
---|
115 | |
---|
116 | va_start(ap, format); |
---|
117 | ret = vsnprintf(buf, 8192, format, ap); |
---|
118 | va_end(ap); |
---|
119 | |
---|
120 | if ( vio != NULL ) { |
---|
121 | roar_vio_write(vio, buf, ret); |
---|
122 | } else { |
---|
123 | switch (roar_debug_stderr_mode) { |
---|
124 | #ifdef ROAR_HAVE_SYSLOG |
---|
125 | case ROAR_DEBUG_MODE_SYSLOG: |
---|
126 | // strip \n if needed for syslog: |
---|
127 | len = strlen(buf); |
---|
128 | if ( buf[len-1] == '\n' ) |
---|
129 | buf[len-1] = 0; |
---|
130 | |
---|
131 | syslog(LOG_ERR, "%s", buf); // bad to use some defaults |
---|
132 | break; |
---|
133 | #endif |
---|
134 | default: |
---|
135 | return; |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | void roar_debug_msg(int type, unsigned long int line, char * file, char * prefix, char * format, ...) { |
---|
142 | struct roar_vio_calls * vio; |
---|
143 | va_list ap; |
---|
144 | char buf[8192]; |
---|
145 | char * bufp = buf; |
---|
146 | char * typename; |
---|
147 | int ret; |
---|
148 | #ifdef ROAR_HAVE_SYSLOG |
---|
149 | int priority; |
---|
150 | #endif |
---|
151 | size_t len; |
---|
152 | |
---|
153 | switch (type) { |
---|
154 | case ROAR_DEBUG_TYPE_ERROR: typename = "Error"; break; |
---|
155 | case ROAR_DEBUG_TYPE_WARNING: typename = "Warning"; break; |
---|
156 | case ROAR_DEBUG_TYPE_INFO: typename = "Info"; break; |
---|
157 | case ROAR_DEBUG_TYPE_DEBUG: typename = "Debug"; break; |
---|
158 | default: typename = "Unknown Type"; break; |
---|
159 | } |
---|
160 | |
---|
161 | ret = snprintf(buf, sizeof(buf), "(%s: %s:%lu): %s: ", prefix, file, line, typename); |
---|
162 | |
---|
163 | if ( ret > 0 && ret < sizeof(buf) ) { |
---|
164 | bufp += ret; |
---|
165 | } else { |
---|
166 | ret = 0; |
---|
167 | } |
---|
168 | |
---|
169 | len = ret; |
---|
170 | |
---|
171 | va_start(ap, format); |
---|
172 | ret = vsnprintf(bufp, sizeof(buf)-ret, format, ap); |
---|
173 | va_end(ap); |
---|
174 | |
---|
175 | len += ret; |
---|
176 | |
---|
177 | switch (roar_debug_stderr_mode) { |
---|
178 | case ROAR_DEBUG_MODE_SYSIO: |
---|
179 | case ROAR_DEBUG_MODE_VIO: |
---|
180 | if ( (vio = roar_debug_get_stderr()) == NULL ) |
---|
181 | return; |
---|
182 | roar_vio_write(vio, buf, len); |
---|
183 | roar_vio_write(vio, "\n", 1); |
---|
184 | break; |
---|
185 | #ifdef ROAR_HAVE_SYSLOG |
---|
186 | case ROAR_DEBUG_MODE_SYSLOG: |
---|
187 | switch (type) { |
---|
188 | case ROAR_DEBUG_TYPE_ERROR: priority = LOG_ERR; break; |
---|
189 | case ROAR_DEBUG_TYPE_WARNING: priority = LOG_WARNING; break; |
---|
190 | case ROAR_DEBUG_TYPE_INFO: priority = LOG_INFO; break; |
---|
191 | case ROAR_DEBUG_TYPE_DEBUG: priority = LOG_DEBUG; break; |
---|
192 | default: priority = LOG_ERR; break; |
---|
193 | } |
---|
194 | syslog(priority, "%s", buf); |
---|
195 | break; |
---|
196 | #endif |
---|
197 | } |
---|
198 | } |
---|
199 | //ll |
---|