mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
[Android] Add the method 'Log.getStackTraceString' (flutter/engine#35884)
This commit is contained in:
parent
7fb5546042
commit
308e906cf5
@ -5,6 +5,7 @@
|
||||
package io.flutter;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Port of {@link android.util.Log} that only logs in {@link io.flutter.BuildConfig#DEBUG} mode and
|
||||
@ -95,4 +96,9 @@ public class Log {
|
||||
public static void wtf(@NonNull String tag, @NonNull String message, @NonNull Throwable tr) {
|
||||
android.util.Log.wtf(tag, message, tr);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String getStackTraceString(@Nullable Throwable tr) {
|
||||
return android.util.Log.getStackTraceString(tr);
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,8 +11,6 @@ import io.flutter.embedding.engine.dart.DartExecutor;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.common.StandardMethodCodec;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -38,10 +36,7 @@ public class PlatformViewsChannel {
|
||||
}
|
||||
|
||||
private static String detailedExceptionString(Exception exception) {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
PrintWriter printWriter = new PrintWriter(stringWriter);
|
||||
exception.printStackTrace(printWriter);
|
||||
return stringWriter.toString();
|
||||
return Log.getStackTraceString(exception);
|
||||
}
|
||||
|
||||
private final MethodChannel.MethodCallHandler parsingHandler =
|
||||
|
||||
@ -11,9 +11,6 @@ import io.flutter.BuildConfig;
|
||||
import io.flutter.Log;
|
||||
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
|
||||
import io.flutter.plugin.common.BinaryMessenger.BinaryReply;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
@ -280,14 +277,8 @@ public class MethodChannel {
|
||||
Log.e(TAG + name, "Failed to handle method call", e);
|
||||
reply.reply(
|
||||
codec.encodeErrorEnvelopeWithStacktrace(
|
||||
"error", e.getMessage(), null, getStackTrace(e)));
|
||||
"error", e.getMessage(), null, Log.getStackTraceString(e)));
|
||||
}
|
||||
}
|
||||
|
||||
private String getStackTrace(Exception e) {
|
||||
Writer result = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(result));
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,10 +5,8 @@
|
||||
package io.flutter.plugin.common;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import io.flutter.Log;
|
||||
import io.flutter.plugin.common.StandardMessageCodec.ExposedByteArrayOutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
@ -75,7 +73,7 @@ public final class StandardMethodCodec implements MethodCodec {
|
||||
messageCodec.writeValue(stream, errorCode);
|
||||
messageCodec.writeValue(stream, errorMessage);
|
||||
if (errorDetails instanceof Throwable) {
|
||||
messageCodec.writeValue(stream, getStackTrace((Throwable) errorDetails));
|
||||
messageCodec.writeValue(stream, Log.getStackTraceString((Throwable) errorDetails));
|
||||
} else {
|
||||
messageCodec.writeValue(stream, errorDetails);
|
||||
}
|
||||
@ -96,7 +94,7 @@ public final class StandardMethodCodec implements MethodCodec {
|
||||
messageCodec.writeValue(stream, errorCode);
|
||||
messageCodec.writeValue(stream, errorMessage);
|
||||
if (errorDetails instanceof Throwable) {
|
||||
messageCodec.writeValue(stream, getStackTrace((Throwable) errorDetails));
|
||||
messageCodec.writeValue(stream, Log.getStackTraceString((Throwable) errorDetails));
|
||||
} else {
|
||||
messageCodec.writeValue(stream, errorDetails);
|
||||
}
|
||||
@ -134,11 +132,4 @@ public final class StandardMethodCodec implements MethodCodec {
|
||||
}
|
||||
throw new IllegalArgumentException("Envelope corrupted");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static String getStackTrace(@NonNull Throwable t) {
|
||||
Writer result = new StringWriter();
|
||||
t.printStackTrace(new PrintWriter(result));
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package io.flutter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class LogTest {
|
||||
|
||||
@Test
|
||||
public void canGetStacktraceString() {
|
||||
Exception exception = new Exception();
|
||||
String str = Log.getStackTraceString(exception);
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
PrintWriter printWriter = new PrintWriter(stringWriter);
|
||||
exception.printStackTrace(printWriter);
|
||||
String expectStr = stringWriter.toString();
|
||||
|
||||
assertEquals(str, expectStr);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user