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