162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel/*
262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel * $Id: debug.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel *
462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel * Michael Clark <michael@metaparadigm.com>
662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel *
762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel * This library is free software; you can redistribute it and/or modify
862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel * it under the terms of the MIT license. See COPYING for details.
962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel *
1062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel */
1162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
1262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include "config.h"
1362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
1462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include <stdio.h>
1562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include <stdlib.h>
1662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include <string.h>
1762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include <stdarg.h>
1862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
1962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#if HAVE_SYSLOG_H
2062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel# include <syslog.h>
2162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#endif /* HAVE_SYSLOG_H */
2262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
2362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#if HAVE_UNISTD_H
2462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel# include <unistd.h>
2562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#endif /* HAVE_UNISTD_H */
2662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
2762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#if HAVE_SYS_PARAM_H
2862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include <sys/param.h>
2962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#endif /* HAVE_SYS_PARAM_H */
3062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
3162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#include "debug.h"
3262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
3362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelstatic int _syslog = 0;
3462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelstatic int _debug = 0;
3562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
3662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelvoid mc_set_debug(int debug) { _debug = debug; }
3762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelint mc_get_debug(void) { return _debug; }
3862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
3962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelextern void mc_set_syslog(int syslog)
4062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel{
4162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  _syslog = syslog;
4262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel}
4362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
4462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelvoid mc_debug(const char *msg, ...)
4562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel{
4662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_list ap;
4762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  if(_debug) {
4862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel    va_start(ap, msg);
4962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#if HAVE_VSYSLOG
5062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel    if(_syslog) {
5162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel		vsyslog(LOG_DEBUG, msg, ap);
5262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel	} else
5362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#endif
5462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel		vprintf(msg, ap);
5562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel    va_end(ap);
5662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  }
5762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel}
5862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
5962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelvoid mc_error(const char *msg, ...)
6062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel{
6162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_list ap;
6262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_start(ap, msg);
6362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#if HAVE_VSYSLOG
6462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel    if(_syslog) {
6562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel		vsyslog(LOG_ERR, msg, ap);
6662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel	} else
6762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#endif
6862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel		vfprintf(stderr, msg, ap);
6962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_end(ap);
7062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel}
7162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel
7262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudelvoid mc_info(const char *msg, ...)
7362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel{
7462874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_list ap;
7562874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_start(ap, msg);
7662874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#if HAVE_VSYSLOG
7762874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel    if(_syslog) {
7862874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel		vsyslog(LOG_INFO, msg, ap);
7962874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel	} else
8062874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel#endif
8162874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel		vfprintf(stderr, msg, ap);
8262874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel  va_end(ap);
8362874b3b227d7dc3db44065741cd05d4d8f9dc48Thierry Strudel}
84