source: roaraudio/libroar/debug.c @ 3614:762276cff748

Last change on this file since 3614:762276cff748 was 3614:762276cff748, checked in by phi, 14 years ago

added roar_debug_set_stderr_vio() to support VIO mode

File size: 4.8 KB
Line 
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
38static int                     roar_debug_stderr_mode = ROAR_DEBUG_MODE_SYSIO;
39static int                     roar_debug_stderr_fh   = ROAR_STDERR;
40static struct roar_vio_calls * roar_debug_stderr_vio  = NULL;
41
42void roar_debug_warn_sysio_real(char * func, char * newfunc, char * info) {
43 struct roar_libroar_config * config = roar_libroar_get_config();
44
45 if ( config->warnings.sysio == ROAR_WARNING_ALWAYS ) {
46  if ( newfunc == NULL ) {
47   ROAR_WARN("%s(*): This function is obsolete. %s", func, info == NULL ? "" : info);
48  } else {
49   ROAR_WARN("%s(*): This function is obsolete. Please use %s(...). %s", func, newfunc, info == NULL ? "" : info);
50  }
51 }
52}
53
54void   roar_debug_set_stderr_fh(int fh) {
55 roar_debug_stderr_fh = fh;
56}
57
58void   roar_debug_set_stderr_vio(struct roar_vio_calls * vio) {
59 roar_debug_stderr_vio = vio;
60}
61
62void   roar_debug_set_stderr_mode(int mode) {
63 roar_debug_stderr_mode = mode;
64}
65
66struct roar_vio_calls * roar_debug_get_stderr(void) {
67 static struct roar_vio_calls STDERR;
68
69 switch (roar_debug_stderr_mode) {
70  case ROAR_DEBUG_MODE_SYSIO:
71    if ( roar_debug_stderr_fh == -1 )
72     return NULL;
73
74    roar_vio_open_fh(&STDERR, roar_debug_stderr_fh);
75
76    return &STDERR;
77   break;
78  case ROAR_DEBUG_MODE_VIO:
79    return roar_debug_stderr_vio;
80   break;
81  default:
82    return NULL;
83   break;
84 }
85}
86
87void roar_debug_msg_simple(const char *format, ...) {
88 struct roar_vio_calls * vio;
89 va_list ap;
90 int ret;
91 char buf[8192];
92 size_t len;
93
94 vio = roar_debug_get_stderr();
95
96 va_start(ap, format);
97 ret = vsnprintf(buf, 8192, format, ap);
98 va_end(ap);
99
100 if ( vio != NULL ) {
101  roar_vio_write(vio, buf, ret);
102 } else {
103  switch (roar_debug_stderr_mode) {
104#ifdef ROAR_HAVE_SYSLOG
105   case ROAR_DEBUG_MODE_SYSLOG:
106     // strip \n if needed for syslog:
107     len = strlen(buf);
108     if ( buf[len-1] == '\n' )
109      buf[len-1] = 0;
110
111     syslog(LOG_ERR, "%s", buf); // bad to use some defaults
112    break;
113#endif
114   default:
115     return;
116    break;
117  }
118 }
119}
120
121void roar_debug_msg(int type, unsigned long int line, char * file, char * prefix, char * format, ...) {
122 struct roar_vio_calls * vio;
123 va_list ap;
124 char    buf[8192];
125 char  * bufp = buf;
126 char  * typename;
127 int     ret;
128 int     priority;
129 size_t  len;
130
131 switch (type) {
132  case ROAR_DEBUG_TYPE_ERROR:   typename = "Error";   break;
133  case ROAR_DEBUG_TYPE_WARNING: typename = "Warning"; break;
134  case ROAR_DEBUG_TYPE_INFO:    typename = "Info";    break;
135  case ROAR_DEBUG_TYPE_DEBUG:   typename = "Debug";   break;
136  default: typename = "Unknown Type"; break;
137 }
138
139 ret = snprintf(buf, sizeof(buf), "(%s: %s:%lu): %s: ", prefix, file, line, typename);
140
141 if ( ret > 0 && ret < sizeof(buf) ) {
142  bufp += ret;
143 } else {
144  ret = 0;
145 }
146
147 len = ret;
148
149 va_start(ap, format);
150 ret = vsnprintf(bufp, sizeof(buf)-ret, format, ap);
151 va_end(ap);
152
153 len += ret;
154
155 switch (roar_debug_stderr_mode) {
156  case ROAR_DEBUG_MODE_SYSIO:
157  case ROAR_DEBUG_MODE_VIO:
158    if ( (vio = roar_debug_get_stderr()) == NULL )
159     return;
160    roar_vio_write(vio, buf, len);
161    roar_vio_write(vio, "\n", 1);
162   break;
163#ifdef ROAR_HAVE_SYSLOG
164  case ROAR_DEBUG_MODE_SYSLOG:
165    switch (type) {
166     case ROAR_DEBUG_TYPE_ERROR:   priority = LOG_ERR;     break;
167     case ROAR_DEBUG_TYPE_WARNING: priority = LOG_WARNING; break;
168     case ROAR_DEBUG_TYPE_INFO:    priority = LOG_INFO;    break;
169     case ROAR_DEBUG_TYPE_DEBUG:   priority = LOG_DEBUG;   break;
170     default: priority = LOG_ERR; break;
171    }
172    syslog(priority, "%s", buf);
173   break;
174#endif
175 }
176}
177//ll
Note: See TracBrowser for help on using the repository browser.