]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2019 The Jaeger Authors. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #ifndef JAEGERTRACING_UTILS_ENV_VARIABLE_H | |
18 | #define JAEGERTRACING_UTILS_ENV_VARIABLE_H | |
19 | ||
20 | #include <string> | |
21 | #include <sstream> | |
22 | #include <algorithm> | |
23 | ||
24 | namespace jaegertracing { | |
25 | namespace utils { | |
26 | namespace EnvVariable { | |
27 | ||
28 | inline std::string getStringVariable(const char* envVar) | |
29 | { | |
30 | const auto rawVariable = std::getenv(envVar); | |
31 | return std::string(rawVariable ? rawVariable : ""); | |
32 | } | |
33 | ||
34 | inline std::pair<bool, int> getIntVariable(const char* envVar) | |
35 | { | |
36 | const auto rawVariable = std::getenv(envVar); | |
37 | const std::string variable(rawVariable ? rawVariable : ""); | |
38 | if (!variable.empty()) { | |
39 | std::istringstream iss(variable); | |
40 | int intVal = 0; | |
41 | if (iss >> intVal) { | |
42 | return std::make_pair(false, intVal); | |
43 | } | |
44 | } | |
45 | return std::make_pair(false, 0); | |
46 | } | |
47 | ||
48 | inline std::pair<bool, bool> getBoolVariable(const char* envVar) | |
49 | { | |
50 | const auto rawVariable = std::getenv(envVar); | |
51 | std::string variable(rawVariable ? rawVariable : ""); | |
52 | ||
53 | if (!variable.empty()) { | |
54 | std::transform( | |
55 | variable.begin(), variable.end(), variable.begin(), ::tolower); | |
56 | return std::make_pair(true, (variable == "true")); | |
57 | } | |
58 | return std::make_pair(false, false); | |
59 | } | |
60 | ||
61 | } // namespace EnvVariable | |
62 | } // namespace utils | |
63 | } // namespace jaegertracing | |
64 | ||
65 | #endif // JAEGERTRACING_UTILS_ENV_VARIABLE_H |