1// Copyright (c) 1999, 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: Ray Sidney and many others
31//
32// Defines the VLOG_IS_ON macro that controls the variable-verbosity
33// conditional logging.
34//
35// It's used by VLOG and VLOG_IF in logging.h
36// and by RAW_VLOG in raw_logging.h to trigger the logging.
37//
38// It can also be used directly e.g. like this:
39// if (VLOG_IS_ON(2)) {
40// // do some logging preparation and logging
41// // that can't be accomplished e.g. via just VLOG(2) << ...;
42// }
43//
44// The truth value that VLOG_IS_ON(level) returns is determined by
45// the three verbosity level flags:
46// --v=<n> Gives the default maximal active V-logging level;
47// 0 is the default.
48// Normally positive values are used for V-logging levels.
49// --vmodule=<str> Gives the per-module maximal V-logging levels to override
50// the value given by --v.
51// E.g. "my_module=2,foo*=3" would change the logging level
52// for all code in source files "my_module.*" and "foo*.*"
53// ("-inl" suffixes are also disregarded for this matching).
54//
55// SetVLOGLevel helper function is provided to do limited dynamic control over
56// V-logging by overriding the per-module settings given via --vmodule flag.
57//
58// CAVEAT: --vmodule functionality is not available in non gcc compilers.
59//
60
61#ifndef BASE_VLOG_IS_ON_H_
62#define BASE_VLOG_IS_ON_H_
63
64#include "glog/log_severity.h"
65
66// Annoying stuff for windows -- makes sure clients can import these functions
67#ifndef GOOGLE_GLOG_DLL_DECL
68# if defined(_WIN32) && !defined(__CYGWIN__)
69# define GOOGLE_GLOG_DLL_DECL __declspec(dllimport)
70# else
71# define GOOGLE_GLOG_DLL_DECL
72# endif
73#endif
74
75#if defined(__GNUC__)
76// We emit an anonymous static int* variable at every VLOG_IS_ON(n) site.
77// (Normally) the first time every VLOG_IS_ON(n) site is hit,
78// we determine what variable will dynamically control logging at this site:
79// it's either FLAGS_v or an appropriate internal variable
80// matching the current source file that represents results of
81// parsing of --vmodule flag and/or SetVLOGLevel calls.
82#define VLOG_IS_ON(verboselevel) \
83 __extension__ \
84 ({ static google::int32* vlocal__ = &google::kLogSiteUninitialized; \
85 google::int32 verbose_level__ = (verboselevel); \
86 (*vlocal__ >= verbose_level__) && \
87 ((vlocal__ != &google::kLogSiteUninitialized) || \
88 (google::InitVLOG3__(&vlocal__, &FLAGS_v, \
89 __FILE__, verbose_level__))); })
90#else
91// GNU extensions not available, so we do not support --vmodule.
92// Dynamic value of FLAGS_v always controls the logging level.
93#define VLOG_IS_ON(verboselevel) (FLAGS_v >= (verboselevel))
94#endif
95
96// Set VLOG(_IS_ON) level for module_pattern to log_level.
97// This lets us dynamically control what is normally set by the --vmodule flag.
98// Returns the level that previously applied to module_pattern.
99// NOTE: To change the log level for VLOG(_IS_ON) sites
100// that have already executed after/during InitGoogleLogging,
101// one needs to supply the exact --vmodule pattern that applied to them.
102// (If no --vmodule pattern applied to them
103// the value of FLAGS_v will continue to control them.)
104extern GOOGLE_GLOG_DLL_DECL int SetVLOGLevel(const char* module_pattern,
105 int log_level);
106
107// Various declarations needed for VLOG_IS_ON above: =========================
108
109// Special value used to indicate that a VLOG_IS_ON site has not been
110// initialized. We make this a large value, so the common-case check
111// of "*vlocal__ >= verbose_level__" in VLOG_IS_ON definition
112// passes in such cases and InitVLOG3__ is then triggered.
113extern google::int32 kLogSiteUninitialized;
114
115// Helper routine which determines the logging info for a particalur VLOG site.
116// site_flag is the address of the site-local pointer to the controlling
117// verbosity level
118// site_default is the default to use for *site_flag
119// fname is the current source file name
120// verbose_level is the argument to VLOG_IS_ON
121// We will return the return value for VLOG_IS_ON
122// and if possible set *site_flag appropriately.
123extern GOOGLE_GLOG_DLL_DECL bool InitVLOG3__(
124 google::int32** site_flag,
125 google::int32* site_default,
126 const char* fname,
127 google::int32 verbose_level);
128
129#endif // BASE_VLOG_IS_ON_H_
130