report.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <stdlib.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/macros.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/progname.h>
  31. #include <dpkg/color.h>
  32. #include <dpkg/report.h>
  33. static int piped_mode = _IOLBF;
  34. void
  35. dpkg_set_report_piped_mode(int mode)
  36. {
  37. piped_mode = mode;
  38. }
  39. void
  40. dpkg_set_report_buffer(FILE *fp)
  41. {
  42. if (isatty(fileno(fp)))
  43. setvbuf(fp, NULL, _IONBF, 0);
  44. else
  45. setvbuf(fp, NULL, piped_mode, 0);
  46. }
  47. static int warn_count = 0;
  48. int
  49. warning_get_count(void)
  50. {
  51. return warn_count;
  52. }
  53. void
  54. warningv(const char *fmt, va_list args)
  55. {
  56. char *buf = NULL;
  57. warn_count++;
  58. m_vasprintf(&buf, fmt, args);
  59. fprintf(stderr, "%s%s:%s %s%s:%s %s\n",
  60. color_get(COLOR_PROG), dpkg_get_progname(), color_reset(),
  61. color_get(COLOR_WARN), _("warning"), color_reset(), buf);
  62. free(buf);
  63. }
  64. void
  65. warning(const char *fmt, ...)
  66. {
  67. va_list args;
  68. va_start(args, fmt);
  69. warningv(fmt, args);
  70. va_end(args);
  71. }
  72. void
  73. notice(const char *fmt, ...)
  74. {
  75. char *buf = NULL;
  76. va_list args;
  77. va_start(args, fmt);
  78. m_vasprintf(&buf, fmt, args);
  79. va_end(args);
  80. fprintf(stderr, "%s%s:%s %s\n",
  81. color_get(COLOR_PROG), dpkg_get_progname(), color_reset(), buf);
  82. free(buf);
  83. }
  84. void
  85. info(const char *fmt, ...)
  86. {
  87. char *buf;
  88. va_list args;
  89. va_start(args, fmt);
  90. m_vasprintf(&buf, fmt, args);
  91. va_end(args);
  92. printf("%s%s:%s %s\n",
  93. color_get(COLOR_PROG), dpkg_get_progname(), color_reset(), buf);
  94. free(buf);
  95. }