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