Loading...
Searching...
No Matches
debug.c
Go to the documentation of this file.
/*!
* \file lib/gis/debug.c
*
* \brief GIS Library - Debug functions.
*
* (C) 2001-2012 by the GRASS Development Team
*
* This program is free software under the GNU General Public License
* (>=v2). Read the file COPYING that comes with GRASS for details.
*
* \author GRASS Development Team
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <grass/gis.h>
#include <grass/glocale.h>
static int initialized;
static int grass_debug_level;
/**
* \brief Initiate debugging.
*/
void G_init_debug(void)
{
const char *lstr;
if (G_is_initialized(&initialized))
return;
lstr = G_getenv_nofatal("DEBUG");
if (lstr != NULL)
grass_debug_level = atoi(lstr);
else
grass_debug_level = 0;
G_initialize_done(&initialized);
}
/**
* \brief Print debugging message.
*
* Print debugging message if environment variable GRASS_DEBUG_LEVEL
* is set to level equal or greater
*
* Levels: (recommended levels)<br>
* - 1 - message is printed once or twice per module<br>
* - 2 - less interesting once-per-module messages,<br>
* - 2 - library functions likely to be used once in a module<br>
* - 3 - library functions likely to be called a few times in a module
* (<=10),<br>
* - 3 - database opening and closing logistics<br>
* - 4 - each row (raster) or line (vector) or database/column (DB),<br>
* - 4 - each column/cat (DB)<br>
* - 5 - each cell (raster) or point (vector) or cat/attribute (DB)
*
* \param[in] level level
* \param[in] msg message
* \return 0 on error
* \return 1 on success
*/
int G_debug(int level, const char *msg, ...)
{
char *filen;
va_list ap;
FILE *fd;
G_init_debug();
if (grass_debug_level >= level) {
filen = getenv("GRASS_DEBUG_FILE");
if (filen != NULL) {
fd = fopen(filen, "a");
if (!fd) {
G_warning(_("Cannot open debug file '%s'"), filen);
return 0;
}
}
else {
fd = stderr;
}
fprintf(fd, "D%d/%d: ", level, grass_debug_level);
va_start(ap, msg);
vfprintf(fd, msg, ap);
va_end(ap);
fprintf(fd, "\n");
fflush(fd);
if (filen != NULL)
fclose(fd);
}
return 1;
}
NULL
#define NULL
Definition ccmath.h:32
G_initialize_done
void G_initialize_done(int *p)
Definition counter.c:77
G_is_initialized
int G_is_initialized(int *p)
Definition counter.c:60
G_init_debug
void G_init_debug(void)
Initiate debugging.
Definition debug.c:27
G_debug
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition debug.c:66
G_getenv_nofatal
const char * G_getenv_nofatal(const char *name)
Get environment variable.
Definition env.c:405
G_warning
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition gis/error.c:203