GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
d_error.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_driver/d_error.c
3 
4  \brief DBMI Library (driver) - error reporting
5 
6  Taken from DB drivers.
7 
8  (C) 1999-2008, 2012 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public
11  License (>=v2). Read the file COPYING that comes with GRASS
12  for details.
13 
14  \author Joel Jones (CERL/UIUC)
15  \author Radim Blazek
16  \author Adopted for DBMI by Martin Landa <landa.martin@gmail.com>
17 */
18 
19 #include <string.h>
20 #include <grass/dbmi.h>
21 #include <grass/glocale.h>
22 
23 /* initialize the global struct */
24 struct error_state {
25  char *driver_name;
26  dbString *errMsg;
27 };
28 
29 static struct error_state state;
30 static struct error_state *st = &state;
31 
32 static void init()
33 {
34  db_set_string(st->errMsg, "");
35  db_d_append_error(_("DBMI-%s driver error:"), st->driver_name);
36  db_append_string(st->errMsg, "\n");
37 }
38 
39 
40 /*!
41  \brief Init error message for DB driver
42 
43  Initialize prefix
44 
45  \param name driver name (eg. "SQLite"))
46 */
47 void db_d_init_error(const char *name)
48 {
49  if (!st->errMsg) {
50  st->errMsg = (dbString *) G_malloc(sizeof(dbString));
51  db_init_string(st->errMsg);
52  }
53 
54  G_debug(1, "db_d_init_error(): %s", name);
55 
56  st->driver_name = G_malloc(strlen(name) + 1);
57  strcpy(st->driver_name, name);
58  init();
59 }
60 
61 /*!
62  \brief Append error message for DB driver
63 
64  \param fmt formatted message
65 */
66 void db_d_append_error(const char *fmt, ...)
67 {
68  FILE *fp = NULL;
69  char *work = NULL;
70  int count = 0;
71  va_list ap;
72 
73  va_start(ap, fmt);
74  if ((fp = tmpfile())) {
75  count = vfprintf(fp, fmt, ap);
76  if (count >= 0 && (work = G_calloc(count + 1, 1))) {
77  rewind(fp);
78  fread(work, 1, count, fp);
79  db_append_string(st->errMsg, work);
80  G_free(work);
81  }
82  fclose(fp);
83  }
84  va_end(ap);
85 }
86 
87 /*!
88  \brief Report error message for DB driver
89 */
91 {
92  db_append_string(st->errMsg, "\n");
93  db_error(db_get_string(st->errMsg));
94 
95  init();
96 }
#define G_malloc(n)
Definition: defs/gis.h:112
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
char * db_get_string(const dbString *)
Get string.
Definition: string.c:140
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int count
#define NULL
Definition: ccmath.h:32
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
void db_d_append_error(const char *fmt,...)
Append error message for DB driver.
Definition: d_error.c:66
#define G_calloc(m, n)
Definition: defs/gis.h:113
struct state * st
Definition: parser.c:104
int db_append_string(dbString *, const char *)
Append string to dbString.
Definition: string.c:205
void db_error(const char *)
Report error message.
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:7
struct state state
Definition: parser.c:103
void db_d_report_error(void)
Report error message for DB driver.
Definition: d_error.c:90
int G_debug(int, const char *,...) __attribute__((format(printf
void db_d_init_error(const char *name)
Init error message for DB driver.
Definition: d_error.c:47
void init(double work[])
Definition: as177.c:65