2
0

opkg_message.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* opkg_message.c - the opkg package management system
  2. Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
  3. Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include <stdio.h>
  14. #include "opkg_conf.h"
  15. #include "opkg_message.h"
  16. #include "libbb/libbb.h"
  17. struct errlist {
  18. char *errmsg;
  19. struct errlist *next;
  20. };
  21. static struct errlist *error_list_head, *error_list_tail;
  22. static void push_error_list(char *msg)
  23. {
  24. struct errlist *e;
  25. e = xcalloc(1, sizeof(struct errlist));
  26. e->errmsg = xstrdup(msg);
  27. e->next = NULL;
  28. if (error_list_head) {
  29. error_list_tail->next = e;
  30. error_list_tail = e;
  31. } else {
  32. error_list_head = error_list_tail = e;
  33. }
  34. }
  35. void free_error_list(void)
  36. {
  37. struct errlist *err, *err_tmp;
  38. err = error_list_head;
  39. while (err != NULL) {
  40. free(err->errmsg);
  41. err_tmp = err;
  42. err = err->next;
  43. free(err_tmp);
  44. }
  45. }
  46. void print_error_list(void)
  47. {
  48. struct errlist *err = error_list_head;
  49. if (err) {
  50. fprintf(stderr, "Collected errors:\n");
  51. /* Here we print the errors collected and free the list */
  52. while (err != NULL) {
  53. fprintf(stderr, " * %s", err->errmsg);
  54. err = err->next;
  55. }
  56. }
  57. }
  58. void opkg_message(message_level_t level, const char *fmt, ...)
  59. {
  60. va_list ap;
  61. if (conf->verbosity < level)
  62. return;
  63. if (conf->opkg_vmessage) {
  64. /* Pass the message to libopkg users. */
  65. va_start(ap, fmt);
  66. conf->opkg_vmessage(level, fmt, ap);
  67. va_end(ap);
  68. return;
  69. }
  70. va_start(ap, fmt);
  71. if (level == ERROR) {
  72. #define MSG_LEN 4096
  73. char msg[MSG_LEN];
  74. int ret;
  75. ret = vsnprintf(msg, MSG_LEN, fmt, ap);
  76. if (ret < 0) {
  77. fprintf(stderr, "%s: encountered an output or encoding"
  78. " error during vsnprintf.\n", __FUNCTION__);
  79. va_end(ap);
  80. exit(EXIT_FAILURE);
  81. }
  82. if (ret >= MSG_LEN) {
  83. fprintf(stderr, "%s: Message truncated.\n",
  84. __FUNCTION__);
  85. }
  86. push_error_list(msg);
  87. } else {
  88. if (vprintf(fmt, ap) < 0) {
  89. fprintf(stderr, "%s: encountered an output or encoding"
  90. " error during vprintf.\n", __FUNCTION__);
  91. exit(EXIT_FAILURE);
  92. }
  93. }
  94. va_end(ap);
  95. }