diff options
-rw-r--r-- | core/java/android/util/Log.java | 2 | ||||
-rw-r--r-- | ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRuntimeEnvironmentController.java | 9 | ||||
-rw-r--r-- | ravenwood/runtime-helper-src/framework/android/util/Log_ravenwood.java (renamed from ravenwood/runtime-helper-src/framework/android/util/Log_host.java) | 54 | ||||
-rw-r--r-- | ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java | 2 | ||||
-rw-r--r-- | ravenwood/runtime-jni/ravenwood_runtime.cpp | 4 |
5 files changed, 56 insertions, 15 deletions
diff --git a/core/java/android/util/Log.java b/core/java/android/util/Log.java index 1dd9d46fdfb7..f8737a55a5b0 100644 --- a/core/java/android/util/Log.java +++ b/core/java/android/util/Log.java @@ -75,7 +75,7 @@ import java.net.UnknownHostException; @android.ravenwood.annotation.RavenwoodClassLoadHook( "com.android.platform.test.ravenwood.runtimehelper.ClassLoadHook.onClassLoaded") // 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 { /** @hide */ @IntDef({ASSERT, ERROR, WARN, INFO, DEBUG, VERBOSE}) diff --git a/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRuntimeEnvironmentController.java b/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRuntimeEnvironmentController.java index c2ed45d8f427..8c9f30ba262b 100644 --- a/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRuntimeEnvironmentController.java +++ b/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRuntimeEnvironmentController.java @@ -58,6 +58,7 @@ import android.provider.DeviceConfig_host; import android.system.ErrnoException; import android.system.Os; import android.util.Log; +import android.util.Log_ravenwood; import android.view.DisplayAdjustments; import androidx.test.platform.app.InstrumentationRegistry; @@ -219,6 +220,9 @@ public class RavenwoodRuntimeEnvironmentController { // Some process-wide initialization. (maybe redirect stdout/stderr) RavenwoodCommonUtils.loadJniLibrary(LIBRAVENWOOD_INITIALIZER_NAME); + // Redirect stdout/stdin to the Log API. + RuntimeInit.redirectLogStreams(); + dumpCommandLineArgs(); // We haven't initialized liblog yet, so directly write to System.out here. @@ -232,6 +236,8 @@ public class RavenwoodRuntimeEnvironmentController { // Make sure libravenwood_runtime is loaded. System.load(RavenwoodCommonUtils.getJniLibraryPath(RAVENWOOD_NATIVE_RUNTIME_NAME)); + Log_ravenwood.onRavenwoodRuntimeNativeReady(); + // Do the basic set up for the android sysprops. RavenwoodSystemProperties.initialize(); setSystemProperties(null); @@ -250,9 +256,6 @@ public class RavenwoodRuntimeEnvironmentController { // Make sure libandroid_runtime is loaded. RavenwoodNativeLoader.loadFrameworkNativeCode(); - // Redirect stdout/stdin to liblog. - RuntimeInit.redirectLogStreams(); - // Touch some references early to ensure they're <clinit>'ed Objects.requireNonNull(Build.TYPE); Objects.requireNonNull(Build.VERSION.SDK); diff --git a/ravenwood/runtime-helper-src/framework/android/util/Log_host.java b/ravenwood/runtime-helper-src/framework/android/util/Log_ravenwood.java index c85bd23db893..7b26fe531e7e 100644 --- a/ravenwood/runtime-helper-src/framework/android/util/Log_host.java +++ b/ravenwood/runtime-helper-src/framework/android/util/Log_ravenwood.java @@ -18,9 +18,13 @@ package android.util; import android.util.Log.Level; import com.android.internal.os.RuntimeInit; +import com.android.ravenwood.RavenwoodRuntimeNative; import com.android.ravenwood.common.RavenwoodCommonUtils; 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}. @@ -29,7 +33,10 @@ import java.io.PrintStream; * In order to switch to this Java implementation, uncomment the @RavenwoodNativeSubstitutionClass * 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) { return true; @@ -39,15 +46,6 @@ public class Log_host { if (priority < Log.INFO && !RavenwoodCommonUtils.RAVENWOOD_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; switch (priority) { @@ -60,8 +58,12 @@ public class Log_host { 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")) { - getRealOut().println(String.format("logd: [%s] %s %s: %s", buffer, prio, tag, s)); + out.print(leading); + out.println(s); } return msg.length(); } @@ -81,4 +83,34 @@ public class Log_host { 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(); + } } diff --git a/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java b/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java index 7b940b423b69..0cfce663e93a 100644 --- a/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java +++ b/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java @@ -58,6 +58,8 @@ public class RavenwoodRuntimeNative { public static native void clearSystemProperties(); + public static native int getpid(); + public static native int gettid(); public static long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException { diff --git a/ravenwood/runtime-jni/ravenwood_runtime.cpp b/ravenwood/runtime-jni/ravenwood_runtime.cpp index c1993f691686..bab4c7e0fd14 100644 --- a/ravenwood/runtime-jni/ravenwood_runtime.cpp +++ b/ravenwood/runtime-jni/ravenwood_runtime.cpp @@ -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)); } +static jint Linux_getpid(JNIEnv* env, jobject) { + return getpid(); +} static jint Linux_gettid(JNIEnv* env, jobject) { // 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 }, { "nOpen", "(Ljava/lang/String;II)I", (void*)Linux_open }, { "setenv", "(Ljava/lang/String;Ljava/lang/String;Z)V", (void*)Linux_setenv }, + { "getpid", "()I", (void*)Linux_getpid }, { "gettid", "()I", (void*)Linux_gettid }, }; |