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

Commit 2606c0ed authored by Makoto Onuki's avatar Makoto Onuki Committed by Automerger Merge Worker
Browse files

Merge "Make the log format more realistic" into main am: e2f6a591 am: 3863a8d7

parents c3828392 3863a8d7
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -75,7 +75,7 @@ import java.net.UnknownHostException;
@android.ravenwood.annotation.RavenwoodClassLoadHook(
@android.ravenwood.annotation.RavenwoodClassLoadHook(
        "com.android.platform.test.ravenwood.runtimehelper.ClassLoadHook.onClassLoaded")
        "com.android.platform.test.ravenwood.runtimehelper.ClassLoadHook.onClassLoaded")
// Uncomment the following annotation to switch to the Java substitution version.
// Uncomment the following annotation to switch to the Java substitution version.
@android.ravenwood.annotation.RavenwoodRedirectionClass("Log_host")
@android.ravenwood.annotation.RavenwoodRedirectionClass("Log_ravenwood")
public final class Log {
public final class Log {
    /** @hide */
    /** @hide */
    @IntDef({ASSERT, ERROR, WARN, INFO, DEBUG, VERBOSE})
    @IntDef({ASSERT, ERROR, WARN, INFO, DEBUG, VERBOSE})
+6 −3
Original line number Original line Diff line number Diff line
@@ -58,6 +58,7 @@ import android.provider.DeviceConfig_host;
import android.system.ErrnoException;
import android.system.ErrnoException;
import android.system.Os;
import android.system.Os;
import android.util.Log;
import android.util.Log;
import android.util.Log_ravenwood;
import android.view.DisplayAdjustments;
import android.view.DisplayAdjustments;


import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -217,6 +218,9 @@ public class RavenwoodRuntimeEnvironmentController {
        // Some process-wide initialization. (maybe redirect stdout/stderr)
        // Some process-wide initialization. (maybe redirect stdout/stderr)
        RavenwoodCommonUtils.loadJniLibrary(LIBRAVENWOOD_INITIALIZER_NAME);
        RavenwoodCommonUtils.loadJniLibrary(LIBRAVENWOOD_INITIALIZER_NAME);


        // Redirect stdout/stdin to the Log API.
        RuntimeInit.redirectLogStreams();

        dumpCommandLineArgs();
        dumpCommandLineArgs();


        // We haven't initialized liblog yet, so directly write to System.out here.
        // We haven't initialized liblog yet, so directly write to System.out here.
@@ -230,6 +234,8 @@ public class RavenwoodRuntimeEnvironmentController {
        // Make sure libravenwood_runtime is loaded.
        // Make sure libravenwood_runtime is loaded.
        System.load(RavenwoodCommonUtils.getJniLibraryPath(RAVENWOOD_NATIVE_RUNTIME_NAME));
        System.load(RavenwoodCommonUtils.getJniLibraryPath(RAVENWOOD_NATIVE_RUNTIME_NAME));


        Log_ravenwood.onRavenwoodRuntimeNativeReady();

        // Do the basic set up for the android sysprops.
        // Do the basic set up for the android sysprops.
        RavenwoodSystemProperties.initialize();
        RavenwoodSystemProperties.initialize();


@@ -247,9 +253,6 @@ public class RavenwoodRuntimeEnvironmentController {
        // Make sure libandroid_runtime is loaded.
        // Make sure libandroid_runtime is loaded.
        RavenwoodNativeLoader.loadFrameworkNativeCode();
        RavenwoodNativeLoader.loadFrameworkNativeCode();


        // Redirect stdout/stdin to liblog.
        RuntimeInit.redirectLogStreams();

        // Touch some references early to ensure they're <clinit>'ed
        // Touch some references early to ensure they're <clinit>'ed
        Objects.requireNonNull(Build.TYPE);
        Objects.requireNonNull(Build.TYPE);
        Objects.requireNonNull(Build.VERSION.SDK);
        Objects.requireNonNull(Build.VERSION.SDK);
+43 −11
Original line number Original line Diff line number Diff line
@@ -18,9 +18,13 @@ package android.util;
import android.util.Log.Level;
import android.util.Log.Level;


import com.android.internal.os.RuntimeInit;
import com.android.internal.os.RuntimeInit;
import com.android.ravenwood.RavenwoodRuntimeNative;
import com.android.ravenwood.common.RavenwoodCommonUtils;
import com.android.ravenwood.common.RavenwoodCommonUtils;


import java.io.PrintStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
/**
 * Ravenwood "native substitution" class for {@link android.util.Log}.
 * Ravenwood "native substitution" class for {@link android.util.Log}.
@@ -29,7 +33,10 @@ import java.io.PrintStream;
 * In order to switch to this Java implementation, uncomment the @RavenwoodNativeSubstitutionClass
 * In order to switch to this Java implementation, uncomment the @RavenwoodNativeSubstitutionClass
 * annotation on {@link android.util.Log}.
 * annotation on {@link android.util.Log}.
 */
 */
public class Log_host {
public class Log_ravenwood {

    public static final SimpleDateFormat sTimestampFormat =
            new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US);


    public static boolean isLoggable(String tag, @Level int level) {
    public static boolean isLoggable(String tag, @Level int level) {
        return true;
        return true;
@@ -39,15 +46,6 @@ public class Log_host {
        if (priority < Log.INFO && !RavenwoodCommonUtils.RAVENWOOD_VERBOSE_LOGGING) {
        if (priority < Log.INFO && !RavenwoodCommonUtils.RAVENWOOD_VERBOSE_LOGGING) {
            return msg.length(); // No verbose logging.
            return msg.length(); // No verbose logging.
        }
        }
        final String buffer;
        switch (bufID) {
            case Log.LOG_ID_MAIN: buffer = "main"; break;
            case Log.LOG_ID_RADIO: buffer = "radio"; break;
            case Log.LOG_ID_EVENTS: buffer = "event"; break;
            case Log.LOG_ID_SYSTEM: buffer = "system"; break;
            case Log.LOG_ID_CRASH: buffer = "crash"; break;
            default: buffer = "buf:" + bufID; break;
        }


        final String prio;
        final String prio;
        switch (priority) {
        switch (priority) {
@@ -60,8 +58,12 @@ public class Log_host {
            default: prio = "prio:" + priority; break;
            default: prio = "prio:" + priority; break;
        }
        }


        String leading =  sTimestampFormat.format(new Date())
                + " %-6d %-6d %s %-8s: ".formatted(getPid(), getTid(), prio, tag);
        var out = getRealOut();
        for (String s : msg.split("\\n")) {
        for (String s : msg.split("\\n")) {
            getRealOut().println(String.format("logd: [%s] %s %s: %s", buffer, prio, tag, s));
            out.print(leading);
            out.println(s);
        }
        }
        return msg.length();
        return msg.length();
    }
    }
@@ -81,4 +83,34 @@ public class Log_host {
            return System.out;
            return System.out;
        }
        }
    }
    }

    /**
     * PID. We need to use a JNI method to get it, but JNI isn't initially ready.
     * Call {@link #onRavenwoodRuntimeNativeReady} to signal when JNI is ready, at which point
     * we set this field.
     * (We don't want to call native methods that may not be fully initialized even with a
     * try-catch, because partially initialized JNI methods could crash the process.)
     */
    private static volatile int sPid = 0;

    private static ThreadLocal<Integer> sTid =
            ThreadLocal.withInitial(RavenwoodRuntimeNative::gettid);

    /**
     * Call it when {@link RavenwoodRuntimeNative} is usable.
     */
    public static void onRavenwoodRuntimeNativeReady() {
        sPid = RavenwoodRuntimeNative.getpid();
    }

    private static int getPid() {
        return sPid;
    }

    private static int getTid() {
        if (sPid == 0) {
            return 0; // Native methods not ready yet.
        }
        return sTid.get();
    }
}
}
+2 −0
Original line number Original line Diff line number Diff line
@@ -62,6 +62,8 @@ public class RavenwoodRuntimeNative {
        removeSystemProperty(null);
        removeSystemProperty(null);
    }
    }


    public static native int getpid();

    public static native int gettid();
    public static native int gettid();


    public static long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException {
    public static long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException {
+4 −0
Original line number Original line Diff line number Diff line
@@ -174,6 +174,9 @@ static void Linux_setenv(JNIEnv* env, jobject, jstring javaName, jstring javaVal
    throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite ? 1 : 0));
    throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite ? 1 : 0));
}
}


static jint Linux_getpid(JNIEnv* env, jobject) {
    return getpid();
}


static jint Linux_gettid(JNIEnv* env, jobject) {
static jint Linux_gettid(JNIEnv* env, jobject) {
    // gettid(2() was added in glibc 2.30 but Android uses an older version in prebuilt.
    // gettid(2() was added in glibc 2.30 but Android uses an older version in prebuilt.
@@ -196,6 +199,7 @@ static const JNINativeMethod sMethods[] =
    { "stat", "(Ljava/lang/String;)Landroid/system/StructStat;", (void*)Linux_stat },
    { "stat", "(Ljava/lang/String;)Landroid/system/StructStat;", (void*)Linux_stat },
    { "nOpen", "(Ljava/lang/String;II)I", (void*)Linux_open },
    { "nOpen", "(Ljava/lang/String;II)I", (void*)Linux_open },
    { "setenv", "(Ljava/lang/String;Ljava/lang/String;Z)V", (void*)Linux_setenv },
    { "setenv", "(Ljava/lang/String;Ljava/lang/String;Z)V", (void*)Linux_setenv },
    { "getpid", "()I", (void*)Linux_getpid },
    { "gettid", "()I", (void*)Linux_gettid },
    { "gettid", "()I", (void*)Linux_gettid },
};
};