gflag.cpp 2.83 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// 
// 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.

gejun's avatar
gejun committed
15 16 17 18 19 20 21 22 23
// 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 {

24
GFlag::GFlag(const butil::StringPiece& gflag_name) {
gejun's avatar
gejun committed
25 26 27
    expose(gflag_name);
}

28 29
GFlag::GFlag(const butil::StringPiece& prefix,
      const butil::StringPiece& gflag_name)
gejun's avatar
gejun committed
30 31 32 33 34
    : _gflag_name(gflag_name.data(), gflag_name.size()) {
    expose_as(prefix, gflag_name);
}

void GFlag::describe(std::ostream& os, bool quote_string) const {
35 36
    GFLAGS_NS::CommandLineFlagInfo info;
    if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
gejun's avatar
gejun committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        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 {
55 56
    GFLAGS_NS::CommandLineFlagInfo info;
    if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
gejun's avatar
gejun committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        *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;
79
    if (!GFLAGS_NS::GetCommandLineOption(gflag_name().c_str(), &str)) {
gejun's avatar
gejun committed
80 81 82 83 84 85
        return "Unknown gflag=" + gflag_name();
    }
    return str;
}

bool GFlag::set_value(const char* value) {
86
    return !GFLAGS_NS::SetCommandLineOption(gflag_name().c_str(), value).empty();
gejun's avatar
gejun committed
87 88 89
}

}  // namespace bvar