• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 gRPC authors.
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 "src/core/util/dump_args.h"
16 
17 #include "absl/log/check.h"
18 #include "absl/strings/ascii.h"
19 #include "absl/strings/string_view.h"
20 
21 namespace grpc_core {
22 namespace dump_args_detail {
23 
Stringify(CustomSink & sink) const24 void DumpArgs::Stringify(CustomSink& sink) const {
25   // Parse the argument string into a vector of keys.
26   // #__VA_ARGS__ produces a stringified version of the arguments passed to the
27   // macro. It's comma separated, and we can use that to split the string into
28   // keys. Those keys might include parenthesis for e.g. argument lists, and so
29   // we need to skip commas that are inside parenthesis.
30   std::vector<absl::string_view> keys;
31   int depth = 0;
32   const char* start = arg_string_;
33   for (const char* p = arg_string_; *p; ++p) {
34     if (*p == '(') {
35       ++depth;
36     } else if (*p == ')') {
37       --depth;
38     } else if (*p == ',' && depth == 0) {
39       keys.push_back(absl::string_view(start, p - start));
40       start = p + 1;
41     }
42   }
43   keys.push_back(start);
44   CHECK_EQ(keys.size(), arg_dumpers_.size());
45   for (size_t i = 0; i < keys.size(); i++) {
46     if (i != 0) sink.Append(", ");
47     sink.Append(absl::StripAsciiWhitespace(keys[i]));
48     sink.Append(" = ");
49     arg_dumpers_[i](sink);
50   }
51 }
52 
53 }  // namespace dump_args_detail
54 }  // namespace grpc_core
55