fix: 调整 adb init 时日志

This commit is contained in:
zhangshuai
2024-11-04 11:24:41 +08:00
parent f90a6deffb
commit 3965a7a44d
2 changed files with 55 additions and 28 deletions

View File

@@ -9,7 +9,9 @@ import net.geedge.common.Constant;
import net.geedge.common.RCode;
import net.geedge.common.T;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
@@ -718,24 +720,11 @@ public class AdbUtil {
* exec shell command
*/
public String execShellCommand(String cmd, Integer timeout){
Process process = commandExec.execForProcess(AdbCommandBuilder.builder()
String result = commandExec.exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell " + cmd)
.build());
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
try {
String result = future.get(timeout, TimeUnit.SECONDS);
return result;
} catch (TimeoutException e) {
process.destroyForcibly();
throw new APIException(RCode.TIMEOUT);
} catch (ExecutionException | InterruptedException e) {
throw new APIException(RCode.ERROR);
} finally {
executor.shutdown();
}
return result;
}
/**
@@ -750,7 +739,7 @@ public class AdbUtil {
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"iptables -L OUTPUT --line-numbers | grep ASW_OUTPUT\""))
.build());
log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [result: {}]", outputChainResult);
log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [exist: {}]", T.StrUtil.isEmpty(outputChainResult));
if (T.StrUtil.isEmpty(outputChainResult)) {
// ASW_OUTPUT 添加到 OUTPUT 链中
this.execShellCommand("shell iptables -A OUTPUT -j ASW_OUTPUT");
@@ -864,20 +853,25 @@ public class AdbUtil {
.build();
Process process = commandExec.execForProcess(command);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), logFile, "UTF-8");
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
// 处理每一行输出
T.FileUtil.appendString(T.StrUtil.concat(true, line, "\n"), logFile, "UTF-8");
}
int exitCode = process.waitFor();
String result = future.get(10, TimeUnit.SECONDS);
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), logFile, "UTF-8");
T.FileUtil.appendString(T.StrUtil.concat(true, result.stripTrailing(), "\n"), logFile, "UTF-8");
return new CommandResult(exitCode, result);
return new CommandResult(exitCode, T.StrUtil.EMPTY);
} catch (Exception e) {
process.destroyForcibly();
throw new APIException(RCode.ERROR);
}finally {
executor.shutdown();
T.IoUtil.close(inputStreamReader);
T.IoUtil.close(bufferedReader);
}
}
}