log.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * dpkg - main program for package management
  3. * log.c - logging related functions
  4. *
  5. * Copyright © 2005 Scott James Remnant <scott@netsplit.com>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <dpkg/i18n.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/dpkg-db.h>
  31. const char *log_file = NULL;
  32. void
  33. log_message(const char *fmt, ...)
  34. {
  35. static struct varbuf log;
  36. static FILE *logfd = NULL;
  37. char time_str[20];
  38. time_t now;
  39. va_list al;
  40. if (!log_file)
  41. return;
  42. if (!logfd) {
  43. logfd = fopen(log_file, "a");
  44. if (!logfd) {
  45. fprintf(stderr, _("couldn't open log `%s': %s\n"),
  46. log_file, strerror(errno));
  47. log_file = NULL;
  48. return;
  49. }
  50. setlinebuf(logfd);
  51. setcloexec(fileno(logfd), log_file);
  52. }
  53. va_start(al, fmt);
  54. varbufreset(&log);
  55. varbufvprintf(&log, fmt, al);
  56. varbufaddc(&log, 0);
  57. va_end(al);
  58. time(&now);
  59. strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S",
  60. localtime(&now));
  61. fprintf(logfd, "%s %s\n", time_str, log.buf);
  62. }
  63. struct pipef *status_pipes = NULL;
  64. void
  65. statusfd_send(const char *fmt, ...)
  66. {
  67. static struct varbuf vb;
  68. struct pipef *pipef;
  69. const char *p;
  70. int r, l;
  71. va_list al;
  72. if (!status_pipes)
  73. return;
  74. va_start(al, fmt);
  75. varbufreset(&vb);
  76. varbufvprintf(&vb, fmt, al);
  77. /* Sanitize string to not include new lines, as front-ends should be
  78. * doing their own word-wrapping. */
  79. varbufsubstc(&vb, '\n', ' ');
  80. varbufaddc(&vb, '\n');
  81. va_end(al);
  82. for (pipef = status_pipes; pipef; pipef = pipef->next) {
  83. for (p = vb.buf, l = vb.used; l; p += r, l -= r) {
  84. r = write(pipef->fd, p, l);
  85. if (r < 0)
  86. ohshite(_("unable to write to status fd %d"),
  87. pipef->fd);
  88. assert(r && r <= l);
  89. }
  90. }
  91. }