source: roaraudio/libroar/debug.c @ 3611:e33c2cfeb17e

Last change on this file since 3611:e33c2cfeb17e was 3611:e33c2cfeb17e, checked in by phi, 14 years ago

added support for syslog, wrote additional interface roar_debug_msg()

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