Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * $Id: debug.c,v 1.3 2004/08/07 03:11:38 mclark Exp $ |
| 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 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <syslog.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/param.h> |
| 26 | |
| 27 | #include "debug.h" |
| 28 | |
| 29 | |
| 30 | static int _syslog = 0; |
| 31 | static int _debug = 0; |
| 32 | |
| 33 | void mc_set_debug(int debug) { _debug = debug; } |
| 34 | int mc_get_debug() { return _debug; } |
| 35 | |
| 36 | extern void mc_set_syslog(int syslog) |
| 37 | { |
| 38 | _syslog = syslog; |
| 39 | } |
| 40 | |
| 41 | void mc_abort(const char *msg, ...) |
| 42 | { |
| 43 | va_list ap; |
| 44 | va_start(ap, msg); |
| 45 | if(_syslog) vsyslog(LOG_ERR, msg, ap); |
| 46 | else vprintf(msg, ap); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void mc_debug(const char *msg, ...) |
| 52 | { |
| 53 | va_list ap; |
| 54 | if(_debug) { |
| 55 | va_start(ap, msg); |
| 56 | if(_syslog) vsyslog(LOG_DEBUG, msg, ap); |
| 57 | else vprintf(msg, ap); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void mc_error(const char *msg, ...) |
| 62 | { |
| 63 | va_list ap; |
| 64 | va_start(ap, msg); |
| 65 | if(_syslog) vsyslog(LOG_ERR, msg, ap); |
| 66 | else vfprintf(stderr, msg, ap); |
| 67 | } |
| 68 | |
| 69 | void mc_info(const char *msg, ...) |
| 70 | { |
| 71 | va_list ap; |
| 72 | va_start(ap, msg); |
| 73 | if(_syslog) vsyslog(LOG_INFO, msg, ap); |
| 74 | else vfprintf(stderr, msg, ap); |
| 75 | } |