• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2019 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
20 #define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "src/core/lib/gprpp/global_config_generic.h"
25 #include "src/core/lib/gprpp/memory.h"
26 
27 namespace grpc_core {
28 
29 typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message);
30 
31 /*
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.
35  */
36 void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func);
37 
38 // Base class for all classes to access environment variables.
39 class GlobalConfigEnv {
40  protected:
41   // `name` should be writable and alive after constructor is called.
GlobalConfigEnv(char * name)42   constexpr explicit GlobalConfigEnv(char* name) : name_(name) {}
43 
44  public:
45   // Returns the value of `name` variable.
46   grpc_core::UniquePtr<char> GetValue();
47 
48   // Sets the value of `name` variable.
49   void SetValue(const char* value);
50 
51   // Unsets `name` variable.
52   void Unset();
53 
54  protected:
55   char* GetName();
56 
57  private:
58   char* name_;
59 };
60 
61 class GlobalConfigEnvBool : public GlobalConfigEnv {
62  public:
GlobalConfigEnvBool(char * name,bool default_value)63   constexpr GlobalConfigEnvBool(char* name, bool default_value)
64       : GlobalConfigEnv(name), default_value_(default_value) {}
65 
66   bool Get();
67   void Set(bool value);
68 
69  private:
70   bool default_value_;
71 };
72 
73 class GlobalConfigEnvInt32 : public GlobalConfigEnv {
74  public:
GlobalConfigEnvInt32(char * name,int32_t default_value)75   constexpr GlobalConfigEnvInt32(char* name, int32_t default_value)
76       : GlobalConfigEnv(name), default_value_(default_value) {}
77 
78   int32_t Get();
79   void Set(int32_t value);
80 
81  private:
82   int32_t default_value_;
83 };
84 
85 class GlobalConfigEnvString : public GlobalConfigEnv {
86  public:
GlobalConfigEnvString(char * name,const char * default_value)87   constexpr GlobalConfigEnvString(char* name, const char* default_value)
88       : GlobalConfigEnv(name), default_value_(default_value) {}
89 
90   grpc_core::UniquePtr<char> Get();
91   void Set(const char* value);
92 
93  private:
94   const char* default_value_;
95 };
96 
97 }  // namespace grpc_core
98 
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.
105 
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, \
109                                                        default_value);   \
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); }
112 
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, \
116                                                         default_value);   \
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); }
119 
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, \
123                                                          default_value);   \
124   ::grpc_core::UniquePtr<char> gpr_global_config_get_##name() {            \
125     return g_env_##name.Get();                                             \
126   }                                                                        \
127   void gpr_global_config_set_##name(const char* value) {                   \
128     g_env_##name.Set(value);                                               \
129   }
130 
131 #endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */
132