qemu-error.c 9.5 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. int error_printf(const char *fmt, ...)
  25. {
  26. va_list ap;
  27. int ret;
  28. va_start(ap, fmt);
  29. ret = error_vprintf(fmt, ap);
  30. va_end(ap);
  31. return ret;
  32. }
  33. int error_printf_unless_qmp(const char *fmt, ...)
  34. {
  35. va_list ap;
  36. int ret;
  37. va_start(ap, fmt);
  38. ret = error_vprintf_unless_qmp(fmt, ap);
  39. va_end(ap);
  40. return ret;
  41. }
  42. static Location std_loc = {
  43. .kind = LOC_NONE
  44. };
  45. static Location *cur_loc = &std_loc;
  46. /*
  47. * Push location saved in LOC onto the location stack, return it.
  48. * The top of that stack is the current location.
  49. * Needs a matching loc_pop().
  50. */
  51. Location *loc_push_restore(Location *loc)
  52. {
  53. assert(!loc->prev);
  54. loc->prev = cur_loc;
  55. cur_loc = loc;
  56. return loc;
  57. }
  58. /*
  59. * Initialize *LOC to "nowhere", push it onto the location stack.
  60. * The top of that stack is the current location.
  61. * Needs a matching loc_pop().
  62. * Return LOC.
  63. */
  64. Location *loc_push_none(Location *loc)
  65. {
  66. loc->kind = LOC_NONE;
  67. loc->prev = NULL;
  68. return loc_push_restore(loc);
  69. }
  70. /*
  71. * Pop the location stack.
  72. * LOC must be the current location, i.e. the top of the stack.
  73. */
  74. Location *loc_pop(Location *loc)
  75. {
  76. assert(cur_loc == loc && loc->prev);
  77. cur_loc = loc->prev;
  78. loc->prev = NULL;
  79. return loc;
  80. }
  81. /*
  82. * Save the current location in LOC, return LOC.
  83. */
  84. Location *loc_save(Location *loc)
  85. {
  86. *loc = *cur_loc;
  87. loc->prev = NULL;
  88. return loc;
  89. }
  90. /*
  91. * Change the current location to the one saved in LOC.
  92. */
  93. void loc_restore(Location *loc)
  94. {
  95. Location *prev = cur_loc->prev;
  96. assert(!loc->prev);
  97. *cur_loc = *loc;
  98. cur_loc->prev = prev;
  99. }
  100. /*
  101. * Change the current location to "nowhere in particular".
  102. */
  103. void loc_set_none(void)
  104. {
  105. cur_loc->kind = LOC_NONE;
  106. }
  107. /*
  108. * Change the current location to argument ARGV[IDX..IDX+CNT-1].
  109. */
  110. void loc_set_cmdline(char **argv, int idx, int cnt)
  111. {
  112. cur_loc->kind = LOC_CMDLINE;
  113. cur_loc->num = cnt;
  114. cur_loc->ptr = argv + idx;
  115. }
  116. /*
  117. * Change the current location to file FNAME, line LNO.
  118. */
  119. void loc_set_file(const char *fname, int lno)
  120. {
  121. assert (fname || cur_loc->kind == LOC_FILE);
  122. cur_loc->kind = LOC_FILE;
  123. cur_loc->num = lno;
  124. if (fname) {
  125. cur_loc->ptr = fname;
  126. }
  127. }
  128. static const char *progname;
  129. /*
  130. * Set the program name for error_print_loc().
  131. */
  132. static void error_set_progname(const char *argv0)
  133. {
  134. const char *p = strrchr(argv0, '/');
  135. progname = p ? p + 1 : argv0;
  136. }
  137. const char *error_get_progname(void)
  138. {
  139. return progname;
  140. }
  141. /*
  142. * Print current location to current monitor if we have one, else to stderr.
  143. */
  144. static void print_loc(void)
  145. {
  146. const char *sep = "";
  147. int i;
  148. const char *const *argp;
  149. if (!cur_mon && progname) {
  150. fprintf(stderr, "%s:", progname);
  151. sep = " ";
  152. }
  153. switch (cur_loc->kind) {
  154. case LOC_CMDLINE:
  155. argp = cur_loc->ptr;
  156. for (i = 0; i < cur_loc->num; i++) {
  157. error_printf("%s%s", sep, argp[i]);
  158. sep = " ";
  159. }
  160. error_printf(": ");
  161. break;
  162. case LOC_FILE:
  163. error_printf("%s:", (const char *)cur_loc->ptr);
  164. if (cur_loc->num) {
  165. error_printf("%d:", cur_loc->num);
  166. }
  167. error_printf(" ");
  168. break;
  169. default:
  170. error_printf("%s", sep);
  171. }
  172. }
  173. bool enable_timestamp_msg;
  174. /*
  175. * Print a message to current monitor if we have one, else to stderr.
  176. * @report_type is the type of message: error, warning or informational.
  177. * Format arguments like vsprintf(). The resulting message should be
  178. * a single phrase, with no newline or trailing punctuation.
  179. * Prepend the current location and append a newline.
  180. */
  181. static void vreport(report_type type, const char *fmt, va_list ap)
  182. {
  183. GTimeVal tv;
  184. gchar *timestr;
  185. if (enable_timestamp_msg && !cur_mon) {
  186. g_get_current_time(&tv);
  187. timestr = g_time_val_to_iso8601(&tv);
  188. error_printf("%s ", timestr);
  189. g_free(timestr);
  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. /* Set the program name for error_print_loc(). */
  353. error_set_progname(argv0);
  354. /*
  355. * This sets up glib logging so libraries using it also print their logs
  356. * through error_report(), warn_report(), info_report().
  357. */
  358. g_log_set_default_handler(qemu_log_func, NULL);
  359. g_warn_if_fail(qemu_glog_domains == NULL);
  360. qemu_glog_domains = g_strdup(g_getenv("G_MESSAGES_DEBUG"));
  361. }