Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need Help, redirecting STDERR && STDOUT to same file.

JoeMcJoeMc Posts: 55Member
Hi All,

I'm working on a script where I need to collect errors from stderr and collect conditional stdout messages containing the line numbers they occur on, all the while printing both collected data groups to the same log file.
The idea is if I can get the line numbers printed to the log file using the stdout mixed inbetween possible stderr error's, I could parse the log file and get a good guess on what line number the error happened on, as well as some other data I'll print to stdout through variables used in my script. I'm trying to do this now, but keep running into walls.
Any help would be much appreciated. :-)

By the way this code is to be used on a Win32 system.

What I'm trying:

[code][color=Blue]sub redirect_all_stderr_to_file {

my $logfile = shift;

# save STDERR off
open(SAVERR, ">&STDERR") or die "Failed to dup STDERR: $!";

# redirect STDERR to log file.
open(STDERR, ">", "$logfile") or die "Failed to re-direct STDERR to '$logfile': $!";

# create handle to print messages to that writes to log file.
my $log = IO::File->new(">>$logfile") or die "Failed to duplicate STDERR: $!";

$log->autoflush(1);

# returns filehandle.
return $log;
}[/color][/code]

Then through out code having $log as a global handle,
I can run the following command:
[code][color=Blue]$lnum = __LINE__;
$message = "hello this is my message.";
$log->print("${lnum}|${message}
");[/color][/code]

Any help is much appreciated.

Regards,
JoeMc

Comments

  • rlambert7rlambert7 Posts: 2Member
    Did you ever solve this problem? I want to do that, too. My first attempt was:

    open (FH, ">stdout_stderr_log.txt"); # Zero-out log file
    close (FH);
    open (STDERR, ">>stdout_stderr_log.txt");
    open (STDOUT, ">>stdout_stderr_log.txt");

    but all of the STDOUT stuff appears first in the logfile followed by all of the STDERR stuff, or vis versa, making it pretty much worthless :(

    So, what I am doing currently is:

    open(STDERR, ">stdout_stderr_log.txt");

    Then, wherever I would have done a
    print "blah-blah-blah
    ";
    I do a
    print STDERR "blah-blah-blah
    ";

    So, if you had code like:

    unlink("x_x"); Make sure file "x_x" does not exist.
    print STDERR "Does x_x exist?
    ";
    open(FH, "<x_x") or warn "File x_x does not exist.
    ";
    print STDERR "See, x_x does not exist.
    ";

    the logfile would look like:

    Does x_x exist?
    File x_x does not exist.
    See, x_x does not exist.

    Works, but quite a kludge.
  • JonathanJonathan Posts: 2,914Member
    : Did you ever solve this problem? I want to do that, too. My first
    : attempt was:
    :
    : open (FH, ">stdout_stderr_log.txt"); # Zero-out log file
    : close (FH);
    : open (STDERR, ">>stdout_stderr_log.txt");
    : open (STDOUT, ">>stdout_stderr_log.txt");
    :
    : but all of the STDOUT stuff appears first in the logfile followed by
    : all of the STDERR stuff, or vis versa, making it pretty much
    : worthless :(
    Suspect it's a buffering issue - Perl does buffering to try and improve I/O performance (which is a good thing normally but gets in the way in situations like this).

    Try at the top of your program doing:

    $| = 1;

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • rlambert7rlambert7 Posts: 2Member

    : Try at the top of your program doing:
    :
    : $| = 1;
    :
    : Jonathan


    Yes, that works! Thanks. Sorry I didn't respond sooner. It says I'm supposed to receive email notices when there were new posts in this thread. I never got one :( But, thanks again. :)

    Richard

Sign In or Register to comment.