Separate standard output from standard error when running a program in Eclipse
I am debugging a Java program in Eclipse. This program normally writes binary 开发者_如何学Godata to its standard output, but it can write error messages and stack traces to its standard error stream.
I would like to redirect the binary data to a file, while continuing to show the standard error output in a console. This is trivial when running from the command line. Is it possible under Eclipse?
So far I have only figured out how to redirect both standard output and standard error to the same file. I cannot see a way to separate the two other than adding a new command-line option to the program.
Related: I/O redirection in Eclipse?
Can you try to redirect your stream stdout to a custom View with a SWT Text in it, leaving stderr to the default console.
See this thread
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
public class ConsumeSysout {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.NONE);
shell.open();
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
text.append(Character.toString((char) b));
}
};
System.setOut(new PrintStream(out));
System.out.println("Hello World!");
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
From (http://www.eclipsezone.com/eclipse/forums/t52910.html):
If someone would like to see the strings written to System.out/System.err by an already deployed plugin in Eclipse, then it can be done by running Eclipse in debug mode from command prompt as eclipse.exe -debug
.
Or else you can hack a little bit to capture the stderr/stdout of the JVM (running Eclipse) as described in Redirect stdout and stderr of Eclipse to a file (Eclipse 3.0 on WinXP)
If you run Eclipse in debug mode, you should be able to start it from a command line and include a stderr redirect. In Unix it would be "command 2> file".
This thread has some related content: Redirect STDERR / STDOUT of a process AFTER it's been started, using command line?
This link is more DOS/Windows specific: Stderr
The DOS shell, command.com, offers no mean to redirect stderr. Because of that, it's impossible to save to a file, or pipe into a program, the messages that a program prints to stderr. "foo >out" only redirects stdout of foo, not stderr. The latter still goes to the console.
Since stderr is typically where important error messages are printed, this is extremely unfortunate, as this makes us unable to detect errors in unattended jobs.
So what do you do? One solution is to switch to Unix, where the shell allows one to redirect stderr. With sh and its derivatives, use
foo >out 2>&1
to save both stdout and stderr of foo to out. Similarly, use
foo 2>&1 | bar
to pipe both stdout and stderr of foo into bar. If you're stuck with DOS, Stderr is what you need.
Stderr runs a command and merges its stderr into its stdout. Therefore, redirecting or piping stdout is equivalent to doing the same thing to stderr as well.
I'll have to keep looking for Windows solutions. PowerShell has become popular, and it might be worth checking out.
Well, you can replace System.out with your own PrintStream that writes out to a file. I'm just writing this off the cuff, so maybe it's not exactly perfectly right, but you get the idea:
FileOutputStream fos = new FileOutputStream("c:\myfile.bin");
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintStream ps = new PrintStream(bos);
System.out = ps;
That, or something very similar should do the trick. All output that normally goes to standard out will now go into that file (don't forget to close() the Printstream when you shut your program down, or it won't always flush the last of the data out to the file.)
Of course, you can also do the same trick with System.err if you want.
Two solutions come to mind right now for filtering stdout/stderr in an independent console (but they are not for binary streams, hence the Community-Wiki mode):
Either do not redirect anything (all outputs are in the console), and use a plugin like Grep Console to highlight relevant outputs
And/or redirect everything in a file, and use a plugin like NTail and use some "Filter lines" to exclude selected lines from log file display (that may be closer to what you are looking for).
(You can define a many a NTail views as you need/want.)
精彩评论