blob: a4d12f17be90f6b3ff4793dad40e8b6f05e137ff [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clark4504df72007-03-13 08:26:20 +00002 * $Id: debug.c,v 1.4 2005/06/14 22:41:51 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
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 Clark4504df72007-03-13 08:26:20 +000019#include "config.h"
20
Michael Clarkf0d08882007-03-13 08:26:18 +000021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdarg.h>
Michael Clark4504df72007-03-13 08:26:20 +000025
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 Clarkf0d08882007-03-13 08:26:18 +000035#include <sys/param.h>
Michael Clark4504df72007-03-13 08:26:20 +000036#endif /* HAVE_SYS_PARAM_H */
Michael Clarkf0d08882007-03-13 08:26:18 +000037
38#include "debug.h"
39
Michael Clarkf0d08882007-03-13 08:26:18 +000040static int _syslog = 0;
41static int _debug = 0;
42
43void mc_set_debug(int debug) { _debug = debug; }
44int mc_get_debug() { return _debug; }
45
46extern void mc_set_syslog(int syslog)
47{
48 _syslog = syslog;
49}
50
51void mc_abort(const char *msg, ...)
52{
53 va_list ap;
54 va_start(ap, msg);
Michael Clark4504df72007-03-13 08:26:20 +000055#if HAVE_VSYSLOG
56 if(_syslog) {
57 vsyslog(LOG_ERR, msg, ap);
58 } else
59#endif
60 vprintf(msg, ap);
Michael Clarkf0d08882007-03-13 08:26:18 +000061 exit(1);
62}
63
64
65void mc_debug(const char *msg, ...)
66{
67 va_list ap;
68 if(_debug) {
69 va_start(ap, msg);
Michael Clark4504df72007-03-13 08:26:20 +000070#if HAVE_VSYSLOG
71 if(_syslog) {
72 vsyslog(LOG_DEBUG, msg, ap);
73 } else
74#endif
75 vprintf(msg, ap);
Michael Clarkf0d08882007-03-13 08:26:18 +000076 }
77}
78
79void mc_error(const char *msg, ...)
80{
81 va_list ap;
82 va_start(ap, msg);
Michael Clark4504df72007-03-13 08:26:20 +000083#if HAVE_VSYSLOG
84 if(_syslog) {
85 vsyslog(LOG_ERR, msg, ap);
86 } else
87#endif
88 vfprintf(stderr, msg, ap);
Michael Clarkf0d08882007-03-13 08:26:18 +000089}
90
91void mc_info(const char *msg, ...)
92{
93 va_list ap;
94 va_start(ap, msg);
Michael Clark4504df72007-03-13 08:26:20 +000095#if HAVE_VSYSLOG
96 if(_syslog) {
97 vsyslog(LOG_INFO, msg, ap);
98 } else
99#endif
100 vfprintf(stderr, msg, ap);
Michael Clarkf0d08882007-03-13 08:26:18 +0000101}