Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 38124460 authored by Alison Cichowlas's avatar Alison Cichowlas Committed by Android (Google) Code Review
Browse files

Merge "Tron - Omit overlong lines and warn when doing so."

parents 2cff9319 38c4680a
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -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");
        }
        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,6 +210,11 @@ 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;
    }

+0 −3
Original line number Diff line number Diff line
@@ -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);
        }
+11 −1
Original line number Diff line number Diff line
@@ -114,4 +114,14 @@ public class LogBuilderTest extends TestCase {
        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);
    }

}