GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
verbose.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/verbose.c
3  *
4  * \brief GIS Library - Subroutines to manage verbosity level
5  *
6  * Note that verbosity can be controlled by GRASS_VERBOSE environment
7  * variable.
8  *
9  * See relevant subroutines:
10  * - G_percent()
11  * - G_important_message()
12  * - G_message()
13  * - G_verbose_message()
14  * - G_warning()
15  * - G_fatal_error()
16  *
17  * (C) 2001-2008, 2012-2013 by the GRASS Development Team
18  *
19  * This program is free software under the GNU General Public License
20  * (>=v2). Read the file COPYING that comes with GRASS for details.
21  *
22  * \author Jachym Cepicky - jachym.cepicky at gmail.com
23  */
24 
25 #include <stdlib.h>
26 #include <grass/config.h>
27 #include <grass/gis.h>
28 
29 /*! \brief Maximum verbosity level */
30 #define MAXLEVEL 3
31 /*! \brief Standard verbosity level */
32 #define STDLEVEL 2
33 /*! \brief Minimum verbosity level (quiet) */
34 #define MINLEVEL 0
35 
36 static struct state {
37  int initialized;
38  int verbose; /* current verbosity level */
39 } state;
40 
41 static struct state *st = &state;
42 
43 /*!
44  * \brief Get current verbosity level.
45  *
46  * Currently, there are 5 levels of verbosity (see return codes)
47  *
48  * \return -1 - nothing will be printed (also errors and warnings will be also discarded)
49  * \return 0 - nothing will be printed except of errors and warnings
50  * (G_fatal_error(), G_warning()). Triggered by <tt>--q</tt> or <tt>--quiet</tt> flag..
51  * \return 1 - only progress information (G_percent()) and important messages (G_important_message()) will be printed
52  * \return 2 - all messages (G_message() and G_important_message()) will be printed
53  * \return 3 - also verbose messages (G_verbose_message()) will be printed. Triggered by <tt>--v</tt> or <tt>--verbose</tt> flag.
54  */
55 int G_verbose(void)
56 {
57  const char *verstr; /* string for GRASS_VERBOSE content */
58 
59  if (G_is_initialized(&(st->initialized)))
60  return st->verbose;
61 
62  /* verbose not defined -> get it from env. */
63  verstr = getenv("GRASS_VERBOSE");
64  st->verbose = verstr ? atoi(verstr) : STDLEVEL;
65 
66  G_initialize_done(&(st->initialized));
67 
68  return st->verbose;
69 }
70 
71 /*!
72  * \brief Get max verbosity level.
73  *
74  * \return max verbosity level
75  */
76 int G_verbose_max(void)
77 {
78  return MAXLEVEL;
79 }
80 
81 /*!
82  * \brief Get standard verbosity level.
83  *
84  * \return standard verbosity level
85  */
86 int G_verbose_std(void)
87 {
88  return STDLEVEL;
89 }
90 
91 /*!
92  * \brief Get min verbosity level.
93  *
94  * \return min verbosity level
95  */
96 int G_verbose_min(void)
97 {
98  return MINLEVEL;
99 }
100 
101 /*!
102  * \brief Set verbosity level.
103  *
104  * - -1 - nothing will be printed (also errors and warnings will be also discarded)
105  * - 0 - nothing will be printed except of errors and warnings
106  * (G_fatal_error(), G_warning()). Triggered by <tt>--q</tt> or <tt>--quiet</tt> flag.
107  * - 1 - only progress information (G_percent()) and important messages (G_important_message()) will be printed
108  * - 2 - all messages (G_message() and G_important_message()) will be printed
109  * - 3 - also verbose messages (G_verbose_message()) will be printed. Triggered by <tt>--v</tt> or <tt>--verbose</tt> flag.
110  *
111  * \param level new verbosity level (-1,0,1,2,3)
112  *
113  * \return 0 on invalid verbosity level (verbosity level untouched)
114  * \return 1 on success
115  */
116 int G_set_verbose(int level)
117 {
118  if (level == -1 || (level >= MINLEVEL && level <= MAXLEVEL)) {
119  st->verbose = level;
120  if (!G_is_initialized(&(st->initialized)))
121  G_initialize_done(&(st->initialized));
122 
123  return 1;
124  }
125 
126  return 0;
127 }
#define MINLEVEL
Minimum verbosity level (quiet)
Definition: verbose.c:34
#define MAXLEVEL
Maximum verbosity level.
Definition: verbose.c:30
int G_set_verbose(int level)
Set verbosity level.
Definition: verbose.c:116
void G_initialize_done(int *)
Definition: counter.c:76
struct state * st
Definition: parser.c:104
int G_verbose_max(void)
Get max verbosity level.
Definition: verbose.c:76
int G_verbose_std(void)
Get standard verbosity level.
Definition: verbose.c:86
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:55
int G_is_initialized(int *)
Definition: counter.c:59
#define STDLEVEL
Standard verbosity level.
Definition: verbose.c:32
int G_verbose_min(void)
Get min verbosity level.
Definition: verbose.c:96
char * getenv()
struct state state
Definition: parser.c:103