GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2001-2012 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author GRASS Development Team
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <stdarg.h>
16#include <grass/gis.h>
17#include <grass/glocale.h>
18
19static int initialized;
20static int grass_debug_level;
21
22/**
23 * \brief Initiate debugging.
24 */
25void G_init_debug(void)
26{
27 const char *lstr;
28
29 if (G_is_initialized(&initialized))
30 return;
31
32 lstr = G_getenv_nofatal("DEBUG");
33
34 if (lstr != NULL)
35 grass_debug_level = atoi(lstr);
36 else
37 grass_debug_level = 0;
38
39 G_initialize_done(&initialized);
40}
41
42/**
43 * \brief Print debugging message.
44 *
45 * Print debugging message if environment variable GRASS_DEBUG_LEVEL
46 * is set to level equal or greater
47 *
48 * Levels: (recommended levels)<br>
49 * - 1 - message is printed once or twice per module<br>
50 * - 2 - less interesting once-per-module messages,<br>
51 * - 2 - library functions likely to be used once in a module<br>
52 * - 3 - library functions likely to be called a few times in a module
53 * (<=10),<br>
54 * - 3 - database opening and closing logistics<br>
55 * - 4 - each row (raster) or line (vector) or database/column (DB),<br>
56 * - 4 - each column/cat (DB)<br>
57 * - 5 - each cell (raster) or point (vector) or cat/attribute (DB)
58 *
59 * \param[in] level level
60 * \param[in] msg message
61 * \return 0 on error
62 * \return 1 on success
63 */
64int G_debug(int level, const char *msg, ...)
65{
66 char *filen;
67 va_list ap;
68 FILE *fd;
69
71
72 if (grass_debug_level >= level) {
73 filen = getenv("GRASS_DEBUG_FILE");
74 if (filen != NULL) {
75 fd = fopen(filen, "a");
76 if (!fd) {
77 G_warning(_("Cannot open debug file '%s'"), filen);
78 return 0;
79 }
80 }
81 else {
82 fd = stderr;
83 }
84
85 fprintf(fd, "D%d/%d: ", level, grass_debug_level);
86 va_start(ap, msg);
87 vfprintf(fd, msg, ap);
88 va_end(ap);
89 fprintf(fd, "\n");
90 fflush(fd);
91
92 if (filen != NULL)
93 fclose(fd);
94 }
95
96 return 1;
97}
#define NULL
Definition ccmath.h:32
void G_init_debug(void)
Initiate debugging.
Definition debug.c:25
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition debug.c:64
const char * G_getenv_nofatal(const char *)
Get environment variable.
Definition env.c:403
void G_warning(const char *,...) __attribute__((format(printf
int G_is_initialized(int *)
Definition counter.c:60
void G_initialize_done(int *)
Definition counter.c:77
#define _(str)
Definition glocale.h:10