debug.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * $Id: debug.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #include "config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #if HAVE_SYSLOG_H
  17. # include <syslog.h>
  18. #endif /* HAVE_SYSLOG_H */
  19. #if HAVE_UNISTD_H
  20. # include <unistd.h>
  21. #endif /* HAVE_UNISTD_H */
  22. #if HAVE_SYS_PARAM_H
  23. #include <sys/param.h>
  24. #endif /* HAVE_SYS_PARAM_H */
  25. #include "debug.h"
  26. static int _syslog = 0;
  27. static int _debug = 0;
  28. void mc_set_debug(int debug) { _debug = debug; }
  29. int mc_get_debug(void) { return _debug; }
  30. extern void mc_set_syslog(int syslog)
  31. {
  32. _syslog = syslog;
  33. }
  34. void mc_debug(const char *msg, ...)
  35. {
  36. va_list ap;
  37. if(_debug) {
  38. va_start(ap, msg);
  39. #if HAVE_VSYSLOG
  40. if(_syslog) {
  41. vsyslog(LOG_DEBUG, msg, ap);
  42. } else
  43. #endif
  44. vprintf(msg, ap);
  45. va_end(ap);
  46. }
  47. }
  48. void mc_error(const char *msg, ...)
  49. {
  50. va_list ap;
  51. va_start(ap, msg);
  52. #if HAVE_VSYSLOG
  53. if(_syslog) {
  54. vsyslog(LOG_ERR, msg, ap);
  55. } else
  56. #endif
  57. vfprintf(stderr, msg, ap);
  58. va_end(ap);
  59. }
  60. void mc_info(const char *msg, ...)
  61. {
  62. va_list ap;
  63. va_start(ap, msg);
  64. #if HAVE_VSYSLOG
  65. if(_syslog) {
  66. vsyslog(LOG_INFO, msg, ap);
  67. } else
  68. #endif
  69. vfprintf(stderr, msg, ap);
  70. va_end(ap);
  71. }