global_config_env.cc
Go to the documentation of this file.
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 
20 
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 
32 #include <grpc/support/log.h>
34 
35 #include "src/core/lib/gpr/env.h"
37 
38 namespace grpc_core {
39 
40 namespace {
41 
42 void DefaultGlobalConfigEnvErrorFunction(const char* error_message) {
43  gpr_log(GPR_ERROR, "%s", error_message);
44 }
45 
46 GlobalConfigEnvErrorFunctionType g_global_config_env_error_func =
47  DefaultGlobalConfigEnvErrorFunction;
48 
49 void LogParsingError(const char* name, const char* value) {
50  std::string error_message = absl::StrFormat(
51  "Illegal value '%s' specified for environment variable '%s'", value,
52  name);
53  (*g_global_config_env_error_func)(error_message.c_str());
54 }
55 
56 } // namespace
57 
59  g_global_config_env_error_func = func;
60 }
61 
64 }
65 
66 void GlobalConfigEnv::SetValue(const char* value) {
68 }
69 
71 
73  // This makes sure that name_ is in a canonical form having uppercase
74  // letters. This is okay to be called serveral times.
75  for (char* c = name_; *c != 0; ++c) {
76  *c = toupper(*c);
77  }
78  return name_;
79 }
81  "GlobalConfigEnvBool needs to be trivially destructible.");
82 
85  if (str == nullptr) {
86  return default_value_;
87  }
88  // parsing given value string.
89  bool result = false;
90  if (!gpr_parse_bool_value(str.get(), &result)) {
91  LogParsingError(GetName(), str.get());
93  }
94  return result;
95 }
96 
98  SetValue(value ? "true" : "false");
99 }
100 
102  "GlobalConfigEnvInt32 needs to be trivially destructible.");
103 
106  if (str == nullptr) {
107  return default_value_;
108  }
109  // parsing given value string.
110  char* end = str.get();
111  long result = strtol(str.get(), &end, 10);
112  if (*end != 0) {
113  LogParsingError(GetName(), str.get());
115  }
116  return static_cast<int32_t>(result);
117 }
118 
122  SetValue(buffer);
123 }
124 
126  "GlobalConfigEnvString needs to be trivially destructible.");
127 
130  if (str == nullptr) {
132  }
133  return str;
134 }
135 
137 
138 } // namespace grpc_core
xds_interop_client.str
str
Definition: xds_interop_client.py:487
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
global_config_env.h
grpc_core::GlobalConfigEnvString::Get
UniquePtr< char > Get()
Definition: global_config_env.cc:128
log.h
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_core
Definition: call_metric_recorder.h:31
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::SetGlobalConfigEnvErrorFunction
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func)
Definition: global_config_env.cc:58
setup.name
name
Definition: setup.py:542
env.h
grpc_core::GlobalConfigEnvInt32::default_value_
int32_t default_value_
Definition: global_config_env.h:84
grpc_core::GlobalConfigEnvInt32::Set
void Set(int32_t value)
Definition: global_config_env.cc:119
gpr_parse_bool_value
bool gpr_parse_bool_value(const char *value, bool *dst)
Definition: string.cc:325
grpc_core::GlobalConfigEnvBool::Get
bool Get()
Definition: global_config_env.cc:83
string_util.h
gpr_getenv
char * gpr_getenv(const char *name)
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
GPR_LTOA_MIN_BUFSIZE
#define GPR_LTOA_MIN_BUFSIZE
Definition: string.h:51
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::GlobalConfigEnv::GetName
char * GetName()
Definition: global_config_env.cc:72
grpc_core::UniquePtr
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: src/core/lib/gprpp/memory.h:43
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::GlobalConfigEnvErrorFunctionType
void(* GlobalConfigEnvErrorFunctionType)(const char *error_message)
Definition: global_config_env.h:31
grpc_core::GlobalConfigEnvString::Set
void Set(const char *value)
Definition: global_config_env.cc:136
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
grpc_core::GlobalConfigEnv::Unset
void Unset()
Definition: global_config_env.cc:70
grpc_core::GlobalConfigEnv::GetValue
UniquePtr< char > GetValue()
Definition: global_config_env.cc:62
grpc_core::GlobalConfigEnv::SetValue
void SetValue(const char *value)
Definition: global_config_env.cc:66
grpc_core::GlobalConfigEnvInt32::Get
int32_t Get()
Definition: global_config_env.cc:104
grpc_core::GlobalConfigEnvString::default_value_
const char * default_value_
Definition: global_config_env.h:96
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
gpr_unsetenv
void gpr_unsetenv(const char *name)
grpc_core::GlobalConfigEnvBool::Set
void Set(bool value)
Definition: global_config_env.cc:97
gpr_ltoa
int gpr_ltoa(long value, char *output)
Definition: string.cc:176
grpc_core::GlobalConfigEnvBool::default_value_
bool default_value_
Definition: global_config_env.h:72
gpr_setenv
void gpr_setenv(const char *name, const char *value)
grpc_core::GlobalConfigEnv::name_
char * name_
Definition: global_config_env.h:60
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:28