Tron - Omit overlong lines and warn when doing so.

Test: updated LogBuilderTest

Change-Id: I0218acfacca53598a546cea5b3adeecd0d639031
This commit is contained in:
Alison Cichowlas
2017-01-19 09:47:17 -05:00
parent a2feb80b83
commit 38c4680aae
3 changed files with 31 additions and 7 deletions

View File

@@ -23,6 +23,7 @@ import android.view.View;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
/**
* Helper class to assemble more complex logs.
*
@@ -31,6 +32,13 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
public class LogBuilder {
private static final String TAG = "LogBuilder";
// Min required eventlog line length.
// See: android/util/cts/EventLogTest.java
// Size checks enforced here are intended only as sanity checks;
// your logs may be truncated earlier. Please log responsibly.
public static final int MAX_SERIALIZED_SIZE = 4000;
private SparseArray<Object> entries = new SparseArray();
public LogBuilder(int mainCategory) {
@@ -97,7 +105,11 @@ public class LogBuilder {
throw new IllegalArgumentException(
"Value must be loggable type - int, long, float, String");
}
entries.put(tag, value);
if (value.toString().getBytes().length > MAX_SERIALIZED_SIZE) {
Log.i(TAG, "Log value too long, omitted: " + value.toString());
} else {
entries.put(tag, value);
}
return this;
}
@@ -198,18 +210,23 @@ public class LogBuilder {
out[i * 2] = entries.keyAt(i);
out[i * 2 + 1] = entries.valueAt(i);
}
int size = out.toString().getBytes().length;
if (size > MAX_SERIALIZED_SIZE) {
Log.i(TAG, "Log line too long, did not emit: " + size + " bytes.");
throw new RuntimeException();
}
return out;
}
public void deserialize(Object[] items) {
int i = 0;
while(i < items.length) {
while (i < items.length) {
Object key = items[i++];
Object value = i < items.length ? items[i++] : null;
if (key instanceof Integer) {
entries.put((Integer) key, value);
} else {
Log.i(TAG, "Invalid key " + key.toString());
Log.i(TAG, "Invalid key " + key.toString());
}
}
}

View File

@@ -94,9 +94,6 @@ public class MetricsLogger {
}
public static void action(LogBuilder content) {
//EventLog.writeEvent(524292, content.serialize());
// Below would be the *right* way to do this, using the generated
// EventLogTags method, but that doesn't work.
if (content.getType() == MetricsEvent.TYPE_UNKNOWN) {
content.setType(MetricsEvent.TYPE_ACTION);
}

View File

@@ -107,11 +107,21 @@ public class LogBuilderTest extends TestCase {
assertEquals(123.0F, out[7]);
}
public void testCategoryDefault() {
public void testCategoryDefault() {
LogBuilder builder = new LogBuilder(10);
Object[] out = builder.serialize();
assertEquals(MetricsEvent.RESERVED_FOR_LOGBUILDER_CATEGORY, out[0]);
assertEquals(10, out[1]);
}
public void testGiantLogOmitted() {
LogBuilder badBuilder = new LogBuilder(0);
StringBuilder b = new StringBuilder();
for (int i = 0; i < 4000; i++) {
b.append("test, " + i);
}
badBuilder.addTaggedData(100, b.toString());
assertTrue(badBuilder.serialize().length < LogBuilder.MAX_SERIALIZED_SIZE);
}
}