Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions shell/platform/android/io/flutter/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,4 +96,9 @@ public static void wtf(@NonNull String tag, @NonNull String message) {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
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;
Expand All @@ -38,10 +36,7 @@ public void invokeViewFocused(int viewId) {
}

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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
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;

/**
Expand Down Expand Up @@ -280,14 +277,8 @@ public void notImplemented() {
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -75,7 +73,7 @@ public ByteBuffer encodeErrorEnvelope(
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);
}
Expand All @@ -96,7 +94,7 @@ public ByteBuffer encodeErrorEnvelopeWithStacktrace(
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);
}
Expand Down Expand Up @@ -134,11 +132,4 @@ public Object decodeEnvelope(@NonNull ByteBuffer envelope) {
}
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();
}
}
32 changes: 32 additions & 0 deletions shell/platform/android/test/io/flutter/LogTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}