debug.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * debug.c - debugging support
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@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 <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <dpkg/dpkg.h>
  26. #include <dpkg/report.h>
  27. #include <dpkg/debug.h>
  28. static int debug_mask = 0;
  29. static FILE *debug_output = NULL;
  30. /**
  31. * Set the debugging output file.
  32. *
  33. * Marks the file descriptor as close-on-exec.
  34. */
  35. void
  36. debug_set_output(FILE *output, const char *filename)
  37. {
  38. setcloexec(fileno(output), filename);
  39. dpkg_set_report_buffer(output);
  40. debug_output = output;
  41. }
  42. /**
  43. * Set the debugging mask.
  44. *
  45. * The mask determines what debugging flags are going to take effect at
  46. * run-time. The output will be set to stderr if it has not been set before.
  47. */
  48. void
  49. debug_set_mask(int mask)
  50. {
  51. debug_mask = mask;
  52. if (!debug_output)
  53. debug_output = stderr;
  54. }
  55. /**
  56. * Check if a debugging flag is currently set on the debugging mask.
  57. */
  58. bool
  59. debug_has_flag(int flag)
  60. {
  61. return debug_mask & flag;
  62. }
  63. /**
  64. * Output a debugging message.
  65. *
  66. * The message will be printed to the previously specified output if the
  67. * specified flag is present in the current debugging mask.
  68. */
  69. void
  70. debug(int flag, const char *fmt, ...)
  71. {
  72. va_list args;
  73. if (!debug_has_flag(flag))
  74. return;
  75. fprintf(debug_output, "D0%05o: ", flag);
  76. va_start(args, fmt);
  77. vfprintf(debug_output, fmt, args);
  78. va_end(args);
  79. putc('\n', debug_output);
  80. }