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