gflag.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Date: Sun Aug 9 12:26:03 CST 2015
  18. #include <stdlib.h>
  19. #include <gflags/gflags.h>
  20. #include "bvar/gflag.h"
  21. namespace bvar {
  22. GFlag::GFlag(const butil::StringPiece& gflag_name) {
  23. expose(gflag_name);
  24. }
  25. GFlag::GFlag(const butil::StringPiece& prefix,
  26. const butil::StringPiece& gflag_name)
  27. : _gflag_name(gflag_name.data(), gflag_name.size()) {
  28. expose_as(prefix, gflag_name);
  29. }
  30. void GFlag::describe(std::ostream& os, bool quote_string) const {
  31. GFLAGS_NS::CommandLineFlagInfo info;
  32. if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
  33. if (quote_string) {
  34. os << '"';
  35. }
  36. os << "Unknown gflag=" << gflag_name();
  37. if (quote_string) {
  38. os << '"';
  39. }
  40. } else {
  41. if (quote_string && info.type == "string") {
  42. os << '"' << info.current_value << '"';
  43. } else {
  44. os << info.current_value;
  45. }
  46. }
  47. }
  48. #ifdef BAIDU_INTERNAL
  49. void GFlag::get_value(boost::any* value) const {
  50. GFLAGS_NS::CommandLineFlagInfo info;
  51. if (!GFLAGS_NS::GetCommandLineFlagInfo(gflag_name().c_str(), &info)) {
  52. *value = "Unknown gflag=" + gflag_name();
  53. } else if (info.type == "string") {
  54. *value = info.current_value;
  55. } else if (info.type == "int32") {
  56. *value = static_cast<int>(atoi(info.current_value.c_str()));
  57. } else if (info.type == "int64") {
  58. *value = static_cast<int64_t>(atoll(info.current_value.c_str()));
  59. } else if (info.type == "uint64") {
  60. *value = static_cast<uint64_t>(
  61. strtoull(info.current_value.c_str(), NULL, 10));
  62. } else if (info.type == "bool") {
  63. *value = (info.current_value == "true");
  64. } else if (info.type == "double") {
  65. *value = strtod(info.current_value.c_str(), NULL);
  66. } else {
  67. *value = "Unknown type=" + info.type + " of gflag=" + gflag_name();
  68. }
  69. }
  70. #endif
  71. std::string GFlag::get_value() const {
  72. std::string str;
  73. if (!GFLAGS_NS::GetCommandLineOption(gflag_name().c_str(), &str)) {
  74. return "Unknown gflag=" + gflag_name();
  75. }
  76. return str;
  77. }
  78. bool GFlag::set_value(const char* value) {
  79. return !GFLAGS_NS::SetCommandLineOption(gflag_name().c_str(), value).empty();
  80. }
  81. } // namespace bvar