Is there some way I can catch when a method generates a write to STDERR? I'm trying to pre-pend some information with each STDERR write.
let's say our method is:
parser.thatGeneratesSTDERR();
it'll generate something like
line 37:29 no viable alternative at character 'a'
can I just wrap it with something that catches the stderr to be written and pre-pend some info so it'll look like this instead:
File: mybadfile.txt -- line 37:29 no viable alternative at character 'a'
I know I could probably dig through the actual code that does the STDERR writing but I really don't want to do that -- I'd rather just wrap some code around this method instead.
I should note that this method doesn't always generate STDERR -- the reason I'm trying to catch them is that it SHOULDN'T be generating any STDERR -- but since this is called on quite a few files (over 40) I never know which file is generating the error message -- it takes forever to find.
UPDATE so yeh, we can change our STDERR -- I was just forgetting that I need to override my println in my new STDERR... here's the relevant code:
public static String errfile = "";
static {
final PrintStream currentErr = System.err;
PrintStream newErr = new PrintStream(currentErr)
{
public void println(String string) {
print("File: " + errfile + " --");
print(string);
super.println();
}
};
System.setErr(newErr);
}
then for each file that I'm testing all I have to do is change errfile to the filename and my output comes out expected...
thanks a lot!