GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72095
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
debug.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/debug.c
3  *
4  * \brief GIS Library - Debug functions.
5  *
6  * (C) 2001-2012 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author GRASS GIS Development Team
12  */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdarg.h>
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 
21 static int initialized;
22 static int grass_debug_level;
23 
24 /**
25  * \brief Initiate debugging.
26  */
27 void G_init_debug(void)
28 {
29  const char *lstr;
30 
31  if (G_is_initialized(&initialized))
32  return;
33 
34  lstr = G_getenv_nofatal("DEBUG");
35 
36  if (lstr != NULL)
37  grass_debug_level = atoi(lstr);
38  else
39  grass_debug_level = 0;
40 
41  G_initialize_done(&initialized);
42 }
43 
44 /**
45  * \brief Print debugging message.
46  *
47  * Print debugging message if environment variable GRASS_DEBUG_LEVEL
48  * is set to level equal or greater
49  *
50  * Levels: (recommended levels)<br>
51  * - 1 - message is printed once or twice per module<br>
52  * - 2 - less interesting once-per-module messages,<br>
53  * - 2 - library functions likely to be used once in a module<br>
54  * - 3 - library functions likely to be called a few times in a module (<=10),<br>
55  * - 3 - database opening and closing logistics<br>
56  * - 4 - each row (raster) or line (vector) or database/column (DB),<br>
57  * - 4 - each column/cat (DB)<br>
58  * - 5 - each cell (raster) or point (vector) or cat/attribute (DB)
59  *
60  * \param[in] level level
61  * \param[in] msg message
62  * \return 0 on error
63  * \return 1 on success
64  */
65 int G_debug(int level, const char *msg, ...)
66 {
67  char *filen;
68  va_list ap;
69  FILE *fd;
70 
71  G_init_debug();
72 
73  if (grass_debug_level >= level) {
74  va_start(ap, msg);
75 
76  filen = getenv("GRASS_DEBUG_FILE");
77  if (filen != NULL) {
78  fd = fopen(filen, "a");
79  if (!fd) {
80  G_warning(_("Cannot open debug file '%s'"), filen);
81  return 0;
82  }
83  }
84  else {
85  fd = stderr;
86  }
87 
88  fprintf(fd, "D%d/%d: ", level, grass_debug_level);
89  vfprintf(fd, msg, ap);
90  fprintf(fd, "\n");
91  fflush(fd);
92 
93  if (filen != NULL)
94  fclose(fd);
95 
96  va_end(ap);
97  }
98 
99  return 1;
100 }
101 
void G_init_debug(void)
Initiate debugging.
Definition: debug.c:27
int G_is_initialized(int *p)
Definition: counter.c:59
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *p)
Definition: counter.c:76
fd
Definition: d/range.c:69
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
const char * G_getenv_nofatal(const char *name)
Get environment variable.
Definition: env.c:381
fclose(fd)
#define _(str)
Definition: glocale.h:13
char * getenv()
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203