d_error.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. \file lib/db/dbmi_driver/d_error.c
  3. \brief DBMI Library (driver) - error reporting
  4. Taken from DB drivers.
  5. (C) 1999-2008, 2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. \author Joel Jones (CERL/UIUC)
  10. \author Radim Blazek
  11. \author Adopted for DBMI by Martin Landa <landa.martin@gmail.com>
  12. */
  13. #include <string.h>
  14. #include <grass/dbmi.h>
  15. #include <grass/glocale.h>
  16. /* initialize the global struct */
  17. struct state {
  18. char *driver_name;
  19. dbString *errMsg;
  20. } state;
  21. struct state *st = &state;
  22. static void init();
  23. /*!
  24. \brief Init error message for DB driver
  25. Initialize prefix
  26. \param name driver name (eg. "SQLite"))
  27. */
  28. void db_d_init_error(const char *name)
  29. {
  30. if (!st->errMsg) {
  31. st->errMsg = (dbString *) G_malloc(sizeof(dbString));
  32. db_init_string(st->errMsg);
  33. }
  34. G_debug(1, "db_d_init_error(): %s", name);
  35. st->driver_name = G_malloc(strlen(name) + 1);
  36. strcpy(st->driver_name, name);
  37. init();
  38. }
  39. void init()
  40. {
  41. db_set_string(st->errMsg, "");
  42. db_d_append_error(_("DBMI-%s driver error:"), st->driver_name);
  43. db_append_string(st->errMsg, "\n");
  44. }
  45. /*!
  46. \brief Append error message for DB driver
  47. \param frmt formatted message
  48. */
  49. void db_d_append_error(const char *fmt, ...)
  50. {
  51. FILE *fp = NULL;
  52. char *work = NULL;
  53. int count = 0;
  54. va_list ap;
  55. va_start(ap, fmt);
  56. if ((fp = tmpfile())) {
  57. count = vfprintf(fp, fmt, ap);
  58. if (count >= 0 && (work = G_calloc(count + 1, 1))) {
  59. rewind(fp);
  60. fread(work, 1, count, fp);
  61. db_append_string(st->errMsg, work);
  62. G_free(work);
  63. }
  64. fclose(fp);
  65. }
  66. va_end(ap);
  67. }
  68. /*!
  69. \brief Report error message for DB driver
  70. */
  71. void db_d_report_error(void)
  72. {
  73. db_append_string(st->errMsg, "\n");
  74. db_error(db_get_string(st->errMsg));
  75. init();
  76. }