2
0

error-report.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. * @report_type is the type of message: error, warning or
  17. * informational.
  18. */
  19. typedef enum {
  20. REPORT_TYPE_ERROR,
  21. REPORT_TYPE_WARNING,
  22. REPORT_TYPE_INFO,
  23. } report_type;
  24. /* Prepend timestamp to messages */
  25. bool message_with_timestamp;
  26. bool error_with_guestname;
  27. const char *error_guest_name;
  28. int error_printf(const char *fmt, ...)
  29. {
  30. va_list ap;
  31. int ret;
  32. va_start(ap, fmt);
  33. ret = error_vprintf(fmt, ap);
  34. va_end(ap);
  35. return ret;
  36. }
  37. static Location std_loc = {
  38. .kind = LOC_NONE
  39. };
  40. static Location *cur_loc = &std_loc;
  41. /*
  42. * Push location saved in LOC onto the location stack, return it.
  43. * The top of that stack is the current location.
  44. * Needs a matching loc_pop().
  45. */
  46. Location *loc_push_restore(Location *loc)
  47. {
  48. assert(!loc->prev);
  49. loc->prev = cur_loc;
  50. cur_loc = loc;
  51. return loc;
  52. }
  53. /*
  54. * Initialize *LOC to "nowhere", push it onto the location stack.
  55. * The top of that stack is the current location.
  56. * Needs a matching loc_pop().
  57. * Return LOC.
  58. */
  59. Location *loc_push_none(Location *loc)
  60. {
  61. loc->kind = LOC_NONE;
  62. loc->prev = NULL;
  63. return loc_push_restore(loc);
  64. }
  65. /*
  66. * Pop the location stack.
  67. * LOC must be the current location, i.e. the top of the stack.
  68. */
  69. Location *loc_pop(Location *loc)
  70. {
  71. assert(cur_loc == loc && loc->prev);
  72. cur_loc = loc->prev;
  73. loc->prev = NULL;
  74. return loc;
  75. }
  76. /*
  77. * Save the current location in LOC, return LOC.
  78. */
  79. Location *loc_save(Location *loc)
  80. {
  81. *loc = *cur_loc;
  82. loc->prev = NULL;
  83. return loc;
  84. }
  85. /*
  86. * Change the current location to the one saved in LOC.
  87. */
  88. void loc_restore(Location *loc)
  89. {
  90. Location *prev = cur_loc->prev;
  91. assert(!loc->prev);
  92. *cur_loc = *loc;
  93. cur_loc->prev = prev;
  94. }
  95. /*
  96. * Change the current location to "nowhere in particular".
  97. */
  98. void loc_set_none(void)
  99. {
  100. cur_loc->kind = LOC_NONE;
  101. }
  102. /*
  103. * Change the current location to argument ARGV[IDX..IDX+CNT-1].
  104. */
  105. void loc_set_cmdline(char **argv, int idx, int cnt)
  106. {
  107. cur_loc->kind = LOC_CMDLINE;
  108. cur_loc->num = cnt;
  109. cur_loc->ptr = argv + idx;
  110. }
  111. /*
  112. * Change the current location to file FNAME, line LNO.
  113. */
  114. void loc_set_file(const char *fname, int lno)
  115. {
  116. assert (fname || cur_loc->kind == LOC_FILE);
  117. cur_loc->kind = LOC_FILE;
  118. cur_loc->num = lno;
  119. if (fname) {
  120. cur_loc->ptr = fname;
  121. }
  122. }
  123. /*
  124. * Print current location to current monitor if we have one, else to stderr.
  125. */
  126. static void print_loc(void)
  127. {
  128. const char *sep = "";
  129. int i;
  130. const char *const *argp;
  131. if (!monitor_cur() && g_get_prgname()) {
  132. error_printf("%s:", g_get_prgname());
  133. sep = " ";
  134. }
  135. switch (cur_loc->kind) {
  136. case LOC_CMDLINE:
  137. argp = cur_loc->ptr;
  138. for (i = 0; i < cur_loc->num; i++) {
  139. error_printf("%s%s", sep, argp[i]);
  140. sep = " ";
  141. }
  142. error_printf(": ");
  143. break;
  144. case LOC_FILE:
  145. error_printf("%s:", (const char *)cur_loc->ptr);
  146. if (cur_loc->num) {
  147. error_printf("%d:", cur_loc->num);
  148. }
  149. error_printf(" ");
  150. break;
  151. default:
  152. error_printf("%s", sep);
  153. }
  154. }
  155. static char *
  156. real_time_iso8601(void)
  157. {
  158. #if GLIB_CHECK_VERSION(2,62,0)
  159. g_autoptr(GDateTime) dt = g_date_time_new_now_utc();
  160. /* ignore deprecation warning, since GLIB_VERSION_MAX_ALLOWED is 2.56 */
  161. #pragma GCC diagnostic push
  162. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  163. return g_date_time_format_iso8601(dt);
  164. #pragma GCC diagnostic pop
  165. #else
  166. GTimeVal tv;
  167. g_get_current_time(&tv);
  168. return g_time_val_to_iso8601(&tv);
  169. #endif
  170. }
  171. /*
  172. * Print a message to current monitor if we have one, else to stderr.
  173. * @report_type is the type of message: error, warning or informational.
  174. * Format arguments like vsprintf(). The resulting message should be
  175. * a single phrase, with no newline or trailing punctuation.
  176. * Prepend the current location and append a newline.
  177. */
  178. G_GNUC_PRINTF(2, 0)
  179. static void vreport(report_type type, const char *fmt, va_list ap)
  180. {
  181. gchar *timestr;
  182. if (message_with_timestamp && !monitor_cur()) {
  183. timestr = real_time_iso8601();
  184. error_printf("%s ", timestr);
  185. g_free(timestr);
  186. }
  187. /* Only prepend guest name if -msg guest-name and -name guest=... are set */
  188. if (error_with_guestname && error_guest_name && !monitor_cur()) {
  189. error_printf("%s ", error_guest_name);
  190. }
  191. print_loc();
  192. switch (type) {
  193. case REPORT_TYPE_ERROR:
  194. break;
  195. case REPORT_TYPE_WARNING:
  196. error_printf("warning: ");
  197. break;
  198. case REPORT_TYPE_INFO:
  199. error_printf("info: ");
  200. break;
  201. }
  202. error_vprintf(fmt, ap);
  203. error_printf("\n");
  204. }
  205. /*
  206. * Print an error message to current monitor if we have one, else to stderr.
  207. * Format arguments like vsprintf(). The resulting message should be
  208. * a single phrase, with no newline or trailing punctuation.
  209. * Prepend the current location and append a newline.
  210. * It's wrong to call this in a QMP monitor. Use error_setg() there.
  211. */
  212. void error_vreport(const char *fmt, va_list ap)
  213. {
  214. vreport(REPORT_TYPE_ERROR, fmt, ap);
  215. }
  216. /*
  217. * Print a warning message to current monitor if we have one, else to stderr.
  218. * Format arguments like vsprintf(). The resulting message should be
  219. * a single phrase, with no newline or trailing punctuation.
  220. * Prepend the current location and append a newline.
  221. */
  222. void warn_vreport(const char *fmt, va_list ap)
  223. {
  224. vreport(REPORT_TYPE_WARNING, fmt, ap);
  225. }
  226. /*
  227. * Print an information message to current monitor if we have one, else to
  228. * stderr.
  229. * Format arguments like vsprintf(). The resulting message should be
  230. * a single phrase, with no newline or trailing punctuation.
  231. * Prepend the current location and append a newline.
  232. */
  233. void info_vreport(const char *fmt, va_list ap)
  234. {
  235. vreport(REPORT_TYPE_INFO, fmt, ap);
  236. }
  237. /*
  238. * Print an error message to current monitor if we have one, else to stderr.
  239. * Format arguments like sprintf(). The resulting message should be
  240. * a single phrase, with no newline or trailing punctuation.
  241. * Prepend the current location and append a newline.
  242. * It's wrong to call this in a QMP monitor. Use error_setg() there.
  243. */
  244. void error_report(const char *fmt, ...)
  245. {
  246. va_list ap;
  247. va_start(ap, fmt);
  248. vreport(REPORT_TYPE_ERROR, fmt, ap);
  249. va_end(ap);
  250. }
  251. /*
  252. * Print a warning message to current monitor if we have one, else to stderr.
  253. * Format arguments like sprintf(). The resulting message should be a
  254. * single phrase, with no newline or trailing punctuation.
  255. * Prepend the current location and append a newline.
  256. */
  257. void warn_report(const char *fmt, ...)
  258. {
  259. va_list ap;
  260. va_start(ap, fmt);
  261. vreport(REPORT_TYPE_WARNING, fmt, ap);
  262. va_end(ap);
  263. }
  264. /*
  265. * Print an information message to current monitor if we have one, else to
  266. * stderr.
  267. * Format arguments like sprintf(). The resulting message should be a
  268. * single phrase, with no newline or trailing punctuation.
  269. * Prepend the current location and append a newline.
  270. */
  271. void info_report(const char *fmt, ...)
  272. {
  273. va_list ap;
  274. va_start(ap, fmt);
  275. vreport(REPORT_TYPE_INFO, fmt, ap);
  276. va_end(ap);
  277. }
  278. /*
  279. * Like error_report(), except print just once.
  280. * If *printed is false, print the message, and flip *printed to true.
  281. * Return whether the message was printed.
  282. */
  283. bool error_report_once_cond(bool *printed, const char *fmt, ...)
  284. {
  285. va_list ap;
  286. assert(printed);
  287. if (*printed) {
  288. return false;
  289. }
  290. *printed = true;
  291. va_start(ap, fmt);
  292. vreport(REPORT_TYPE_ERROR, fmt, ap);
  293. va_end(ap);
  294. return true;
  295. }
  296. /*
  297. * Like warn_report(), except print just once.
  298. * If *printed is false, print the message, and flip *printed to true.
  299. * Return whether the message was printed.
  300. */
  301. bool warn_report_once_cond(bool *printed, const char *fmt, ...)
  302. {
  303. va_list ap;
  304. assert(printed);
  305. if (*printed) {
  306. return false;
  307. }
  308. *printed = true;
  309. va_start(ap, fmt);
  310. vreport(REPORT_TYPE_WARNING, fmt, ap);
  311. va_end(ap);
  312. return true;
  313. }
  314. static char *qemu_glog_domains;
  315. static void qemu_log_func(const gchar *log_domain,
  316. GLogLevelFlags log_level,
  317. const gchar *message,
  318. gpointer user_data)
  319. {
  320. switch (log_level & G_LOG_LEVEL_MASK) {
  321. case G_LOG_LEVEL_DEBUG:
  322. case G_LOG_LEVEL_INFO:
  323. /*
  324. * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug
  325. * messages
  326. */
  327. if (qemu_glog_domains == NULL) {
  328. break;
  329. }
  330. if (strcmp(qemu_glog_domains, "all") != 0 &&
  331. (log_domain == NULL || !strstr(qemu_glog_domains, log_domain))) {
  332. break;
  333. }
  334. /* Fall through */
  335. case G_LOG_LEVEL_MESSAGE:
  336. info_report("%s%s%s",
  337. log_domain ?: "", log_domain ? ": " : "", message);
  338. break;
  339. case G_LOG_LEVEL_WARNING:
  340. warn_report("%s%s%s",
  341. log_domain ?: "", log_domain ? ": " : "", message);
  342. break;
  343. case G_LOG_LEVEL_CRITICAL:
  344. case G_LOG_LEVEL_ERROR:
  345. error_report("%s%s%s",
  346. log_domain ?: "", log_domain ? ": " : "", message);
  347. break;
  348. }
  349. }
  350. void error_init(const char *argv0)
  351. {
  352. const char *p = strrchr(argv0, '/');
  353. /* Set the program name for error_print_loc(). */
  354. g_set_prgname(p ? p + 1 : argv0);
  355. /*
  356. * This sets up glib logging so libraries using it also print their logs
  357. * through error_report(), warn_report(), info_report().
  358. */
  359. g_log_set_default_handler(qemu_log_func, NULL);
  360. g_warn_if_fail(qemu_glog_domains == NULL);
  361. qemu_glog_domains = g_strdup(g_getenv("G_MESSAGES_DEBUG"));
  362. }