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 "src/core/lib/surface/call.h"
22
23 #include <inttypes.h>
24
25 #include <vector>
26
27 #include "absl/strings/str_format.h"
28 #include "absl/strings/str_join.h"
29
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/string_util.h>
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/slice/slice_string_helpers.h"
34
add_metadata(const grpc_metadata * md,size_t count,std::vector<std::string> * b)35 static void add_metadata(const grpc_metadata* md, size_t count,
36 std::vector<std::string>* b) {
37 if (md == nullptr) {
38 b->push_back("(nil)");
39 return;
40 }
41 for (size_t i = 0; i < count; i++) {
42 b->push_back("\nkey=");
43 b->push_back(std::string(grpc_core::StringViewFromSlice(md[i].key)));
44 b->push_back(" value=");
45 char* dump = grpc_dump_slice(md[i].value, GPR_DUMP_HEX | GPR_DUMP_ASCII);
46 b->push_back(dump);
47 gpr_free(dump);
48 }
49 }
50
grpc_op_string(const grpc_op * op)51 static std::string grpc_op_string(const grpc_op* op) {
52 std::vector<std::string> parts;
53 switch (op->op) {
54 case GRPC_OP_SEND_INITIAL_METADATA:
55 parts.push_back("SEND_INITIAL_METADATA");
56 add_metadata(op->data.send_initial_metadata.metadata,
57 op->data.send_initial_metadata.count, &parts);
58 break;
59 case GRPC_OP_SEND_MESSAGE:
60 parts.push_back(absl::StrFormat("SEND_MESSAGE ptr=%p",
61 op->data.send_message.send_message));
62 break;
63 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
64 parts.push_back("SEND_CLOSE_FROM_CLIENT");
65 break;
66 case GRPC_OP_SEND_STATUS_FROM_SERVER:
67 parts.push_back(
68 absl::StrFormat("SEND_STATUS_FROM_SERVER status=%d details=",
69 op->data.send_status_from_server.status));
70 if (op->data.send_status_from_server.status_details != nullptr) {
71 char* dump = grpc_dump_slice(
72 *op->data.send_status_from_server.status_details, GPR_DUMP_ASCII);
73 parts.push_back(dump);
74 gpr_free(dump);
75 } else {
76 parts.push_back("(null)");
77 }
78 add_metadata(op->data.send_status_from_server.trailing_metadata,
79 op->data.send_status_from_server.trailing_metadata_count,
80 &parts);
81 break;
82 case GRPC_OP_RECV_INITIAL_METADATA:
83 parts.push_back(absl::StrFormat(
84 "RECV_INITIAL_METADATA ptr=%p",
85 op->data.recv_initial_metadata.recv_initial_metadata));
86 break;
87 case GRPC_OP_RECV_MESSAGE:
88 parts.push_back(absl::StrFormat("RECV_MESSAGE ptr=%p",
89 op->data.recv_message.recv_message));
90 break;
91 case GRPC_OP_RECV_STATUS_ON_CLIENT:
92 parts.push_back(absl::StrFormat(
93 "RECV_STATUS_ON_CLIENT metadata=%p status=%p details=%p",
94 op->data.recv_status_on_client.trailing_metadata,
95 op->data.recv_status_on_client.status,
96 op->data.recv_status_on_client.status_details));
97 break;
98 case GRPC_OP_RECV_CLOSE_ON_SERVER:
99 parts.push_back(absl::StrFormat("RECV_CLOSE_ON_SERVER cancelled=%p",
100 op->data.recv_close_on_server.cancelled));
101 }
102 return absl::StrJoin(parts, "");
103 }
104
grpc_call_log_batch(const char * file,int line,gpr_log_severity severity,const grpc_op * ops,size_t nops)105 void grpc_call_log_batch(const char* file, int line, gpr_log_severity severity,
106 const grpc_op* ops, size_t nops) {
107 for (size_t i = 0; i < nops; i++) {
108 gpr_log(file, line, severity, "ops[%" PRIuPTR "]: %s", i,
109 grpc_op_string(&ops[i]).c_str());
110 }
111 }
112