I need to monitor console output in Java, I have tried several different ways of retrieving the output as it is streamed, but I fell into a few pitfalls (looping the same output, only one part of output being captured, losing output) and have decided to go about it a different way.
I am developing a Java plugin for a server of the popular game, Minecraft, and I need to be able to monitor console output from other plugins.
I think a good way to do this is by redirecting console output to file, and then set up a recurring async task that checks the file, carries out anything it needs to do, and then clears the file for more input. I think I can do this with a simple use of System.setOut(PrintStream stream); and one of the many guides I can find on Google.
But there is a problem, and that is why I am asking here today. I need console output to stay on the console like normal, hence "mirroring". I cannot edit any of the plugins to output somewhere else, it needs to be from what my Java plugin can do and that only. Sure, maybe on every scheduled check on the file I could reprint it all back to the console but that would result in blocks being printed at a time, which would not be ideal. I do hope this is possible. Thank you!
EDIT: I don't think I explained fully. "plugins"'s classes are run by the program that I am developing for. I cannot change how classes are run and I cannot change how other plugins print to console.
I also do not want to direct console output to files for logging, that idea is only to keep it in a temporary place while it is parsed. Ideally, I need to pass each line of console output to a function (parseString(String line)
) which carries out operations depending on the contents of the line. Also, I don't understand how I can read stream contents line-by-line properly, so if someone has any idea on how to, please let me know. :)
问题:
回答1:
Capture System.out before changing it, and use TeeOutputStream:
OutputStream myCaptureStream = ...;
PrintStream original = System.out;
OutptStream outputtee = new TeeOutputStream(originalOut, myCaptureStream);
PrintStream printTee = new PrintStream(outputTee);
System.setOut(printTee);
Convert output stream (myCaptureStream) to an input stream
- Use Java standard library PipedOutputStream:
new PipedOutputStream
; read carefully - you'll need your reader running on another thread. - Convert the input stream to a Reader,
- and that to a BufferedReader.
BufferedReader gives you a readLine
method.
Pseudo code:
// Feed myCaptureStream to TeeOutputStream
OutputStream myCaptureStream = new PipedOutputStream();
// Prepare to capture data being written to that output stream
InputStream myCaptureAsInputStream = new PipedInputStream(myCaptureStream);
Reader myCaptureReader = new InputStreamReader(myCaptureAsInputStream);
BufferedReader myCaptureBuffered = new BufferedReader(myCaptureReader, 1024);
// This must run on separate reader thread; in spin loop:
myCaptureBuffer.readLine
回答2:
You can change the System.out and System.err to be references to your own custom PrintStreams. This solution is cross platform, unlike using a platform specific helper executable such as tee
.
Documentation for System.setOut(PrintStream)
You can write your own PrintStream Subclass that looks something like:
class MirroringPrintStream extends PrintStream
{
PrintStream first, second;
public MirroringPrintStream(PrintStream first, PrintStream second)
{
// fail now rather than later
if (first == null || second == null) throw new NullPointerException();
this.first = first;
this.second = second;
}
@override
// methods go here (I do believe there are rather a lot of them, but they will all look the same)
}
You can then use System.setOut(new MirroringPrintStream(System.out, myLogStream))
where myLogStream
is the PrintStream you opened to your log file. I'm not sure how this will handle appending to a file that something else periodically truncates, you might want to experiment with that a bit.
All else fails, go to irc.esper.net and ask about it in #risucraft
回答3:
Assuming this is a friendly takeover of console output, why not log to your own stream and mirror that to the console?