qemu-error.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Error reporting
  3. *
  4. * Copyright (C) 2010 Red Hat Inc.
  5. *
  6. * Authors:
  7. * Markus Armbruster <armbru@redhat.com>,
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "monitor/monitor.h"
  14. #include "qemu/error-report.h"
  15. /*
  16. * Print to current monitor if we have one, else to stderr.
  17. * TODO should return int, so callers can calculate width, but that
  18. * requires surgery to monitor_vprintf(). Left for another day.
  19. */
  20. void error_vprintf(const char *fmt, va_list ap)
  21. {
  22. if (cur_mon && !monitor_cur_is_qmp()) {
  23. monitor_vprintf(cur_mon, fmt, ap);
  24. } else {
  25. vfprintf(stderr, fmt, ap);
  26. }
  27. }
  28. /*
  29. * Print to current monitor if we have one, else to stderr.
  30. * TODO just like error_vprintf()
  31. */
  32. void error_printf(const char *fmt, ...)
  33. {
  34. va_list ap;
  35. va_start(ap, fmt);
  36. error_vprintf(fmt, ap);
  37. va_end(ap);
  38. }
  39. void error_printf_unless_qmp(const char *fmt, ...)
  40. {
  41. va_list ap;
  42. if (!monitor_cur_is_qmp()) {
  43. va_start(ap, fmt);
  44. error_vprintf(fmt, ap);
  45. va_end(ap);
  46. }
  47. }
  48. static Location std_loc = {
  49. .kind = LOC_NONE
  50. };
  51. static Location *cur_loc = &std_loc;
  52. /*
  53. * Push location saved in LOC onto the location stack, return it.
  54. * The top of that stack is the current location.
  55. * Needs a matching loc_pop().
  56. */
  57. Location *loc_push_restore(Location *loc)
  58. {
  59. assert(!loc->prev);
  60. loc->prev = cur_loc;
  61. cur_loc = loc;
  62. return loc;
  63. }
  64. /*
  65. * Initialize *LOC to "nowhere", push it onto the location stack.
  66. * The top of that stack is the current location.
  67. * Needs a matching loc_pop().
  68. * Return LOC.
  69. */
  70. Location *loc_push_none(Location *loc)
  71. {
  72. loc->kind = LOC_NONE;
  73. loc->prev = NULL;
  74. return loc_push_restore(loc);
  75. }
  76. /*
  77. * Pop the location stack.
  78. * LOC must be the current location, i.e. the top of the stack.
  79. */
  80. Location *loc_pop(Location *loc)
  81. {
  82. assert(cur_loc == loc && loc->prev);
  83. cur_loc = loc->prev;
  84. loc->prev = NULL;
  85. return loc;
  86. }
  87. /*
  88. * Save the current location in LOC, return LOC.
  89. */
  90. Location *loc_save(Location *loc)
  91. {
  92. *loc = *cur_loc;
  93. loc->prev = NULL;
  94. return loc;
  95. }
  96. /*
  97. * Change the current location to the one saved in LOC.
  98. */
  99. void loc_restore(Location *loc)
  100. {
  101. Location *prev = cur_loc->prev;
  102. assert(!loc->prev);
  103. *cur_loc = *loc;
  104. cur_loc->prev = prev;
  105. }
  106. /*
  107. * Change the current location to "nowhere in particular".
  108. */
  109. void loc_set_none(void)
  110. {
  111. cur_loc->kind = LOC_NONE;
  112. }
  113. /*
  114. * Change the current location to argument ARGV[IDX..IDX+CNT-1].
  115. */
  116. void loc_set_cmdline(char **argv, int idx, int cnt)
  117. {
  118. cur_loc->kind = LOC_CMDLINE;
  119. cur_loc->num = cnt;
  120. cur_loc->ptr = argv + idx;
  121. }
  122. /*
  123. * Change the current location to file FNAME, line LNO.
  124. */
  125. void loc_set_file(const char *fname, int lno)
  126. {
  127. assert (fname || cur_loc->kind == LOC_FILE);
  128. cur_loc->kind = LOC_FILE;
  129. cur_loc->num = lno;
  130. if (fname) {
  131. cur_loc->ptr = fname;
  132. }
  133. }
  134. static const char *progname;
  135. /*
  136. * Set the program name for error_print_loc().
  137. */
  138. void error_set_progname(const char *argv0)
  139. {
  140. const char *p = strrchr(argv0, '/');
  141. progname = p ? p + 1 : argv0;
  142. }
  143. const char *error_get_progname(void)
  144. {
  145. return progname;
  146. }
  147. /*
  148. * Print current location to current monitor if we have one, else to stderr.
  149. */
  150. static void error_print_loc(void)
  151. {
  152. const char *sep = "";
  153. int i;
  154. const char *const *argp;
  155. if (!cur_mon && progname) {
  156. fprintf(stderr, "%s:", progname);
  157. sep = " ";
  158. }
  159. switch (cur_loc->kind) {
  160. case LOC_CMDLINE:
  161. argp = cur_loc->ptr;
  162. for (i = 0; i < cur_loc->num; i++) {
  163. error_printf("%s%s", sep, argp[i]);
  164. sep = " ";
  165. }
  166. error_printf(": ");
  167. break;
  168. case LOC_FILE:
  169. error_printf("%s:", (const char *)cur_loc->ptr);
  170. if (cur_loc->num) {
  171. error_printf("%d:", cur_loc->num);
  172. }
  173. error_printf(" ");
  174. break;
  175. default:
  176. error_printf("%s", sep);
  177. }
  178. }
  179. bool enable_timestamp_msg;
  180. /*
  181. * Print an error message to current monitor if we have one, else to stderr.
  182. * Format arguments like vsprintf(). The resulting message should be
  183. * a single phrase, with no newline or trailing punctuation.
  184. * Prepend the current location and append a newline.
  185. * It's wrong to call this in a QMP monitor. Use error_setg() there.
  186. */
  187. void error_vreport(const char *fmt, va_list ap)
  188. {
  189. GTimeVal tv;
  190. gchar *timestr;
  191. if (enable_timestamp_msg && !cur_mon) {
  192. g_get_current_time(&tv);
  193. timestr = g_time_val_to_iso8601(&tv);
  194. error_printf("%s ", timestr);
  195. g_free(timestr);
  196. }
  197. error_print_loc();
  198. error_vprintf(fmt, ap);
  199. error_printf("\n");
  200. }
  201. /*
  202. * Print an error message to current monitor if we have one, else to stderr.
  203. * Format arguments like sprintf(). The resulting message should be a
  204. * single phrase, with no newline or trailing punctuation.
  205. * Prepend the current location and append a newline.
  206. * It's wrong to call this in a QMP monitor. Use error_setg() there.
  207. */
  208. void error_report(const char *fmt, ...)
  209. {
  210. va_list ap;
  211. va_start(ap, fmt);
  212. error_vreport(fmt, ap);
  213. va_end(ap);
  214. }