Public Sub RedirectFilteredLogs
Dim system As JavaObject
system.InitializeStatic("java.lang.System")
' Capture original System.out
Dim originalOut As JavaObject = system.GetField("out")
' Initialize the custom PrintStreamWrapper
Dim filteredStream As JavaObject
filteredStream.InitializeNewInstance("PrintStreamWrapper", Array(originalOut, Me))
system.RunMethod("setOut", Array(filteredStream)) ' Redirect standard output
system.RunMethod("setErr", Array(filteredStream)) ' Redirect error output
End Sub
Public Sub ProcessLogMessage(message As String)
If message.Contains("DEBUG") = False Then 'expand as necessary
File.WriteString(File.DirApp, "filtered_logs.txt", message & Chr(10))
CallSubDelayed2(Me, "ProcessLogMessageInternal", message)
End If
End Sub
Public Sub ProcessLogMessageInternal(message As String)
Dim system As JavaObject
system.InitializeStatic("java.lang.System")
Dim originalOut As JavaObject = system.GetField("out")
originalOut.RunMethod("println", Array(message))
End Sub
#If Java
import java.io.PrintStream;
import java.lang.reflect.Method;
public class PrintStreamWrapper extends PrintStream {
private Object b4jModule;
public PrintStreamWrapper(PrintStream original, Object b4jModule) {
super(original);
this.b4jModule = b4jModule;
}
@Override
public void println(String message) {
try {
if (b4jModule != null) {
Method method = b4jModule.getClass().getMethod("ProcessLogMessage", String.class);
method.invoke(b4jModule, message);
} else {
System.out.println("Error: b4jModule is null.");
}
} catch (Exception e) {
System.out.println("Exception calling ProcessLogMessage: " + e.getMessage());
}
}
}
#End If