C++ protobuf调试打印

1、编译全版本的protobuf;

即让应用消息继承于Message

protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/xxx.proto

备注:最好是cd到proto文件所在的目录再导出,否则会出现生成出来的文件找不到头文件的问题

备注:
如果使用lite版本动态链接库,则proto文件需要添加选项option optimize_for = LITE_RUNTIME; 但这样有很多功能不能用;

2、使用对象的DebugString()方法打印应用消息

std::cout << “[NET using DebugString()]” << msg->DebugString() << std::endl;

printf("[NET using DebugString().c_str()] msg=[%s]\n", msg->DebugString().c_str());

3、使用TextFormat类的静态方法PrintToString打印应用消息

(1)包含头文件
#include “google/protobuf/text_format.h” //PrintToString方法所需要的头文件
(2)把消息对象转换成json风格字符串进行调试打印
std::string debugstr;
using namespace google::protobuf;
TextFormat::PrintToString((*msg), &debugstr); //转换到字符串
std::cout << “[NET Debug]” << debugstr << std::endl;//打印
(3)打印效果
[NET Debug]message_head {
msg_type: LOGIN
}

猜你喜欢

转载自blog.csdn.net/skytering/article/details/105332077
今日推荐