debug.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * debug.c - debugging support
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2011 Guillem Jover <guillem@debian.orgian>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <dpkg/debug.h>
  26. static int debug_mask = 0;
  27. static FILE *debug_output = NULL;
  28. /**
  29. * Set the debugging output file.
  30. */
  31. void
  32. debug_set_output(FILE *output)
  33. {
  34. debug_output = output;
  35. }
  36. /**
  37. * Set the debugging mask.
  38. *
  39. * The mask determines what debugging flags are going to take effect at
  40. * run-time. The output will be set to stderr if it has not been set before.
  41. */
  42. void
  43. debug_set_mask(int mask)
  44. {
  45. debug_mask = mask;
  46. if (!debug_output)
  47. debug_output = stderr;
  48. }
  49. /**
  50. * Check if a debugging flag is currently set on the debugging mask.
  51. */
  52. bool
  53. debug_has_flag(int flag)
  54. {
  55. return debug_mask & flag;
  56. }
  57. /**
  58. * Output a debugging message.
  59. *
  60. * The message will be printed to the previously specified output if the
  61. * specified flag is present in the current debugging mask.
  62. */
  63. void
  64. debug(int flag, const char *fmt, ...)
  65. {
  66. va_list args;
  67. if (!debug_has_flag(flag))
  68. return;
  69. fprintf(debug_output, "D0%05o: ", flag);
  70. va_start(args, fmt);
  71. vfprintf(debug_output, fmt, args);
  72. va_end(args);
  73. putc('\n', debug_output);
  74. }