Various stdio fixes [release]

This commit is contained in:
crschnick
2026-09-12 17:32:08 +00:00
parent 71176c87ea
commit 8eb7cde71f
5 changed files with 49 additions and 7 deletions
@@ -18,6 +18,7 @@ import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.SLF4JServiceProvider;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
@@ -121,7 +122,7 @@ public class AppLogs {
var file = usedLogsDir.resolve(AppNames.ofMain().getKebapName() + ".log");
var fos = new FileOutputStream(file.toFile(), true);
var buf = new BufferedOutputStream(fos);
outFileStream = new PrintStream(buf, false);
outFileStream = new PrintStream(buf, false, StandardCharsets.UTF_8);
} catch (Exception ex) {
ErrorEventFactory.fromThrowable(ex).build().handle();
}
@@ -175,7 +176,7 @@ public class AppLogs {
@Override
public void write(int b) {
if (b == '\r' || b == '\n') {
String line = baos.toString();
String line = baos.toString(StandardCharsets.UTF_8);
if (line.length() == 0) {
return;
}
@@ -186,7 +187,7 @@ public class AppLogs {
baos.write(b);
}
}
}));
},true, StandardCharsets.UTF_8));
}
private void hookUpSystemErr() {
@@ -196,7 +197,7 @@ public class AppLogs {
@Override
public void write(int b) {
if (b == '\r' || b == '\n') {
String line = baos.toString();
String line = baos.toString(StandardCharsets.UTF_8);
if (line.length() == 0) {
return;
}
@@ -209,7 +210,7 @@ public class AppLogs {
baos.write(b);
}
}
}));
},true, StandardCharsets.UTF_8));
}
public void logException(String description, Throwable e) {
@@ -25,6 +25,8 @@ import lombok.Getter;
import lombok.SneakyThrows;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
@@ -129,6 +131,43 @@ public abstract class AppOperationMode {
});
}
// This is very important on Windows to not get into encoding issues
if (OsType.ofLocal() == OsType.WINDOWS) {
var con = System.console();
// If a program does not have console attached, it can't determine the code page
// This happens if the program is run non-interactively, e.g.
// $Script="$(& 'C:\Program Files\XPipe\xpipe.exe' terminal-launch --port ? --request ?)"
// If the output of this command contains special characters, the stdout will print in the system default encoding
// However, all non-interactive usages like this are done in an environment
// where XPipe or another environment sets the code page to UTF8
// Therefore, we have to override the stdout encoding if we don't have a console
if (con == null) {
var outField = FilterOutputStream.class.getDeclaredField("out");
outField.setAccessible(true);
// The original raw out stream is a private class
// designed to unblock carrier threads
// We want to keep this to retain all functionality
var printStreamOut = (BufferedOutputStream) outField.get(System.out);
var fdout = (FileOutputStream) outField.get(printStreamOut);
var newStdout = new PrintStream(new BufferedOutputStream(fdout, 128), true, StandardCharsets.UTF_8);
System.setProperty("stdout.encoding", "UTF-8");
System.setOut(newStdout);
// The original raw err stream is a private class
// designed to unblock carrier threads
// We want to keep this to retain all functionality
var printStreamErr = (BufferedOutputStream) outField.get(System.err);
var fderr = (FileOutputStream) outField.get(printStreamErr);
var newStderr = new PrintStream(new BufferedOutputStream(fderr, 128), true, StandardCharsets.UTF_8);
System.setProperty("stderr.encoding", "UTF-8");
System.setErr(newStderr);
// The stdin is a raw stream
System.setProperty("stdin.encoding", "UTF-8");
}
}
AppProperties.init(args);
if (AppProperties.get().isCli()) {
+1
View File
@@ -144,6 +144,7 @@ def getBaseJvmArgs() {
"--add-opens", "java.base/java.lang=$appPackage",
"--add-opens", "java.base/java.net=$appPackage",
"--add-opens", "java.base/java.nio.file=$appPackage",
"--add-opens", "java.base/java.io=$appPackage",
"--add-opens", "jdk.zipfs/jdk.nio.zipfs=io.xpipe.modulefs",
]
+2 -1
View File
@@ -1 +1,2 @@
- Fix debug output not showing all data messages from terminal sessions
- Fix encoding issue on some Windows systems causing connections launches to fail
- Fix debug output not showing all data messages from terminal sessions
+1 -1
View File
@@ -1 +1 @@
24.2.1-3
24.2.1