mirror of
https://github.com/Genymobile/scrcpy.git
synced 2026-03-03 02:34:29 +01:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b10bb9f8bf | ||
|
|
55a15ad75d |
@@ -132,20 +132,29 @@ public final class DesktopConnection implements Closeable {
|
||||
return controlSocket;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
public void shutdown() throws IOException {
|
||||
if (videoSocket != null) {
|
||||
videoSocket.shutdownInput();
|
||||
videoSocket.shutdownOutput();
|
||||
videoSocket.close();
|
||||
}
|
||||
if (audioSocket != null) {
|
||||
audioSocket.shutdownInput();
|
||||
audioSocket.shutdownOutput();
|
||||
audioSocket.close();
|
||||
}
|
||||
if (controlSocket != null) {
|
||||
controlSocket.shutdownInput();
|
||||
controlSocket.shutdownOutput();
|
||||
}
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (videoSocket != null) {
|
||||
videoSocket.close();
|
||||
}
|
||||
if (audioSocket != null) {
|
||||
audioSocket.close();
|
||||
}
|
||||
if (controlSocket != null) {
|
||||
controlSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,33 +2,37 @@ package com.genymobile.scrcpy;
|
||||
|
||||
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).
|
||||
*/
|
||||
public final class Ln {
|
||||
|
||||
public interface ConsolePrinter {
|
||||
void printOut(String message);
|
||||
void printErr(String message, Throwable throwable);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
private static ConsolePrinter consolePrinter = new DefaultConsolePrinter();
|
||||
private static Level threshold = Level.INFO;
|
||||
|
||||
private Ln() {
|
||||
// not instantiable
|
||||
}
|
||||
|
||||
public static void setConsolePrinter(ConsolePrinter consolePrinter) {
|
||||
Ln.consolePrinter = consolePrinter;
|
||||
public static void disableSystemStreams() {
|
||||
PrintStream nullStream = new PrintStream(new NullOutputStream());
|
||||
System.setOut(nullStream);
|
||||
System.setErr(nullStream);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,28 +53,31 @@ public final class Ln {
|
||||
public static void v(String message) {
|
||||
if (isEnabled(Level.VERBOSE)) {
|
||||
Log.v(TAG, message);
|
||||
consolePrinter.printOut(PREFIX + "VERBOSE: " + message + '\n');
|
||||
consoleOut.print(PREFIX + "VERBOSE: " + message + '\n');
|
||||
}
|
||||
}
|
||||
|
||||
public static void d(String message) {
|
||||
if (isEnabled(Level.DEBUG)) {
|
||||
Log.d(TAG, message);
|
||||
consolePrinter.printOut(PREFIX + "DEBUG: " + message + '\n');
|
||||
consoleOut.print(PREFIX + "DEBUG: " + message + '\n');
|
||||
}
|
||||
}
|
||||
|
||||
public static void i(String message) {
|
||||
if (isEnabled(Level.INFO)) {
|
||||
Log.i(TAG, message);
|
||||
consolePrinter.printOut(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);
|
||||
consolePrinter.printErr(PREFIX + "WARN: " + message + '\n', throwable);
|
||||
consoleErr.print(PREFIX + "WARN: " + message + '\n');
|
||||
if (throwable != null) {
|
||||
throwable.printStackTrace(consoleErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +88,10 @@ public final class Ln {
|
||||
public static void e(String message, Throwable throwable) {
|
||||
if (isEnabled(Level.ERROR)) {
|
||||
Log.e(TAG, message, throwable);
|
||||
consolePrinter.printErr(PREFIX + "ERROR: " + message + '\n', throwable);
|
||||
consoleErr.print(PREFIX + "ERROR: " + message + '\n');
|
||||
if (throwable != null) {
|
||||
throwable.printStackTrace(consoleErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,18 +99,20 @@ public final class Ln {
|
||||
e(message, null);
|
||||
}
|
||||
|
||||
public static class DefaultConsolePrinter implements ConsolePrinter {
|
||||
static class NullOutputStream extends OutputStream {
|
||||
@Override
|
||||
public void printOut(String message) {
|
||||
System.out.print(message);
|
||||
public void write(byte[] b) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printErr(String message, Throwable throwable) {
|
||||
System.err.print(message);
|
||||
if (throwable != null) {
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
public void write(byte[] b, int off, int len) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +163,8 @@ public final class Server {
|
||||
asyncProcessor.stop();
|
||||
}
|
||||
|
||||
connection.shutdown();
|
||||
|
||||
try {
|
||||
initThread.join();
|
||||
for (AsyncProcessor asyncProcessor : asyncProcessors) {
|
||||
@@ -187,7 +189,7 @@ public final class Server {
|
||||
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.
|
||||
@@ -204,6 +206,7 @@ public final class Server {
|
||||
|
||||
Options options = Options.parse(args);
|
||||
|
||||
Ln.disableSystemStreams();
|
||||
Ln.initLogLevel(options.getLogLevel());
|
||||
|
||||
Ln.i("Device: [" + Build.MANUFACTURER + "] " + Build.BRAND + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
||||
|
||||
@@ -14,12 +14,6 @@ import android.os.Build;
|
||||
import android.os.Looper;
|
||||
import android.os.Parcel;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -61,12 +55,6 @@ public final class Workarounds {
|
||||
mustFillAppInfo = true;
|
||||
mustFillBaseContext = true;
|
||||
mustFillAppContext = true;
|
||||
} else if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {
|
||||
// Trash the messages printed to the console by direct calls to System.out and System.err.
|
||||
// Xiaomi device ROMs may print internal errors using e.printStackTrace(), flooding the console with irrelevant errors.
|
||||
ExclusiveConsolePrinter exclusiveConsolePrinter = new ExclusiveConsolePrinter();
|
||||
exclusiveConsolePrinter.installNullSystemStreams();
|
||||
Ln.setConsolePrinter(new ExclusiveConsolePrinter());
|
||||
}
|
||||
|
||||
if (audio && Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
|
||||
@@ -318,48 +306,4 @@ public final class Workarounds {
|
||||
throw new RuntimeException("Cannot create AudioRecord");
|
||||
}
|
||||
}
|
||||
|
||||
static class ExclusiveConsolePrinter implements Ln.ConsolePrinter {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
private final PrintStream realOut = new PrintStream(new FileOutputStream(FileDescriptor.out));
|
||||
private final PrintStream realErr = new PrintStream(new FileOutputStream(FileDescriptor.err));
|
||||
|
||||
void installNullSystemStreams() {
|
||||
PrintStream nullStream = new PrintStream(new NullOutputStream());
|
||||
System.setOut(nullStream);
|
||||
System.setErr(nullStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printOut(String message) {
|
||||
realOut.print(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printErr(String message, Throwable throwable) {
|
||||
realErr.print(message);
|
||||
if (throwable != null) {
|
||||
StringWriter errors = new StringWriter();
|
||||
throwable.printStackTrace(new PrintWriter(errors));
|
||||
realErr.print(errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user