Google Cloud C++ Client  1.32.1
C++ Client Library for Google Cloud Platform
tracing_options.cc
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/tracing_options.h"
16 #include "absl/types/optional.h"
17 #include <algorithm>
18 #include <cstdint>
19 #include <string>
20 
21 namespace google {
22 namespace cloud {
23 inline namespace GOOGLE_CLOUD_CPP_NS {
24 namespace {
25 
26 absl::optional<bool> ParseBoolean(std::string const& str) {
27  for (auto const* t : {"Y", "y", "T", "t", "1", "on"}) {
28  if (str == t) return true;
29  }
30  for (auto const* f : {"N", "n", "F", "f", "0", "off"}) {
31  if (str == f) return false;
32  }
33  return {};
34 }
35 
36 absl::optional<std::int64_t> ParseInteger(std::string const& str) {
37  std::size_t econv = -1;
38  auto val = std::stoll(str, &econv);
39  if (econv != str.size()) return {};
40  return val;
41 }
42 
43 } // namespace
44 
45 bool operator==(TracingOptions const& a, TracingOptions const& b) {
46  return a.single_line_mode_ == b.single_line_mode_ &&
47  a.use_short_repeated_primitives_ == b.use_short_repeated_primitives_ &&
48  a.truncate_string_field_longer_than_ ==
49  b.truncate_string_field_longer_than_;
50 }
51 
52 TracingOptions& TracingOptions::SetOptions(std::string const& str) {
53  auto const beg = str.begin();
54  auto const end = str.end();
55  for (auto pos = beg;;) {
56  auto const comma = std::find(pos, end, ',');
57  auto const equal = std::find(pos, comma, '=');
58  std::string const opt{pos, equal};
59  std::string const val{equal + (equal == comma ? 0 : 1), comma};
60  if (opt == "single_line_mode") {
61  if (auto v = ParseBoolean(val)) single_line_mode_ = *v;
62  } else if (opt == "use_short_repeated_primitives") {
63  if (auto v = ParseBoolean(val)) use_short_repeated_primitives_ = *v;
64  } else if (opt == "truncate_string_field_longer_than") {
65  if (auto v = ParseInteger(val)) truncate_string_field_longer_than_ = *v;
66  }
67  if (comma == end) break;
68  pos = comma + 1;
69  }
70  return *this;
71 }
72 
73 } // namespace GOOGLE_CLOUD_CPP_NS
74 } // namespace cloud
75 } // namespace google