Added dedicated loggers for each extension if logger flag is set

This commit is contained in:
dorving 2022-07-12 01:39:01 +02:00
parent c603cdbd61
commit 8b8c4fff72

View File

@ -12,7 +12,9 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.*; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
@ -41,7 +43,7 @@ public final class NormalExtensionRunner implements ExtensionRunner {
@Override @Override
public void runAllExtensions(int port) { public void runAllExtensions(int port) {
if (dirExists(ExecutionInfo.EXTENSIONS_DIRECTORY)){ if (dirExists(ExecutionInfo.EXTENSIONS_DIRECTORY)) {
final File extensionsDirectory = Paths.get(JAR_PATH, ExecutionInfo.EXTENSIONS_DIRECTORY).toFile(); final File extensionsDirectory = Paths.get(JAR_PATH, ExecutionInfo.EXTENSIONS_DIRECTORY).toFile();
final File[] extensionFiles = extensionsDirectory.listFiles(); final File[] extensionFiles = extensionsDirectory.listFiles();
@ -68,7 +70,7 @@ public final class NormalExtensionRunner implements ExtensionRunner {
final String[] split = name.split("\\."); final String[] split = name.split("\\.");
final String ext = "*." + split[split.length - 1]; final String ext = "*." + split[split.length - 1];
final String realName = String.join(".",Arrays.copyOf(split, split.length-1)); final String realName = String.join(".", Arrays.copyOf(split, split.length - 1));
final String newName = realName + "-" + getRandomString() + ext.substring(1); final String newName = realName + "-" + getRandomString() + ext.substring(1);
final Path newPath = Paths.get(JAR_PATH, ExecutionInfo.EXTENSIONS_DIRECTORY, newName); final Path newPath = Paths.get(JAR_PATH, ExecutionInfo.EXTENSIONS_DIRECTORY, newName);
@ -101,7 +103,7 @@ public final class NormalExtensionRunner implements ExtensionRunner {
for (int i = 0; i < execCommand.length; i++) { for (int i = 0; i < execCommand.length; i++) {
execCommand[i] = execCommand[i] execCommand[i] = execCommand[i]
.replace("{path}", path) .replace("{path}", path)
.replace("{port}", port+"") .replace("{port}", port + "")
.replace("{filename}", filename) .replace("{filename}", filename)
.replace("{cookie}", cookie); .replace("{cookie}", cookie);
} }
@ -110,36 +112,37 @@ public final class NormalExtensionRunner implements ExtensionRunner {
final Process process = processBuilder.start(); final Process process = processBuilder.start();
maybeLogExtension(path, process); maybeLogExtension(path, process);
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Failed to run extension at path {} using port {}", path, port, e); LOGGER.error("Failed to run extension at path {} using port {}", path, port, e);
} }
} }
public static void maybeLogExtension(String path, Process process) { public static void maybeLogExtension(String path, Process process) {
if (GEarth.hasFlag(ExtensionRunner.SHOW_EXTENSIONS_LOG)) { if (GEarth.hasFlag(ExtensionRunner.SHOW_EXTENSIONS_LOG)) {
final String separator = "" + System.lineSeparator(); final Logger logger = LoggerFactory.getLogger(path);
LOGGER.info(path + separator + "Launching" + separator + "----------" + separator);
logger.info("Launching...");
final BufferedReader processInputReader = new BufferedReader(new InputStreamReader(process.getInputStream())); final BufferedReader processInputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
new Thread(() -> { new Thread(() -> {
try { try {
String line; String line;
while((line = processInputReader.readLine()) != null) while ((line = processInputReader.readLine()) != null)
LOGGER.info(path + separator + "Output" + separator + line + separator + "----------" + separator); logger.info(line);
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Failed to read input line from process {}", process, e); LOGGER.error("Failed to read input line from process {}", process, e);
} }
}).start(); }, path+"-input").start();
final BufferedReader processErrorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); final BufferedReader processErrorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
new Thread(() -> { new Thread(() -> {
try { try {
String line; String line;
while((line = processErrorReader.readLine()) != null) { while ((line = processErrorReader.readLine()) != null)
LOGGER.error(path + separator + "Error" + separator + line + separator + "----------" + separator); logger.error(line);
}
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Failed to read error line from process {}", process, e); LOGGER.error("Failed to read error line from process {}", process, e);
} }