blob: bcb12deace27b3fe8f032aabf90bd9d60a5887a1 [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// Copyright (c) 2008, Google Inc.
2// All rights reserved.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -07003//
Austin Schuh906616c2019-01-21 20:25:11 -08004// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
James Kuszmaulba0ac1a2022-08-12 16:29:30 -07007//
Austin Schuh906616c2019-01-21 20:25:11 -08008// * 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.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070017//
Austin Schuh906616c2019-01-21 20:25:11 -080018// 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// ---
31// This file is a compatibility layer that defines Google's version of
32// command line flags that are used for configuration.
33//
34// We put flags into their own namespace. It is purposefully
35// named in an opaque way that people should have trouble typing
36// directly. The idea is that DEFINE puts the flag in the weird
37// namespace, and DECLARE imports the flag from there into the
38// current namespace. The net result is to force people to use
39// DECLARE to get access to a flag, rather than saying
40// extern bool FLAGS_logtostderr;
41// or some such instead. We want this so we can put extra
42// functionality (like sanity-checking) in DECLARE if we want,
43// and make sure it is picked up everywhere.
44//
45// We also put the type of the variable in the namespace, so that
46// people can't DECLARE_int32 something that they DEFINE_bool'd
47// elsewhere.
48#ifndef BASE_COMMANDLINEFLAGS_H__
49#define BASE_COMMANDLINEFLAGS_H__
50
51#include "config.h"
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070052#include <cstdlib> // for getenv
53#include <cstring> // for memchr
Austin Schuh906616c2019-01-21 20:25:11 -080054#include <string>
Austin Schuh906616c2019-01-21 20:25:11 -080055
56#ifdef HAVE_LIB_GFLAGS
57
58#include <gflags/gflags.h>
59
60#else
61
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070062#include <glog/logging.h>
Austin Schuh906616c2019-01-21 20:25:11 -080063
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070064#define DECLARE_VARIABLE(type, shorttype, name, tn) \
65 namespace fL##shorttype { \
66 extern GLOG_EXPORT type FLAGS_##name; \
67 } \
Austin Schuh906616c2019-01-21 20:25:11 -080068 using fL##shorttype::FLAGS_##name
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070069#define DEFINE_VARIABLE(type, shorttype, name, value, meaning, tn) \
70 namespace fL##shorttype { \
71 GLOG_EXPORT type FLAGS_##name(value); \
72 char FLAGS_no##name; \
73 } \
Austin Schuh906616c2019-01-21 20:25:11 -080074 using fL##shorttype::FLAGS_##name
75
76// bool specialization
77#define DECLARE_bool(name) \
78 DECLARE_VARIABLE(bool, B, name, bool)
79#define DEFINE_bool(name, value, meaning) \
80 DEFINE_VARIABLE(bool, B, name, value, meaning, bool)
81
82// int32 specialization
83#define DECLARE_int32(name) \
84 DECLARE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, int32)
85#define DEFINE_int32(name, value, meaning) \
86 DEFINE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, value, meaning, int32)
87
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070088// uint32 specialization
89#ifndef DECLARE_uint32
90#define DECLARE_uint32(name) \
91 DECLARE_VARIABLE(GOOGLE_NAMESPACE::uint32, U, name, uint32)
92#endif // DECLARE_uint64
93#define DEFINE_uint32(name, value, meaning) \
94 DEFINE_VARIABLE(GOOGLE_NAMESPACE::uint32, U, name, value, meaning, uint32)
95
Austin Schuh906616c2019-01-21 20:25:11 -080096// Special case for string, because we have to specify the namespace
97// std::string, which doesn't play nicely with our FLAG__namespace hackery.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070098#define DECLARE_string(name) \
99 namespace fLS { \
100 extern GLOG_EXPORT std::string& FLAGS_##name; \
101 } \
Austin Schuh906616c2019-01-21 20:25:11 -0800102 using fLS::FLAGS_##name
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700103#define DEFINE_string(name, value, meaning) \
104 namespace fLS { \
105 std::string FLAGS_##name##_buf(value); \
106 GLOG_EXPORT std::string& FLAGS_##name = FLAGS_##name##_buf; \
107 char FLAGS_no##name; \
108 } \
Austin Schuh906616c2019-01-21 20:25:11 -0800109 using fLS::FLAGS_##name
110
111#endif // HAVE_LIB_GFLAGS
112
113// Define GLOG_DEFINE_* using DEFINE_* . By using these macros, we
114// have GLOG_* environ variables even if we have gflags installed.
115//
116// If both an environment variable and a flag are specified, the value
117// specified by a flag wins. E.g., if GLOG_v=0 and --v=1, the
118// verbosity will be 1, not 0.
119
120#define GLOG_DEFINE_bool(name, value, meaning) \
121 DEFINE_bool(name, EnvToBool("GLOG_" #name, value), meaning)
122
123#define GLOG_DEFINE_int32(name, value, meaning) \
124 DEFINE_int32(name, EnvToInt("GLOG_" #name, value), meaning)
125
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700126#define GLOG_DEFINE_uint32(name, value, meaning) \
127 DEFINE_uint32(name, EnvToUInt("GLOG_" #name, value), meaning)
128
Austin Schuh906616c2019-01-21 20:25:11 -0800129#define GLOG_DEFINE_string(name, value, meaning) \
130 DEFINE_string(name, EnvToString("GLOG_" #name, value), meaning)
131
132// These macros (could be functions, but I don't want to bother with a .cc
133// file), make it easier to initialize flags from the environment.
134
135#define EnvToString(envname, dflt) \
136 (!getenv(envname) ? (dflt) : getenv(envname))
137
138#define EnvToBool(envname, dflt) \
139 (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL)
140
141#define EnvToInt(envname, dflt) \
142 (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10))
143
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700144#define EnvToUInt(envname, dflt) \
145 (!getenv(envname) ? (dflt) : strtoul(getenv(envname), NULL, 10))
146
Austin Schuh906616c2019-01-21 20:25:11 -0800147#endif // BASE_COMMANDLINEFLAGS_H__