如何更改protobuf日志记录行为(分隔符)

How can I change protobuf logging behavior (separator)?

本文关键字:分隔符 记录 何更改 protobuf 日志      更新时间:2023-10-16

我在protobuf消息上使用ShortDebugString()来获取字符串并将其记录到文件中。这个函数返回以':'作为分隔符的简洁版本。从protobuf代码中,它看起来像是发生在TextFormat::Printer::PrintField中。

generator.Print(": ");
// Write the field value.
PrintFieldValue(message, reflection, field, field_index, generator);

我想改变':'到'='从ShortDebugString返回的字符串。如何在不更改protobuf库代码本身的情况下实现这一点?我可以替换':'与'='后,我得到字符串从ShortDebugString返回,但我希望避免这种情况,以及。谢谢。

最好的办法是复制整个TextFormat类并对其进行修改。TextFormat代码不使用任何私有的protobuf api,因此将其移出核心库应该没有问题。事实上,TextFormat的设计部分是为了作为一个例子,说明你如何为其他文本格式编写自己的编码器,这听起来正是你想要的。:)