perl - redirecting warning messages to text file

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
Right now I am running a perl script with the -w flag to generate warnings. I am also redirecting the program's output to a text file like so:

test_script.pl > test_output.txt

I am doing this because there is a lot of debug code in my script and the console cannot hold all of it, yet I need to be able to see it all to understand what is going on in my script.

The problem I am having is that warning messages are still going to the console, but not the text file that I redirected the output to. All other program output is going to the text file. How can I redirect the warning messages to the text file as well (and why are they not going there to begin with)? It is impossible for me to debug the program when I cannot see where these warning messages are occurring relative to all of the other debug code in my script.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
There are two output streams for any console program, stdout and stderr. By default both go to the console. The > operator only redirects stdout, leaving stderr pointing to the console still. How you redirect stderr to the same place as stdout depends on what shell you're using but for sh and friends it's like so:

test_script.pl > test_output.txt 2>&1

"2>" redirects stderr and sends it to "&1", which is stdout. I believe (but haven't tested) that this would be functionally equivalent:

test_script.pl 2> test_output.txt 1>&2
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
I was able to do it using the following:

test_script.pl >& test_output.txt

This redirected both the stdout and stderr to the text file. When I tried yours, it said "ambiguous output redirection", although it may have been the shell I was using.
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
Once I got the redirection to work, another issue I am now having is that the warning messages don't seem to be appearing in the correct location in the output text file.

In some instances, all of the warning messages will be listed at the top of the text file, one per line, even though the actual event that triggered the warning didn't occur until late in the script.

In most other instances, the warning messages just seem to be almost randomly dispersed throughout the rest of the text file. They will even cut into the text coming from stdout. For example, if I had a line of debug code that was supposed to write the following to the text file:

"This is a line of debug code" (without the quotes)

then when I redirect stderr to the text file, it may look like this:

This is a linWARNING MESSAGE BLAH BLAH BLAH BLAHe of debug code

So the stderr output messages don't seem to be aware of the formatting of the stdout messages.

Is there any way to fix that?

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
My guess would be it's because both streams are buffered and they're getting flushed at different times. They're also obviously not getting flushed at line boundaries. Note the below code. It prints out what you'd expect but when you remove the fflush() calls, it gets unpredictable. Actually, on my system, all the stderr comes out before any of the stdout, but I don't think that's defined behaviour and probably depends on buffer size and such.

#include <stdio.h>
int main() {
int i;
for (i = 0; i < 100; i++) {
fprintf(stderr, "STDERR STDERR STDERR STDERR\n");
fflush(stderr);
fprintf(stdout, "stdout stdout stdout stdout\n");
fflush(stdout);
}
return 0;
}

I'm in no position to advise you on perl programming, but maybe try this: http://www.thescripts.com/forum/thread603712.html