Debian Codegraph @

src/core/lib/gprpp/global_config_env.cc

raw
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/gprpp/global_config_env.h"
22
23 #include <ctype.h>
24 #include <stdlib.h>
25
26 #include <memory>
27 #include <string>
28 #include <type_traits>
29
30 #include "absl/strings/str_format.h"
31 #include "absl/types/optional.h"
32
33 #include <grpc/support/log.h>
34 #include <grpc/support/string_util.h>
35
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/gprpp/env.h"
38
39 namespace grpc_core {
40
41 namespace {
42
43 void DefaultGlobalConfigEnvErrorFunction(const char* error_message) {
44 gpr_log(GPR_ERROR, "%s", error_message);
45 }
46
47 GlobalConfigEnvErrorFunctionType g_global_config_env_error_func =
48 DefaultGlobalConfigEnvErrorFunction;
49
50 void LogParsingError(const char* name, const char* value) {
51 std::string error_message = absl::StrFormat(
52 "Illegal value '%s' specified for environment variable '%s'", value,
53 name);
54 (*g_global_config_env_error_func)(error_message.c_str());
55 }
56
57 } // namespace
58
59 void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func) {
60 g_global_config_env_error_func = func;
61 }
62
63 UniquePtr<char> GlobalConfigEnv::GetValue() {
64 auto env = GetEnv(GetName());
65 return UniquePtr<char>(env.has_value() ? gpr_strdup(env.value().c_str())
66 : nullptr);
67 }
68
69 void GlobalConfigEnv::SetValue(const char* value) { SetEnv(GetName(), value); }
70
71 void GlobalConfigEnv::Unset() { UnsetEnv(GetName()); }
72
73 char* GlobalConfigEnv::GetName() {
74 // This makes sure that name_ is in a canonical form having uppercase
75 // letters. This is okay to be called serveral times.
76 for (char* c = name_; *c != 0; ++c) {
77 *c = toupper(*c);
78 }
79 return name_;
80 }
81 static_assert(std::is_trivially_destructible<GlobalConfigEnvBool>::value,
82 "GlobalConfigEnvBool needs to be trivially destructible.");
83
84 bool GlobalConfigEnvBool::Get() {
85 UniquePtr<char> str = GetValue();
86 if (str == nullptr) {
87 return default_value_;
88 }
89 // parsing given value string.
90 bool result = false;
91 if (!gpr_parse_bool_value(str.get(), &result)) {
92 LogParsingError(GetName(), str.get());
93 result = default_value_;
94 }
95 return result;
96 }
97
98 void GlobalConfigEnvBool::Set(bool value) {
99 SetValue(value ? "true" : "false");
100 }
101
102 static_assert(std::is_trivially_destructible<GlobalConfigEnvInt32>::value,
103 "GlobalConfigEnvInt32 needs to be trivially destructible.");
104
105 int32_t GlobalConfigEnvInt32::Get() {
106 UniquePtr<char> str = GetValue();
107 if (str == nullptr) {
108 return default_value_;
109 }
110 // parsing given value string.
111 char* end = str.get();
112 long result = strtol(str.get(), &end, 10);
113 if (*end != 0) {
114 LogParsingError(GetName(), str.get());
115 result = default_value_;
116 }
117 return static_cast<int32_t>(result);
118 }
119
120 void GlobalConfigEnvInt32::Set(int32_t value) {
121 char buffer[GPR_LTOA_MIN_BUFSIZE];
122 gpr_ltoa(value, buffer);
123 SetValue(buffer);
124 }
125
126 static_assert(std::is_trivially_destructible<GlobalConfigEnvString>::value,
127 "GlobalConfigEnvString needs to be trivially destructible.");
128
129 UniquePtr<char> GlobalConfigEnvString::Get() {
130 UniquePtr<char> str = GetValue();
131 if (str == nullptr) {
132 return UniquePtr<char>(gpr_strdup(default_value_));
133 }
134 return str;
135 }
136
137 void GlobalConfigEnvString::Set(const char* value) { SetValue(value); }
138
139 } // namespace grpc_core