log.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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. #include <dpkg/fdio.h>
  32. const char *log_file = NULL;
  33. void
  34. log_message(const char *fmt, ...)
  35. {
  36. static struct varbuf log;
  37. static FILE *logfd = NULL;
  38. char time_str[20];
  39. time_t now;
  40. va_list args;
  41. if (!log_file)
  42. return;
  43. if (!logfd) {
  44. logfd = fopen(log_file, "a");
  45. if (!logfd) {
  46. fprintf(stderr, _("couldn't open log `%s': %s\n"),
  47. log_file, strerror(errno));
  48. log_file = NULL;
  49. return;
  50. }
  51. setlinebuf(logfd);
  52. setcloexec(fileno(logfd), log_file);
  53. }
  54. va_start(args, fmt);
  55. varbuf_reset(&log);
  56. varbuf_vprintf(&log, fmt, args);
  57. va_end(args);
  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 {
  64. struct pipef *next;
  65. int fd;
  66. };
  67. static struct pipef *status_pipes = NULL;
  68. void
  69. statusfd_add(int fd)
  70. {
  71. struct pipef *pipe_new;
  72. setcloexec(fd, _("<package status and progress file descriptor>"));
  73. pipe_new = nfmalloc(sizeof(struct pipef));
  74. pipe_new->fd = fd;
  75. pipe_new->next = status_pipes;
  76. status_pipes = pipe_new;
  77. }
  78. void
  79. statusfd_send(const char *fmt, ...)
  80. {
  81. static struct varbuf vb;
  82. struct pipef *pipef;
  83. va_list args;
  84. if (!status_pipes)
  85. return;
  86. va_start(args, fmt);
  87. varbuf_reset(&vb);
  88. varbuf_vprintf(&vb, fmt, args);
  89. /* Sanitize string to not include new lines, as front-ends should be
  90. * doing their own word-wrapping. */
  91. varbuf_map_char(&vb, '\n', ' ');
  92. varbuf_add_char(&vb, '\n');
  93. va_end(args);
  94. for (pipef = status_pipes; pipef; pipef = pipef->next) {
  95. if (fd_write(pipef->fd, vb.buf, vb.used) < 0)
  96. ohshite(_("unable to write to status fd %d"),
  97. pipef->fd);
  98. }
  99. }