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

Commit 7daba13f authored by Neil Fuller's avatar Neil Fuller Committed by android-build-merger
Browse files

Merge "Extend LocalLog to enable UTC timestamps" am: 94361340 am: c74594a2

am: 746d69af

Change-Id: I167f79a33218c5b6593c165f9185663f965dcdcd
parents 745fe374 746d69af
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -17,9 +17,11 @@
package android.util;

import android.annotation.UnsupportedAppUsage;
import android.os.SystemClock;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayDeque;
import java.util.Deque;
@@ -33,10 +35,22 @@ public final class LocalLog {
    private final Deque<String> mLog;
    private final int mMaxLines;

    /**
     * {@code true} to use log timestamps expressed in local date/time, {@code false} to use log
     * timestamped expressed with the elapsed realtime clock and UTC system clock. {@code false} is
     * useful when logging behavior that modifies device time zone or system clock.
     */
    private final boolean mUseLocalTimestamps;

    @UnsupportedAppUsage
    public LocalLog(int maxLines) {
        this(maxLines, true /* useLocalTimestamps */);
    }

    public LocalLog(int maxLines, boolean useLocalTimestamps) {
        mMaxLines = Math.max(0, maxLines);
        mLog = new ArrayDeque<>(mMaxLines);
        mUseLocalTimestamps = useLocalTimestamps;
    }

    @UnsupportedAppUsage
@@ -44,7 +58,14 @@ public final class LocalLog {
        if (mMaxLines <= 0) {
            return;
        }
        append(String.format("%s - %s", LocalDateTime.now(), msg));
        final String logLine;
        if (mUseLocalTimestamps) {
            logLine = String.format("%s - %s", LocalDateTime.now(), msg);
        } else {
            logLine = String.format(
                    "%s / %s - %s", SystemClock.elapsedRealtime(), Instant.now(), msg);
        }
        append(logLine);
    }

    private synchronized void append(String logLine) {
+15 −5
Original line number Diff line number Diff line
@@ -29,14 +29,24 @@ import java.util.List;
@LargeTest
public class LocalLogTest extends TestCase {

    public void testA() {
    public void testA_localTimestamps() {
        boolean localTimestamps = true;
        doTestA(localTimestamps);
    }

    public void testA_nonLocalTimestamps() {
        boolean localTimestamps = false;
        doTestA(localTimestamps);
    }

    private void doTestA(boolean localTimestamps) {
        String[] lines = {
                "foo",
                "bar",
                "baz"
        };
        String[] want = lines;
        testcase(new LocalLog(10), lines, want);
        testcase(new LocalLog(10, localTimestamps), lines, want);
    }

    public void testB() {
+2 −1
Original line number Diff line number Diff line
@@ -66,7 +66,8 @@ public final class SimpleTimeDetectorStrategy implements TimeDetectorStrategy {
    private static final long SYSTEM_CLOCK_PARANOIA_THRESHOLD_MILLIS = 2 * 1000;

    // A log for changes made to the system clock and why.
    @NonNull private final LocalLog mTimeChangesLog = new LocalLog(30);
    @NonNull
    private final LocalLog mTimeChangesLog = new LocalLog(30, false /* useLocalTimestamps */);

    // @NonNull after initialize()
    private Callback mCallback;
+1 −1
Original line number Diff line number Diff line
@@ -172,7 +172,7 @@ public class TimeZoneDetectorStrategy {
     * (for use during debugging).
     */
    @NonNull
    private final LocalLog mTimeZoneChangesLog = new LocalLog(30);
    private final LocalLog mTimeZoneChangesLog = new LocalLog(30, false /* useLocalTimestamps */);

    /**
     * A mapping from phoneId to a linked list of phone time zone suggestions (the head being the