Debian Codegraph @

src/core/lib/surface/call_log_batch.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <inttypes.h>
22 #include <stddef.h>
23
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27
28 #include "absl/strings/str_format.h"
29 #include "absl/strings/str_join.h"
30 #include "absl/strings/string_view.h"
31
32 #include <grpc/impl/codegen/grpc_types.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/slice/slice_internal.h"
38 #include "src/core/lib/slice/slice_string_helpers.h"
39 #include "src/core/lib/surface/call.h"
40
41 static void add_metadata(const grpc_metadata* md, size_t count,
42 std::vector<std::string>* b) {
43 if (md == nullptr) {
44 b->push_back("(nil)");
45 return;
46 }
47 for (size_t i = 0; i < count; i++) {
48 b->push_back("\nkey=");
49 b->push_back(std::string(grpc_core::StringViewFromSlice(md[i].key)));
50 b->push_back(" value=");
51 char* dump = grpc_dump_slice(md[i].value, GPR_DUMP_HEX | GPR_DUMP_ASCII);
52 b->push_back(dump);
53 gpr_free(dump);
54 }
55 }
56
57 static std::string grpc_op_string(const grpc_op* op) {
58 std::vector<std::string> parts;
59 switch (op->op) {
60 case GRPC_OP_SEND_INITIAL_METADATA:
61 parts.push_back("SEND_INITIAL_METADATA");
62 add_metadata(op->data.send_initial_metadata.metadata,
63 op->data.send_initial_metadata.count, &parts);
64 break;
65 case GRPC_OP_SEND_MESSAGE:
66 parts.push_back(absl::StrFormat("SEND_MESSAGE ptr=%p",
67 op->data.send_message.send_message));
68 break;
69 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
70 parts.push_back("SEND_CLOSE_FROM_CLIENT");
71 break;
72 case GRPC_OP_SEND_STATUS_FROM_SERVER:
73 parts.push_back(
74 absl::StrFormat("SEND_STATUS_FROM_SERVER status=%d details=",
75 op->data.send_status_from_server.status));
76 if (op->data.send_status_from_server.status_details != nullptr) {
77 char* dump = grpc_dump_slice(
78 *op->data.send_status_from_server.status_details, GPR_DUMP_ASCII);
79 parts.push_back(dump);
80 gpr_free(dump);
81 } else {
82 parts.push_back("(null)");
83 }
84 add_metadata(op->data.send_status_from_server.trailing_metadata,
85 op->data.send_status_from_server.trailing_metadata_count,
86 &parts);
87 break;
88 case GRPC_OP_RECV_INITIAL_METADATA:
89 parts.push_back(absl::StrFormat(
90 "RECV_INITIAL_METADATA ptr=%p",
91 op->data.recv_initial_metadata.recv_initial_metadata));
92 break;
93 case GRPC_OP_RECV_MESSAGE:
94 parts.push_back(absl::StrFormat("RECV_MESSAGE ptr=%p",
95 op->data.recv_message.recv_message));
96 break;
97 case GRPC_OP_RECV_STATUS_ON_CLIENT:
98 parts.push_back(absl::StrFormat(
99 "RECV_STATUS_ON_CLIENT metadata=%p status=%p details=%p",
100 op->data.recv_status_on_client.trailing_metadata,
101 op->data.recv_status_on_client.status,
102 op->data.recv_status_on_client.status_details));
103 break;
104 case GRPC_OP_RECV_CLOSE_ON_SERVER:
105 parts.push_back(absl::StrFormat("RECV_CLOSE_ON_SERVER cancelled=%p",
106 op->data.recv_close_on_server.cancelled));
107 }
108 return absl::StrJoin(parts, "");
109 }
110
111 void grpc_call_log_batch(const char* file, int line, gpr_log_severity severity,
112 const grpc_op* ops, size_t nops) {
113 for (size_t i = 0; i < nops; i++) {
114 gpr_log(file, line, severity, "ops[%" PRIuPTR "]: %s", i,
115 grpc_op_string(&ops[i]).c_str());
116 }
117 }