Skip to content

Instantly share code, notes, and snippets.

@iromu
Last active November 23, 2022 13:11
Show Gist options
  • Save iromu/db8836d338122e7b800a68b36981e929 to your computer and use it in GitHub Desktop.
Save iromu/db8836d338122e7b800a68b36981e929 to your computer and use it in GitHub Desktop.
import org.jetbrains.annotations.Nullable;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class PackagePrintStream extends PrintStream {
public PackagePrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException {
super(out, autoFlush, encoding);
}
@Override
public void println(@Nullable String x) {
if (allow()) {
super.println(x);
}
}
private boolean allow() {
String cn = inferCaller().getClassName();
return !cn.startsWith("org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry$FunctionInvocationWrapper");
}
private boolean isPrintImplFrame(String cname) {
return PackagePrintStream.class.getName().equals(cname);
}
private StackTraceElement inferCaller() {
StackTraceElement stack[] = (new Throwable()).getStackTrace();
int ix = 0;
while (ix < stack.length) {
StackTraceElement frame = stack[ix];
String cname = frame.getClassName();
if (isPrintImplFrame(cname)) {
break;
}
ix++;
}
while (ix < stack.length) {
StackTraceElement frame = stack[ix];
String cname = frame.getClassName();
if (!isPrintImplFrame(cname)) {
return frame;
}
ix++;
}
return new StackTraceElement(PackagePrintStream.class.getName(), "write",
PackagePrintStream.class.getName(), -1);
}
}
// https://github.com/spring-cloud/spring-cloud-function/issues/962
static {
try {
System.setOut(new PackagePrintStream(System.out, true, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment