GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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
49  * also discarded)
50  * \return 0 - nothing will be printed except of errors and warnings
51  * (G_fatal_error(), G_warning()). Triggered by <tt>--q</tt>
52  * or <tt>--quiet</tt> flag..
53  * \return 1 - only progress information (G_percent()) and important messages
54  * (G_important_message()) will be printed
55  * \return 2 - all messages (G_message() and G_important_message()) will be
56  * printed
57  * \return 3 - also verbose messages (G_verbose_message()) will be printed.
58  * Triggered by <tt>--v</tt> or <tt>--verbose</tt> flag.
59  */
60 int G_verbose(void)
61 {
62  const char *verstr; /* string for GRASS_VERBOSE content */
63 
64  if (G_is_initialized(&(st->initialized)))
65  return st->verbose;
66 
67  /* verbose not defined -> get it from env. */
68  verstr = getenv("GRASS_VERBOSE");
69  st->verbose = verstr ? atoi(verstr) : STDLEVEL;
70 
71  G_initialize_done(&(st->initialized));
72 
73  return st->verbose;
74 }
75 
76 /*!
77  * \brief Get max verbosity level.
78  *
79  * \return max verbosity level
80  */
81 int G_verbose_max(void)
82 {
83  return MAXLEVEL;
84 }
85 
86 /*!
87  * \brief Get standard verbosity level.
88  *
89  * \return standard verbosity level
90  */
91 int G_verbose_std(void)
92 {
93  return STDLEVEL;
94 }
95 
96 /*!
97  * \brief Get min verbosity level.
98  *
99  * \return min verbosity level
100  */
101 int G_verbose_min(void)
102 {
103  return MINLEVEL;
104 }
105 
106 /*!
107  * \brief Set verbosity level.
108  *
109  * - -1 - nothing will be printed (also errors and warnings will be also
110  * discarded)
111  * - 0 - nothing will be printed except of errors and warnings
112  * (G_fatal_error(), G_warning()). Triggered by <tt>--q</tt> or
113  * <tt>--quiet</tt> flag.
114  * - 1 - only progress information (G_percent()) and important messages
115  * (G_important_message()) will be printed
116  * - 2 - all messages (G_message() and G_important_message()) will be printed
117  * - 3 - also verbose messages (G_verbose_message()) will be printed. Triggered
118  * by <tt>--v</tt> or <tt>--verbose</tt> flag.
119  *
120  * \param level new verbosity level (-1,0,1,2,3)
121  *
122  * \return 0 on invalid verbosity level (verbosity level untouched)
123  * \return 1 on success
124  */
125 int G_set_verbose(int level)
126 {
127  if (level == -1 || (level >= MINLEVEL && level <= MAXLEVEL)) {
128  st->verbose = level;
129  if (!G_is_initialized(&(st->initialized)))
130  G_initialize_done(&(st->initialized));
131 
132  return 1;
133  }
134 
135  return 0;
136 }
int G_is_initialized(int *)
Definition: counter.c:60
void G_initialize_done(int *)
Definition: counter.c:77
struct state state
Definition: parser.c:103
struct state * st
Definition: parser.c:104
int G_verbose_max(void)
Get max verbosity level.
Definition: verbose.c:81
#define STDLEVEL
Standard verbosity level.
Definition: verbose.c:32
int G_set_verbose(int level)
Set verbosity level.
Definition: verbose.c:125
#define MINLEVEL
Minimum verbosity level (quiet)
Definition: verbose.c:34
#define MAXLEVEL
Maximum verbosity level.
Definition: verbose.c:30
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:60
int G_verbose_min(void)
Get min verbosity level.
Definition: verbose.c:101
int G_verbose_std(void)
Get standard verbosity level.
Definition: verbose.c:91