3 * Copyright 2019 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
20 #define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
22 #include <grpc/support/port_platform.h>
24 #include "src/core/lib/gprpp/global_config_generic.h"
25 #include "src/core/lib/gprpp/memory.h"
29 typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message);
32 * Set global_config_env_error_function which is called when config system
33 * encounters errors such as parsing error. What the default function does
34 * is logging error message.
36 void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func);
38 // Base class for all classes to access environment variables.
39 class GlobalConfigEnv {
41 // `name` should be writable and alive after constructor is called.
42 constexpr explicit GlobalConfigEnv(char* name) : name_(name) {}
45 // Returns the value of `name` variable.
46 UniquePtr<char> GetValue();
48 // Sets the value of `name` variable.
49 void SetValue(const char* value);
51 // Unsets `name` variable.
61 class GlobalConfigEnvBool : public GlobalConfigEnv {
63 constexpr GlobalConfigEnvBool(char* name, bool default_value)
64 : GlobalConfigEnv(name), default_value_(default_value) {}
73 class GlobalConfigEnvInt32 : public GlobalConfigEnv {
75 constexpr GlobalConfigEnvInt32(char* name, int32_t default_value)
76 : GlobalConfigEnv(name), default_value_(default_value) {}
79 void Set(int32_t value);
82 int32_t default_value_;
85 class GlobalConfigEnvString : public GlobalConfigEnv {
87 constexpr GlobalConfigEnvString(char* name, const char* default_value)
88 : GlobalConfigEnv(name), default_value_(default_value) {}
90 UniquePtr<char> Get();
91 void Set(const char* value);
94 const char* default_value_;
97 } // namespace grpc_core
99 // Macros for defining global config instances using environment variables.
100 // This defines a GlobalConfig*Type* instance with arguments for
101 // mutable variable name and default value.
102 // Mutable name (g_env_str_##name) is here for having an array
103 // for the canonical name without dynamic allocation.
104 // `help` argument is ignored for this implementation.
106 #define GPR_GLOBAL_CONFIG_DEFINE_BOOL(name, default_value, help) \
107 static char g_env_str_##name[] = #name; \
108 static ::grpc_core::GlobalConfigEnvBool g_env_##name(g_env_str_##name, \
110 bool gpr_global_config_get_##name() { return g_env_##name.Get(); } \
111 void gpr_global_config_set_##name(bool value) { g_env_##name.Set(value); }
113 #define GPR_GLOBAL_CONFIG_DEFINE_INT32(name, default_value, help) \
114 static char g_env_str_##name[] = #name; \
115 static ::grpc_core::GlobalConfigEnvInt32 g_env_##name(g_env_str_##name, \
117 int32_t gpr_global_config_get_##name() { return g_env_##name.Get(); } \
118 void gpr_global_config_set_##name(int32_t value) { g_env_##name.Set(value); }
120 #define GPR_GLOBAL_CONFIG_DEFINE_STRING(name, default_value, help) \
121 static char g_env_str_##name[] = #name; \
122 static ::grpc_core::GlobalConfigEnvString g_env_##name(g_env_str_##name, \
124 ::grpc_core::UniquePtr<char> gpr_global_config_get_##name() { \
125 return g_env_##name.Get(); \
127 void gpr_global_config_set_##name(const char* value) { \
128 g_env_##name.Set(value); \
131 #endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */