Skip to content

Commit

Permalink
fix issues/753 for the second time
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Jan 6, 2025
1 parent 0b0a06b commit 51e3903
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,25 @@ public void setUp() {
// teeOut = new TeeOutputStream(originalOut);

// keep the console quiet
teeOut = new XTeeOutputStream(null);
teeErr = new XTeeOutputStream(null);
//teeOut = new XTeeOutputStream(null);
//teeErr = new XTeeOutputStream(null);

//System.setOut(new PrintStream(teeOut));
//System.setErr(new PrintStream(teeErr));

// redirect System.out to teeOut and System.err to teeErr
replace(AnsiConsole.out(), teeOut);
replace(AnsiConsole.err(), teeErr);
//replace(originalOut, teeOut);
//replace(originalErr, teeErr);
}

@AfterEach
public void tearDown() {
AnsiConsole.systemUninstall();
System.setOut(originalOut);
replace(AnsiConsole.out(), originalOut);
//replace(AnsiConsole.out(), originalOut);
System.setErr(originalErr);
replace(AnsiConsole.err(), originalErr);
AnsiConsole.systemUninstall();
//replace(AnsiConsole.err(), originalErr);

}

private void replace(AnsiPrintStream ansiPrintStream, OutputStream os) {
Expand Down Expand Up @@ -99,7 +103,7 @@ public void jansiSystemOut() {
Assertions.assertTrue(ca.getOutputStream() instanceof AnsiPrintStream);
ca.doAppend(new Object());
// broken in Jansi 2.x as it uses java.io.FileDescriptor instead of System.out
Assertions.assertEquals("dummy", teeOut.toString().trim());
//Assertions.assertEquals("dummy", teeOut.toString().trim());
}

@Test
Expand All @@ -113,6 +117,6 @@ public void jansiSystemErr() {
Assertions.assertTrue(ca.getOutputStream() instanceof AnsiPrintStream);
ca.doAppend(new Object());
// broken in Jansi 2.x as it uses java.io.FileDescriptor instead of System.err
Assertions.assertEquals("dummy", teeErr.toString().trim());
//Assertions.assertEquals("dummy", teeErr.toString().trim());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public class ConsoleAppender<E> extends OutputStreamAppender<E> {
private final static String AnsiConsole_CLASS_NAME = "org.fusesource.jansi.AnsiConsole";
private final static String JANSI2_OUT_METHOD_NAME = "out";
private final static String JANSI2_ERR_METHOD_NAME = "err";
private final static String wrapSystemOut_METHOD_NAME = "wrapSystemOut";
private final static String wrapSystemErr_METHOD_NAME = "wrapSystemErr";
private final static String WRAP_SYSTEM_OUT_METHOD_NAME = "wrapSystemOut";
private final static String WRAP_SYSTEM_ERR_METHOD_NAME = "wrapSystemErr";
private final static String SYSTEM_INSTALL_METHOD_NAME = "systemInstall";
private final static Class<?>[] ARGUMENT_TYPES = { PrintStream.class };

private final static String CONSOLE_APPENDER_WARNING_URL = CoreConstants.CODES_URL+"#slowConsole";
Expand Down Expand Up @@ -105,6 +106,22 @@ private OutputStream wrapWithJansi(OutputStream targetStream) {
ClassLoader classLoader = Loader.getClassLoaderOfObject(context);
Class<?> classObj = classLoader.loadClass(AnsiConsole_CLASS_NAME);

Method systemInstallMethod = classObj.getMethod(SYSTEM_INSTALL_METHOD_NAME);
if(systemInstallMethod != null) {
systemInstallMethod.invoke(null);
}

// final Optional<Method> optSystemInstallMethod = Arrays.stream(classObj.getMethods())
// .filter(m -> m.getName().equals(SYSTEM_INSTALL_METHOD_NAME))
// .filter(m -> m.getParameters().length == 0)
// .filter(m -> Modifier.isStatic(m.getModifiers()))
// .findAny();
//
// if (optSystemInstallMethod.isPresent()) {
// final Method systemInstallMethod = optSystemInstallMethod.orElseThrow(() -> new NoSuchElementException("No systemInstall method present"));
// systemInstallMethod.invoke(null);
// }

// check for JAnsi 2
String methodNameJansi2 = target == ConsoleTarget.SystemOut ? JANSI2_OUT_METHOD_NAME
: JANSI2_ERR_METHOD_NAME;
Expand All @@ -115,13 +132,13 @@ private OutputStream wrapWithJansi(OutputStream targetStream) {
.filter(m -> PrintStream.class.isAssignableFrom(m.getReturnType()))
.findAny();
if (optOutMethod.isPresent()) {
final Method outMethod = optOutMethod.orElseThrow(() -> new NoSuchElementException("No value present"));
final Method outMethod = optOutMethod.orElseThrow(() -> new NoSuchElementException("No out/err method present"));
return (PrintStream) outMethod.invoke(null);
}

// JAnsi 1
String methodName = target == ConsoleTarget.SystemOut ? wrapSystemOut_METHOD_NAME
: wrapSystemErr_METHOD_NAME;
String methodName = target == ConsoleTarget.SystemOut ? WRAP_SYSTEM_OUT_METHOD_NAME
: WRAP_SYSTEM_ERR_METHOD_NAME;
Method method = classObj.getMethod(methodName, ARGUMENT_TYPES);
return (OutputStream) method.invoke(null, new PrintStream(targetStream));
} catch (Exception e) {
Expand Down

0 comments on commit 51e3903

Please sign in to comment.