Skip to content

Commit afa4930

Browse files
committed
feat(jsonrpc):1.10.10, 为Proto添加setLogEnable()与setLogLabel()接口,方便开发调试
1 parent 3629442 commit afa4930

File tree

8 files changed

+51
-4
lines changed

8 files changed

+51
-4
lines changed

modules/jsonrpc/proto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class Proto {
3535
void setRecvCallback(RecvRequestCallback &&req_cb, RecvRespondCallback &&rsp_cb);
3636
void setSendCallback(SendDataCallback &&cb);
3737

38+
void setLogEnable(bool is_enable) { is_log_enabled_ = is_enable; }
39+
void setLogLabel(const std::string &log_label) { log_label_ = log_label; }
40+
3841
public:
3942
void sendRequest(int id, const std::string &method);
4043
void sendRequest(int id, const std::string &method, const Json &js_params);
@@ -56,6 +59,9 @@ class Proto {
5659
RecvRequestCallback recv_request_cb_;
5760
RecvRespondCallback recv_respond_cb_;
5861
SendDataCallback send_data_cb_;
62+
63+
bool is_log_enabled_ = false;
64+
std::string log_label_;
5965
};
6066

6167
}

modules/jsonrpc/protos/header_stream_proto.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void HeaderStreamProto::sendJson(const Json &js)
3939
{
4040
const auto &json_text = js.dump();
4141

42+
if (is_log_enabled_)
43+
LogTrace("%s send: %s", log_label_.c_str(), json_text.c_str());
44+
4245
std::vector<uint8_t> buff;
4346
util::Serializer pack(buff);
4447

@@ -71,8 +74,13 @@ ssize_t HeaderStreamProto::onRecvData(const void *data_ptr, size_t data_size)
7174
return 0;
7275

7376
const char *str_ptr = static_cast<const char*>(unpack.fetchNoCopy(content_size));
77+
std::string json_text(str_ptr, content_size);
78+
79+
if (is_log_enabled_)
80+
LogTrace("%s recv: %s", log_label_.c_str(), json_text.c_str());
81+
7482
Json js;
75-
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(str_ptr, str_ptr + content_size); });
83+
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(json_text); });
7684
if (is_throw) {
7785
LogNotice("parse json fail");
7886
return -1;

modules/jsonrpc/protos/header_stream_proto_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ TEST(HeaderStreamProto, sendRequest) {
3131
LogOutput_Enable();
3232

3333
HeaderStreamProto proto(0x3e5a);
34+
proto.setLogEnable(true);
3435

3536
int count = 0;
3637
proto.setRecvCallback(
@@ -62,6 +63,7 @@ TEST(HeaderStreamProto, sendRequestWithParams) {
6263
LogOutput_Enable();
6364

6465
HeaderStreamProto proto(0x35ae);
66+
proto.setLogEnable(true);
6567

6668
int count = 0;
6769
proto.setRecvCallback(
@@ -93,6 +95,7 @@ TEST(HeaderStreamProto, sendResult) {
9395
LogOutput_Enable();
9496

9597
HeaderStreamProto proto(0x35ae);
98+
proto.setLogEnable(true);
9699

97100
int count = 0;
98101
proto.setRecvCallback(
@@ -120,6 +123,7 @@ TEST(HeaderStreamProto, sendError) {
120123
LogOutput_Enable();
121124

122125
HeaderStreamProto proto(0x53ea);
126+
proto.setLogEnable(true);
123127

124128
int count = 0;
125129
proto.setRecvCallback(
@@ -146,6 +150,7 @@ TEST(HeaderStreamProto, RecvUncompleteData) {
146150
LogOutput_Enable();
147151

148152
HeaderStreamProto proto(0xea53);
153+
proto.setLogEnable(true);
149154

150155
int count = 0;
151156
proto.setRecvCallback(

modules/jsonrpc/protos/packet_proto.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ void PacketProto::sendJson(const Json &js)
3131
{
3232
if (send_data_cb_) {
3333
const auto &json_text = js.dump();
34+
35+
if (is_log_enabled_)
36+
LogTrace("%s send: %s", log_label_.c_str(), json_text.c_str());
37+
3438
send_data_cb_(json_text.data(), json_text.size());
3539
}
3640
}
@@ -48,8 +52,13 @@ ssize_t PacketProto::onRecvData(const void *data_ptr, size_t data_size)
4852
const char *str_ptr = static_cast<const char*>(data_ptr);
4953
const size_t str_len = data_size;
5054

55+
std::string json_text(str_ptr, str_len);
56+
57+
if (is_log_enabled_)
58+
LogTrace("%s recv: %s", log_label_.c_str(), json_text.c_str());
59+
5160
Json js;
52-
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(str_ptr, str_ptr + str_len); });
61+
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(json_text); });
5362
if (is_throw) {
5463
LogNotice("parse json fail");
5564
return -1;

modules/jsonrpc/protos/packet_proto_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ TEST(PacketProto, sendRequest) {
3131
LogOutput_Enable();
3232

3333
PacketProto proto;
34+
proto.setLogEnable(true);
3435

3536
int count = 0;
3637
proto.setRecvCallback(
@@ -62,6 +63,7 @@ TEST(PacketProto, sendRequestWithParams) {
6263
LogOutput_Enable();
6364

6465
PacketProto proto;
66+
proto.setLogEnable(true);
6567

6668
int count = 0;
6769
proto.setRecvCallback(
@@ -93,6 +95,7 @@ TEST(PacketProto, sendResult) {
9395
LogOutput_Enable();
9496

9597
PacketProto proto;
98+
proto.setLogEnable(true);
9699

97100
int count = 0;
98101
proto.setRecvCallback(
@@ -120,6 +123,7 @@ TEST(PacketProto, sendError) {
120123
LogOutput_Enable();
121124

122125
PacketProto proto;
126+
proto.setLogEnable(true);
123127

124128
int count = 0;
125129
proto.setRecvCallback(
@@ -146,6 +150,7 @@ TEST(PacketProto, RecvUncompleteData) {
146150
LogOutput_Enable();
147151

148152
PacketProto proto;
153+
proto.setLogEnable(true);
149154

150155
int count = 0;
151156
proto.setRecvCallback(

modules/jsonrpc/protos/raw_stream_proto.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ void RawStreamProto::sendJson(const Json &js)
3131
{
3232
if (send_data_cb_) {
3333
const auto &json_text = js.dump();
34+
35+
if (is_log_enabled_)
36+
LogTrace("%s send: %s", log_label_.c_str(), json_text.c_str());
37+
3438
send_data_cb_(json_text.data(), json_text.size());
3539
}
3640
}
@@ -45,8 +49,13 @@ ssize_t RawStreamProto::onRecvData(const void *data_ptr, size_t data_size)
4549
const char *str_ptr = static_cast<const char*>(data_ptr);
4650
auto str_len = util::json::FindEndPos(str_ptr, data_size);
4751
if (str_len > 0) {
52+
std::string json_text(str_ptr, str_len);
53+
54+
if (is_log_enabled_)
55+
LogTrace("%s recv: %s", log_label_.c_str(), json_text.c_str());
56+
4857
Json js;
49-
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(str_ptr, str_ptr + str_len); });
58+
bool is_throw = tbox::CatchThrow([&] { js = Json::parse(json_text); });
5059
if (is_throw) {
5160
LogNotice("parse json fail");
5261
return -1;

modules/jsonrpc/protos/raw_stream_proto_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ TEST(RawStreamProto, sendRequest) {
3131
LogOutput_Enable();
3232

3333
RawStreamProto proto;
34+
proto.setLogEnable(true);
3435

3536
int count = 0;
3637
proto.setRecvCallback(
@@ -62,6 +63,7 @@ TEST(RawStreamProto, sendRequestWithParams) {
6263
LogOutput_Enable();
6364

6465
RawStreamProto proto;
66+
proto.setLogEnable(true);
6567

6668
int count = 0;
6769
proto.setRecvCallback(
@@ -93,6 +95,7 @@ TEST(RawStreamProto, sendResult) {
9395
LogOutput_Enable();
9496

9597
RawStreamProto proto;
98+
proto.setLogEnable(true);
9699

97100
int count = 0;
98101
proto.setRecvCallback(
@@ -120,6 +123,7 @@ TEST(RawStreamProto, sendError) {
120123
LogOutput_Enable();
121124

122125
RawStreamProto proto;
126+
proto.setLogEnable(true);
123127

124128
int count = 0;
125129
proto.setRecvCallback(
@@ -146,6 +150,7 @@ TEST(RawStreamProto, RecvUncompleteData) {
146150
LogOutput_Enable();
147151

148152
RawStreamProto proto;
153+
proto.setLogEnable(true);
149154

150155
int count = 0;
151156
proto.setRecvCallback(

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 10
24-
TBOX_VERSION_REVISION := 9
24+
TBOX_VERSION_REVISION := 10

0 commit comments

Comments
 (0)