in pb.cpp [339:390]
bool PbEncoder::SerializeToString(const LogGroup& logGroup, SLS_OUT std::string& out)
{
size_t totalMsgSize = PbLength(logGroup);
out.resize(totalMsgSize);
// write in order
char* begin = &out[0];
char* pos = begin + totalMsgSize;
// topic = 3
if (!logGroup.topic.empty())
{
char* writeBeginPos = pos - WithFieldHeader(logGroup.topic.size());
WriteStrWithHead(logGroup.topic, (char)((3 << 3) | 2), writeBeginPos);
pos = writeBeginPos;
}
// source = 4
if (!logGroup.source.empty())
{
char* writeBeginPos = pos - WithFieldHeader(logGroup.source.size());
WriteStrWithHead(logGroup.source, (char)((4 << 3) | 2), writeBeginPos);
pos = writeBeginPos;
}
// tags = 6
for (auto it = logGroup.logTags.rbegin(); it != logGroup.logTags.rend(); ++it)
{
auto& logTag = *it;
size_t msgSize = PbLength(logTag.key, logTag.value);
char* writeBeginPos = pos - WithFieldHeader(msgSize);
WriteStrPairWithHead((char)((6 << 3) | 2), msgSize, logTag.key, logTag.value, writeBeginPos);
pos = writeBeginPos;
}
// logs = 1
for (auto it = logGroup.logs.rbegin(); it != logGroup.logs.rend(); ++it)
{
auto& log = *it;
size_t msgSize = PbLength(log);
char* writeBeginPos = pos - WithFieldHeader(msgSize);
WriteLogWithHead(msgSize, log, writeBeginPos);
pos = writeBeginPos;
}
if (pos != begin)
{
out.clear();
return false;
}
return true;
}