report.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * reoport.c - message reporting
  4. *
  5. * Copyright © 2004-2005 Scott James Remnant <scott@netsplit.com>
  6. * Copyright © 2008-2013 Guillem Jover <guillem@debian.org>
  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 <unistd.h>
  26. #include <dpkg/macros.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/progname.h>
  29. #include <dpkg/color.h>
  30. #include <dpkg/report.h>
  31. static int piped_mode = _IOLBF;
  32. void
  33. dpkg_set_report_piped_mode(int mode)
  34. {
  35. piped_mode = mode;
  36. }
  37. void
  38. dpkg_set_report_buffer(FILE *fp)
  39. {
  40. if (isatty(fileno(fp)))
  41. setvbuf(fp, NULL, _IONBF, 0);
  42. else
  43. setvbuf(fp, NULL, piped_mode, 0);
  44. }
  45. static int warn_count = 0;
  46. int
  47. warning_get_count(void)
  48. {
  49. return warn_count;
  50. }
  51. void
  52. warningv(const char *fmt, va_list args)
  53. {
  54. char buf[1024];
  55. warn_count++;
  56. vsnprintf(buf, sizeof(buf), fmt, args);
  57. fprintf(stderr, "%s%s:%s %s%s:%s %s\n",
  58. color_get(COLOR_PROG), dpkg_get_progname(), color_reset(),
  59. color_get(COLOR_WARN), _("warning"), color_reset(), buf);
  60. }
  61. void
  62. warning(const char *fmt, ...)
  63. {
  64. va_list args;
  65. va_start(args, fmt);
  66. warningv(fmt, args);
  67. va_end(args);
  68. }
  69. void
  70. notice(const char *fmt, ...)
  71. {
  72. char buf[1024];
  73. va_list args;
  74. va_start(args, fmt);
  75. vsnprintf(buf, sizeof(buf), fmt, args);
  76. va_end(args);
  77. fprintf(stderr, "%s%s:%s %s\n",
  78. color_get(COLOR_PROG), dpkg_get_progname(), color_reset(), buf);
  79. }
  80. void
  81. info(const char *fmt, ...)
  82. {
  83. char buf[1024];
  84. va_list args;
  85. va_start(args, fmt);
  86. vsnprintf(buf, sizeof(buf), fmt, args);
  87. va_end(args);
  88. printf("%s%s:%s %s\n",
  89. color_get(COLOR_PROG), dpkg_get_progname(), color_reset(), buf);
  90. }