[2650] | 1 | //debug.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[6052] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2015 |
---|
[2650] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[2650] | 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 | |
---|
[3614] | 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; |
---|
[3606] | 41 | |
---|
[2650] | 42 | void roar_debug_warn_sysio_real(char * func, char * newfunc, char * info) { |
---|
[2654] | 43 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
| 44 | |
---|
[3856] | 45 | if ( !roar_libroar_iswarn(config) ) |
---|
| 46 | return; |
---|
| 47 | |
---|
[2654] | 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 | } |
---|
[2650] | 55 | } |
---|
| 56 | |
---|
[3856] | 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 | |
---|
[5109] | 72 | void roar_debug_bin_obsolete(const char * progname, const char * newprog, const char * info) { |
---|
| 73 | if ( info == NULL ) |
---|
| 74 | info = ""; |
---|
| 75 | |
---|
| 76 | if ( newprog == NULL ) { |
---|
| 77 | roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, 0, progname, ROAR_DBG_PREFIX, "This program is obsolete and will be removed soon. %s", info); |
---|
| 78 | } else { |
---|
| 79 | roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, 0, progname, ROAR_DBG_PREFIX, "This program is obsolete and will be removed soon. Please use %s. %s", newprog, info); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
[5949] | 83 | void roar_debug_option_obsolete(const char * progname, const char * option, const char * newopt, const char * info) { |
---|
| 84 | if ( info == NULL ) |
---|
| 85 | info = ""; |
---|
| 86 | |
---|
| 87 | if ( newopt == NULL ) { |
---|
| 88 | roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, 0, progname, ROAR_DBG_PREFIX, "The option \"%s\" is obsolete and will be removed soon. %s", option, info); |
---|
| 89 | } else { |
---|
| 90 | roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, 0, progname, ROAR_DBG_PREFIX, "The option \"%s\" is obsolete and will be removed soon. Please use \"%s\". %s", option, newopt, info); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
[3606] | 94 | void roar_debug_set_stderr_fh(int fh) { |
---|
| 95 | roar_debug_stderr_fh = fh; |
---|
| 96 | } |
---|
| 97 | |
---|
[3614] | 98 | void roar_debug_set_stderr_vio(struct roar_vio_calls * vio) { |
---|
| 99 | roar_debug_stderr_vio = vio; |
---|
| 100 | } |
---|
| 101 | |
---|
[3611] | 102 | void roar_debug_set_stderr_mode(int mode) { |
---|
| 103 | roar_debug_stderr_mode = mode; |
---|
| 104 | } |
---|
| 105 | |
---|
[3606] | 106 | struct roar_vio_calls * roar_debug_get_stderr(void) { |
---|
| 107 | static struct roar_vio_calls STDERR; |
---|
| 108 | |
---|
[3611] | 109 | switch (roar_debug_stderr_mode) { |
---|
| 110 | case ROAR_DEBUG_MODE_SYSIO: |
---|
| 111 | if ( roar_debug_stderr_fh == -1 ) |
---|
| 112 | return NULL; |
---|
| 113 | |
---|
| 114 | roar_vio_open_fh(&STDERR, roar_debug_stderr_fh); |
---|
[3606] | 115 | |
---|
[3611] | 116 | return &STDERR; |
---|
| 117 | break; |
---|
[3614] | 118 | case ROAR_DEBUG_MODE_VIO: |
---|
| 119 | return roar_debug_stderr_vio; |
---|
| 120 | break; |
---|
[3611] | 121 | default: |
---|
| 122 | return NULL; |
---|
| 123 | break; |
---|
| 124 | } |
---|
[3606] | 125 | } |
---|
| 126 | |
---|
[4875] | 127 | // NOTE: This function MUST NOT alter RA's error values nor errno! |
---|
[3607] | 128 | void roar_debug_msg_simple(const char *format, ...) { |
---|
| 129 | struct roar_vio_calls * vio; |
---|
| 130 | va_list ap; |
---|
| 131 | int ret; |
---|
| 132 | char buf[8192]; |
---|
[3904] | 133 | #ifdef ROAR_HAVE_SYSLOG |
---|
[3611] | 134 | size_t len; |
---|
[3904] | 135 | #endif |
---|
[4875] | 136 | int _ra_error = roar_error; |
---|
| 137 | int _sys_error = errno; |
---|
[3607] | 138 | |
---|
[3611] | 139 | vio = roar_debug_get_stderr(); |
---|
[3607] | 140 | |
---|
| 141 | va_start(ap, format); |
---|
[4931] | 142 | ret = vsnprintf(buf, sizeof(buf), format, ap); |
---|
[3607] | 143 | va_end(ap); |
---|
| 144 | |
---|
[4931] | 145 | buf[sizeof(buf)-1] = 0; |
---|
| 146 | |
---|
[3611] | 147 | if ( vio != NULL ) { |
---|
[5111] | 148 | _LIBROAR_IGNORE_RET(roar_vio_write(vio, buf, ret)); |
---|
[4936] | 149 | roar_vio_sync(vio); |
---|
[3611] | 150 | } else { |
---|
| 151 | switch (roar_debug_stderr_mode) { |
---|
| 152 | #ifdef ROAR_HAVE_SYSLOG |
---|
| 153 | case ROAR_DEBUG_MODE_SYSLOG: |
---|
| 154 | // strip \n if needed for syslog: |
---|
| 155 | len = strlen(buf); |
---|
| 156 | if ( buf[len-1] == '\n' ) |
---|
| 157 | buf[len-1] = 0; |
---|
| 158 | |
---|
| 159 | syslog(LOG_ERR, "%s", buf); // bad to use some defaults |
---|
| 160 | break; |
---|
| 161 | #endif |
---|
| 162 | default: |
---|
[4875] | 163 | // do nothing. |
---|
[3611] | 164 | break; |
---|
| 165 | } |
---|
| 166 | } |
---|
[4875] | 167 | |
---|
| 168 | roar_error = _ra_error; |
---|
| 169 | errno = _sys_error; |
---|
[3607] | 170 | } |
---|
| 171 | |
---|
[4784] | 172 | void roar_debug_msg(int type, unsigned long int line, const char * file, const char * prefix, const char * format, ...) { |
---|
[3611] | 173 | struct roar_vio_calls * vio; |
---|
| 174 | va_list ap; |
---|
| 175 | char buf[8192]; |
---|
| 176 | char * bufp = buf; |
---|
| 177 | char * typename; |
---|
| 178 | int ret; |
---|
[3904] | 179 | #ifdef ROAR_HAVE_SYSLOG |
---|
[3611] | 180 | int priority; |
---|
[3904] | 181 | #endif |
---|
[3611] | 182 | size_t len; |
---|
[5201] | 183 | struct roar_error_state error_state; |
---|
| 184 | |
---|
| 185 | roar_err_store(&error_state); |
---|
[3611] | 186 | |
---|
| 187 | switch (type) { |
---|
| 188 | case ROAR_DEBUG_TYPE_ERROR: typename = "Error"; break; |
---|
| 189 | case ROAR_DEBUG_TYPE_WARNING: typename = "Warning"; break; |
---|
| 190 | case ROAR_DEBUG_TYPE_INFO: typename = "Info"; break; |
---|
| 191 | case ROAR_DEBUG_TYPE_DEBUG: typename = "Debug"; break; |
---|
| 192 | default: typename = "Unknown Type"; break; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | ret = snprintf(buf, sizeof(buf), "(%s: %s:%lu): %s: ", prefix, file, line, typename); |
---|
| 196 | |
---|
[5270] | 197 | if ( ret > 0 && ret < (int)sizeof(buf) ) { |
---|
[3611] | 198 | bufp += ret; |
---|
| 199 | } else { |
---|
| 200 | ret = 0; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | len = ret; |
---|
| 204 | |
---|
| 205 | va_start(ap, format); |
---|
| 206 | ret = vsnprintf(bufp, sizeof(buf)-ret, format, ap); |
---|
| 207 | va_end(ap); |
---|
| 208 | |
---|
[4931] | 209 | buf[sizeof(buf)-1] = 0; |
---|
| 210 | |
---|
[3611] | 211 | len += ret; |
---|
| 212 | |
---|
| 213 | switch (roar_debug_stderr_mode) { |
---|
| 214 | case ROAR_DEBUG_MODE_SYSIO: |
---|
| 215 | case ROAR_DEBUG_MODE_VIO: |
---|
[4875] | 216 | if ( (vio = roar_debug_get_stderr()) == NULL ) { |
---|
[5201] | 217 | roar_err_restore(&error_state); |
---|
[3611] | 218 | return; |
---|
[4875] | 219 | } |
---|
[5111] | 220 | _LIBROAR_IGNORE_RET(roar_vio_write(vio, buf, len)); |
---|
| 221 | _LIBROAR_IGNORE_RET(roar_vio_write(vio, "\n", 1)); |
---|
[4936] | 222 | roar_vio_sync(vio); |
---|
[3611] | 223 | break; |
---|
| 224 | #ifdef ROAR_HAVE_SYSLOG |
---|
| 225 | case ROAR_DEBUG_MODE_SYSLOG: |
---|
| 226 | switch (type) { |
---|
| 227 | case ROAR_DEBUG_TYPE_ERROR: priority = LOG_ERR; break; |
---|
| 228 | case ROAR_DEBUG_TYPE_WARNING: priority = LOG_WARNING; break; |
---|
| 229 | case ROAR_DEBUG_TYPE_INFO: priority = LOG_INFO; break; |
---|
| 230 | case ROAR_DEBUG_TYPE_DEBUG: priority = LOG_DEBUG; break; |
---|
| 231 | default: priority = LOG_ERR; break; |
---|
| 232 | } |
---|
| 233 | syslog(priority, "%s", buf); |
---|
| 234 | break; |
---|
| 235 | #endif |
---|
| 236 | } |
---|
[5201] | 237 | roar_err_restore(&error_state); |
---|
[3611] | 238 | } |
---|
[2650] | 239 | //ll |
---|