log.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include <dpkg/fdio.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 args;
  40. if (!log_file)
  41. return;
  42. if (!logfd) {
  43. logfd = fopen(log_file, "a");
  44. if (!logfd) {
  45. notice(_("could not open log '%s': %s"),
  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(args, fmt);
  54. varbuf_reset(&log);
  55. varbuf_vprintf(&log, fmt, args);
  56. va_end(args);
  57. time(&now);
  58. strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S",
  59. localtime(&now));
  60. fprintf(logfd, "%s %s\n", time_str, log.buf);
  61. }
  62. struct pipef {
  63. struct pipef *next;
  64. int fd;
  65. };
  66. static struct pipef *status_pipes = NULL;
  67. void
  68. statusfd_add(int fd)
  69. {
  70. struct pipef *pipe_new;
  71. setcloexec(fd, _("<package status and progress file descriptor>"));
  72. pipe_new = nfmalloc(sizeof(struct pipef));
  73. pipe_new->fd = fd;
  74. pipe_new->next = status_pipes;
  75. status_pipes = pipe_new;
  76. }
  77. void
  78. statusfd_send(const char *fmt, ...)
  79. {
  80. static struct varbuf vb;
  81. struct pipef *pipef;
  82. va_list args;
  83. if (!status_pipes)
  84. return;
  85. va_start(args, fmt);
  86. varbuf_reset(&vb);
  87. varbuf_vprintf(&vb, fmt, args);
  88. /* Sanitize string to not include new lines, as front-ends should be
  89. * doing their own word-wrapping. */
  90. varbuf_map_char(&vb, '\n', ' ');
  91. varbuf_add_char(&vb, '\n');
  92. va_end(args);
  93. for (pipef = status_pipes; pipef; pipef = pipef->next) {
  94. if (fd_write(pipef->fd, vb.buf, vb.used) < 0)
  95. ohshite(_("unable to write to status fd %d"),
  96. pipef->fd);
  97. }
  98. }