Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 2 | * $Id: debug.c,v 1.4 2005/06/14 22:41:51 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright Metaparadigm Pte. Ltd. 2004. |
| 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public (LGPL) |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details: http://www.gnu.org/ |
| 16 | * |
| 17 | */ |
| 18 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 19 | #include "config.h" |
| 20 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdarg.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 25 | |
| 26 | #if HAVE_SYSLOG_H |
| 27 | # include <syslog.h> |
| 28 | #endif /* HAVE_SYSLOG_H */ |
| 29 | |
| 30 | #if HAVE_UNISTD_H |
| 31 | # include <unistd.h> |
| 32 | #endif /* HAVE_UNISTD_H */ |
| 33 | |
| 34 | #if HAVE_SYS_PARAM_H |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 35 | #include <sys/param.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 36 | #endif /* HAVE_SYS_PARAM_H */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 37 | |
| 38 | #include "debug.h" |
| 39 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 40 | static int _syslog = 0; |
| 41 | static int _debug = 0; |
| 42 | |
| 43 | void mc_set_debug(int debug) { _debug = debug; } |
| 44 | int mc_get_debug() { return _debug; } |
| 45 | |
| 46 | extern void mc_set_syslog(int syslog) |
| 47 | { |
| 48 | _syslog = syslog; |
| 49 | } |
| 50 | |
| 51 | void mc_abort(const char *msg, ...) |
| 52 | { |
| 53 | va_list ap; |
| 54 | va_start(ap, msg); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 55 | #if HAVE_VSYSLOG |
| 56 | if(_syslog) { |
| 57 | vsyslog(LOG_ERR, msg, ap); |
| 58 | } else |
| 59 | #endif |
| 60 | vprintf(msg, ap); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 61 | exit(1); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void mc_debug(const char *msg, ...) |
| 66 | { |
| 67 | va_list ap; |
| 68 | if(_debug) { |
| 69 | va_start(ap, msg); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 70 | #if HAVE_VSYSLOG |
| 71 | if(_syslog) { |
| 72 | vsyslog(LOG_DEBUG, msg, ap); |
| 73 | } else |
| 74 | #endif |
| 75 | vprintf(msg, ap); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | void mc_error(const char *msg, ...) |
| 80 | { |
| 81 | va_list ap; |
| 82 | va_start(ap, msg); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 83 | #if HAVE_VSYSLOG |
| 84 | if(_syslog) { |
| 85 | vsyslog(LOG_ERR, msg, ap); |
| 86 | } else |
| 87 | #endif |
| 88 | vfprintf(stderr, msg, ap); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void mc_info(const char *msg, ...) |
| 92 | { |
| 93 | va_list ap; |
| 94 | va_start(ap, msg); |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 95 | #if HAVE_VSYSLOG |
| 96 | if(_syslog) { |
| 97 | vsyslog(LOG_INFO, msg, ap); |
| 98 | } else |
| 99 | #endif |
| 100 | vfprintf(stderr, msg, ap); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 101 | } |