aboutsummaryrefslogtreecommitdiff
path: root/src/core/lib/gprpp/global_config_env.h
blob: 4c2ec2dbd4722decd2c9907eea1df4ce166c177f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 *
 * Copyright 2019 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H

#include <grpc/support/port_platform.h>

#include "src/core/lib/gprpp/global_config_generic.h"
#include "src/core/lib/gprpp/memory.h"

namespace grpc_core {

typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message);

/*
 * Set global_config_env_error_function which is called when config system
 * encounters errors such as parsing error. What the default function does
 * is logging error message.
 */
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func);

// Base class for all classes to access environment variables.
class GlobalConfigEnv {
 protected:
  // `name` should be writable and alive after constructor is called.
  constexpr explicit GlobalConfigEnv(char* name) : name_(name) {}

 public:
  // Returns the value of `name` variable.
  UniquePtr<char> GetValue();

  // Sets the value of `name` variable.
  void SetValue(const char* value);

  // Unsets `name` variable.
  void Unset();

 protected:
  char* GetName();

 private:
  char* name_;
};

class GlobalConfigEnvBool : public GlobalConfigEnv {
 public:
  constexpr GlobalConfigEnvBool(char* name, bool default_value)
      : GlobalConfigEnv(name), default_value_(default_value) {}

  bool Get();
  void Set(bool value);

 private:
  bool default_value_;
};

class GlobalConfigEnvInt32 : public GlobalConfigEnv {
 public:
  constexpr GlobalConfigEnvInt32(char* name, int32_t default_value)
      : GlobalConfigEnv(name), default_value_(default_value) {}

  int32_t Get();
  void Set(int32_t value);

 private:
  int32_t default_value_;
};

class GlobalConfigEnvString : public GlobalConfigEnv {
 public:
  constexpr GlobalConfigEnvString(char* name, const char* default_value)
      : GlobalConfigEnv(name), default_value_(default_value) {}

  UniquePtr<char> Get();
  void Set(const char* value);

 private:
  const char* default_value_;
};

}  // namespace grpc_core

// Macros for defining global config instances using environment variables.
// This defines a GlobalConfig*Type* instance with arguments for
// mutable variable name and default value.
// Mutable name (g_env_str_##name) is here for having an array
// for the canonical name without dynamic allocation.
// `help` argument is ignored for this implementation.

#define GPR_GLOBAL_CONFIG_DEFINE_BOOL(name, default_value, help)       \
  static char g_env_str_##name[] = #name;                              \
  static grpc_core::GlobalConfigEnvBool g_env_##name(g_env_str_##name, \
                                                     default_value);   \
  bool gpr_global_config_get_##name() { return g_env_##name.Get(); }   \
  void gpr_global_config_set_##name(bool value) { g_env_##name.Set(value); }

#define GPR_GLOBAL_CONFIG_DEFINE_INT32(name, default_value, help)       \
  static char g_env_str_##name[] = #name;                               \
  static grpc_core::GlobalConfigEnvInt32 g_env_##name(g_env_str_##name, \
                                                      default_value);   \
  int32_t gpr_global_config_get_##name() { return g_env_##name.Get(); } \
  void gpr_global_config_set_##name(int32_t value) { g_env_##name.Set(value); }

#define GPR_GLOBAL_CONFIG_DEFINE_STRING(name, default_value, help)       \
  static char g_env_str_##name[] = #name;                                \
  static grpc_core::GlobalConfigEnvString g_env_##name(g_env_str_##name, \
                                                       default_value);   \
  grpc_core::UniquePtr<char> gpr_global_config_get_##name() {            \
    return g_env_##name.Get();                                           \
  }                                                                      \
  void gpr_global_config_set_##name(const char* value) {                 \
    g_env_##name.Set(value);                                             \
  }

#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */