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
// Copyright (c) 2015 Baidu, Inc.
//
// 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.
// Author: Ge,Jun (gejun@baidu.com)
// Date: Sun Aug 9 12:26:03 CST 2015
#include <stdlib.h>
#include <gflags/gflags.h>
#include "bvar/gflag.h"
namespace bvar {
GFlag::GFlag(const butil::StringPiece& gflag_name) {
expose(gflag_name);
}
GFlag::GFlag(const butil::StringPiece& prefix,
const butil::StringPiece& gflag_name)
: _gflag_name(gflag_name.data(), gflag_name.size()) {
expose_as(prefix, gflag_name);
}
void GFlag::describe(std::ostream& os, bool quote_string) const {
GFLAGS_NS::CommandLineFlagInfo info;
if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
if (quote_string) {
os << '"';
}
os << "Unknown gflag=" << gflag_name();
if (quote_string) {
os << '"';
}
} else {
if (quote_string && info.type == "string") {
os << '"' << info.current_value << '"';
} else {
os << info.current_value;
}
}
}
#ifdef BAIDU_INTERNAL
void GFlag::get_value(boost::any* value) const {
GFLAGS_NS::CommandLineFlagInfo info;
if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
*value = "Unknown gflag=" + gflag_name();
} else if (info.type == "string") {
*value = info.current_value;
} else if (info.type == "int32") {
*value = static_cast<int>(atoi(info.current_value.c_str()));
} else if (info.type == "int64") {
*value = static_cast<int64_t>(atoll(info.current_value.c_str()));
} else if (info.type == "uint64") {
*value = static_cast<uint64_t>(
strtoull(info.current_value.c_str(), NULL, 10));
} else if (info.type == "bool") {
*value = (info.current_value == "true");
} else if (info.type == "double") {
*value = strtod(info.current_value.c_str(), NULL);
} else {
*value = "Unknown type=" + info.type + " of gflag=" + gflag_name();
}
}
#endif
std::string GFlag::get_value() const {
std::string str;
if (!GFLAGS_NS::GetCommandLineOption(gflag_name().c_str(), &str)) {
return "Unknown gflag=" + gflag_name();
}
return str;
}
bool GFlag::set_value(const char* value) {
return !GFLAGS_NS::SetCommandLineOption(gflag_name().c_str(), value).empty();
}
} // namespace bvar