Skip writing message field in an atom if it's empty

Test: unit test added
Change-Id: I825b1ce526944a20fe65705508ad180ece37492c
This commit is contained in:
Yao Chen
2018-11-29 09:39:45 -08:00
parent d8a9db8486
commit 8e6f998300
4 changed files with 47 additions and 5 deletions

View File

@@ -653,7 +653,7 @@ float LogEvent::GetFloat(size_t key, status_t* err) const {
string LogEvent::ToString() const {
string result;
result += StringPrintf("{ %lld %lld (%d)", (long long)mLogdTimestampNs,
result += StringPrintf("{ uid(%d) %lld %lld (%d)", mLogUid, (long long)mLogdTimestampNs,
(long long)mElapsedTimestampNs, mTagId);
for (const auto& value : mValues) {
result +=

View File

@@ -343,9 +343,11 @@ void writeFieldValueTreeToStreamHelper(int tagId, const std::vector<FieldValue>&
}
}
if (isBytesField) {
protoOutput->write(FIELD_TYPE_MESSAGE | fieldNum,
(const char*)dim.mValue.str_value.c_str(),
dim.mValue.str_value.length());
if (dim.mValue.str_value.length() > 0) {
protoOutput->write(FIELD_TYPE_MESSAGE | fieldNum,
(const char*)dim.mValue.str_value.c_str(),
dim.mValue.str_value.length());
}
} else {
protoOutput->write(FIELD_TYPE_STRING | fieldNum, dim.mValue.str_value);
}

View File

@@ -444,7 +444,44 @@ TEST(LogEventTest, TestBinaryFieldAtom) {
EXPECT_EQ(orig_str, result_str);
}
TEST(LogEventTest, TestBinaryFieldAtom_empty) {
Atom launcherAtom;
auto launcher_event = launcherAtom.mutable_launcher_event();
launcher_event->set_action(stats::launcher::LauncherAction::LONGPRESS);
launcher_event->set_src_state(stats::launcher::LauncherState::OVERVIEW);
launcher_event->set_dst_state(stats::launcher::LauncherState::ALLAPPS);
// empty string.
string extension_str;
LogEvent event1(Atom::kLauncherEventFieldNumber, 1000);
event1.write((int32_t)stats::launcher::LauncherAction::LONGPRESS);
event1.write((int32_t)stats::launcher::LauncherState::OVERVIEW);
event1.write((int64_t)stats::launcher::LauncherState::ALLAPPS);
event1.write(extension_str);
event1.init();
ProtoOutputStream proto;
event1.ToProto(proto);
std::vector<uint8_t> outData;
outData.resize(proto.size());
size_t pos = 0;
auto iter = proto.data();
while (iter.readBuffer() != NULL) {
size_t toRead = iter.currentToRead();
std::memcpy(&(outData[pos]), iter.readBuffer(), toRead);
pos += toRead;
iter.rp()->move(toRead);
}
std::string result_str(outData.begin(), outData.end());
std::string orig_str;
launcherAtom.SerializeToString(&orig_str);
EXPECT_EQ(orig_str, result_str);
}
} // namespace statsd
} // namespace os

View File

@@ -1128,7 +1128,10 @@ write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp
hadStringOrChain = true;
fprintf(out, " jbyte* jbyte_array%d;\n", argIndex);
fprintf(out, " const char* str%d;\n", argIndex);
fprintf(out, " if (arg%d != NULL) {\n", argIndex);
fprintf(out,
" if (arg%d != NULL && env->GetArrayLength(arg%d) > "
"0) {\n",
argIndex, argIndex);
fprintf(out,
" jbyte_array%d = "
"env->GetByteArrayElements(arg%d, NULL);\n",