Skip to content

Commit

Permalink
Disable default stdout/stderr
Browse files Browse the repository at this point in the history
Some devices (mostly Xiaomi) print internal errors using
e.printStackTrace(), flooding the console with irrelevant errors.

Disable system streams used via System.out and System.err streams, to
print only the logs from scrcpy.

Refs #994 <#994>
Refs #4213 <#4213>
  • Loading branch information
rom1v committed Nov 1, 2023
1 parent c64d150 commit 55a15ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
45 changes: 38 additions & 7 deletions server/src/main/java/com/genymobile/scrcpy/Ln.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import android.util.Log;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

/**
* Log both to Android logger (so that logs are visible in "adb logcat") and standard output/error (so that they are visible in the terminal
* directly).
Expand All @@ -11,6 +16,9 @@ public final class Ln {
private static final String TAG = "scrcpy";
private static final String PREFIX = "[server] ";

private static final PrintStream consoleOut = new PrintStream(new FileOutputStream(FileDescriptor.out));
private static final PrintStream consoleErr = new PrintStream(new FileOutputStream(FileDescriptor.err));

enum Level {
VERBOSE, DEBUG, INFO, WARN, ERROR
}
Expand All @@ -21,6 +29,12 @@ private Ln() {
// not instantiable
}

public static void disableSystemStreams() {
PrintStream nullStream = new PrintStream(new NullOutputStream());
System.setOut(nullStream);
System.setErr(nullStream);
}

/**
* Initialize the log level.
* <p>
Expand All @@ -39,30 +53,30 @@ public static boolean isEnabled(Level level) {
public static void v(String message) {
if (isEnabled(Level.VERBOSE)) {
Log.v(TAG, message);
System.out.print(PREFIX + "VERBOSE: " + message + '\n');
consoleOut.print(PREFIX + "VERBOSE: " + message + '\n');
}
}

public static void d(String message) {
if (isEnabled(Level.DEBUG)) {
Log.d(TAG, message);
System.out.print(PREFIX + "DEBUG: " + message + '\n');
consoleOut.print(PREFIX + "DEBUG: " + message + '\n');
}
}

public static void i(String message) {
if (isEnabled(Level.INFO)) {
Log.i(TAG, message);
System.out.print(PREFIX + "INFO: " + message + '\n');
consoleOut.print(PREFIX + "INFO: " + message + '\n');
}
}

public static void w(String message, Throwable throwable) {
if (isEnabled(Level.WARN)) {
Log.w(TAG, message, throwable);
System.err.print(PREFIX + "WARN: " + message + '\n');
consoleErr.print(PREFIX + "WARN: " + message + '\n');
if (throwable != null) {
throwable.printStackTrace();
throwable.printStackTrace(consoleErr);
}
}
}
Expand All @@ -74,14 +88,31 @@ public static void w(String message) {
public static void e(String message, Throwable throwable) {
if (isEnabled(Level.ERROR)) {
Log.e(TAG, message, throwable);
System.err.print(PREFIX + "ERROR: " + message + "\n");
consoleErr.print(PREFIX + "ERROR: " + message + '\n');
if (throwable != null) {
throwable.printStackTrace();
throwable.printStackTrace(consoleErr);
}
}
}

public static void e(String message) {
e(message, null);
}

static class NullOutputStream extends OutputStream {
@Override
public void write(byte[] b) {
// ignore
}

@Override
public void write(byte[] b, int off, int len) {
// ignore
}

@Override
public void write(int b) {
// ignore
}
}
}
3 changes: 2 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static void main(String... args) {
try {
internalMain(args);
} catch (Throwable t) {
t.printStackTrace();
Ln.e(t.getMessage(), t);
status = 1;
} finally {
// By default, the Java process exits when all non-daemon threads are terminated.
Expand All @@ -204,6 +204,7 @@ private static void internalMain(String... args) throws Exception {

Options options = Options.parse(args);

Ln.disableSystemStreams();
Ln.initLogLevel(options.getLogLevel());

Ln.i("Device: [" + Build.MANUFACTURER + "] " + Build.BRAND + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
Expand Down

0 comments on commit 55a15ad

Please sign in to comment.