log.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2009 1&1 Internet AG
  3. *
  4. * This file is part of sip-router, a free SIP server.
  5. *
  6. * sip-router is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * sip-router is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "log.h"
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. static int use_syslog = 0;
  24. static int log_level = LOG_WARNING;
  25. void init_log(char *_prgname, int _use_syslog) {
  26. use_syslog = _use_syslog;
  27. if (use_syslog) {
  28. openlog(_prgname, LOG_PID, LOG_DAEMON);
  29. }
  30. }
  31. void set_log_level(int level) {
  32. log_level = level;
  33. }
  34. void destroy_log(void) {
  35. if (use_syslog) closelog();
  36. }
  37. void log_stderr(char * format, va_list ap)
  38. {
  39. vfprintf(stderr, format, ap);
  40. fflush(stderr);
  41. }
  42. void pdb_log(int priority, char * format, ...) {
  43. va_list ap;
  44. if (priority<=log_level) {
  45. va_start(ap, format);
  46. if (use_syslog) vsyslog(priority, format, ap);
  47. else log_stderr(format, ap);
  48. va_end(ap);
  49. }
  50. }