protobuf中的错误处理
Error handling in protobuf
我是protobuf(C++)的新手,尝试用protobuf编写第一个测试程序。代码为
#include <cstdlib>
#include <iostream>
#include "proto/req.pb.h"
using namespace std;
using namespace google::protobuf;
int main(int argc, char** argv) {
std::string s = "asdfasdf";
auto MyLogHandler = [] (google::protobuf::LogLevel level, const char* filename, int line, const std::string& message)
{
std::cout << "message " << message << std::endl;
};
google::protobuf::SetLogHandler(MyLogHandler);
Request req;
if ( req.ParseFromString(s)){
cout << "Parse - OK" << endl;
}else{
cout << "Parse - ERROR" << endl;
}
return 0;
}
当程序运行时,它只显示错误消息,但不显示任何原因。我如何才能得到错误的原因?
输入数据缺少必填字段。在这种情况下,Protobuf库会编写一条描述问题的日志消息,然后返回false。在这种情况下,日志处理程序将收到错误消息。
输入数据不是有效的Protobuf(它已损坏,或者从一开始就不是Protobuf)。在这种情况下,Protobuf库只是返回false,没有任何错误消息。图书馆在这里真的没有什么有用的信息可以提供。如果发生这种情况,最好的调试方法是在序列化消息之后和解析消息之前转储确切的字节,然后查找差异。
相关文章:
- 使用返回对象的函数处理错误
- C++:处理错误,看不懂我的错误
- 如何处理错误"E1696命令行错误:无法在Visual Studio 2017中打开元数据文件"mscorlib.dll"?
- C++ 异常处理错误输出
- 信号处理错误
- 文件处理I/O C 错误
- 文件处理 - 错误:与 while 循环 (C++) 中的"运算符>>"不匹配(代码::块)
- SDL的事件处理错误
- HMODULE在重建导入地址表IAT时处理错误
- C++ 中的文件处理错误 - 调用"std::basic_fstream<char, std::char_traits<char> >::open(const char[8],
- 在处理线程时,处理错误的正确方法是什么
- 处理C++错误
- 异常处理C++错误
- CUDA图像处理错误
- 文件处理错误
- 代码::阻止资源预处理错误
- 使用模板创建类的新实例,不知道如何处理错误
- Qt 与 opencv 异常处理错误
- 提升 MSM 中的错误处理错误
- 如何处理错误“vtkTrivialProducer :此数据对象不包含请求的范围”