redirecting stderr and stdout

Discussion in 'Perl' started by Jon Landenburer, May 12, 2004.

  1. It seems that no matter how thorough our error checking gets we always
    hit a new error not expected. SO I decided trying to ty STDERR, STOUT
    and a logfile all together.

    This seems to work. Even sytem errors or DBI erros get caught and
    resirect. Out problem is htat the order of the statements written to
    the output file does not match the order in the code. SO we we tried
    flushing all file handlers. to no avail

    Code
    #!/bin/perl
    use DBI;

    open (STDOUT, ">dog") or "die cannot open STDOUT to dog\n";
    print "begin\n";
    close STDOUT;
    open (STDOUT, ">>dog") or die "cannot open STDOUT to dog\n";
    $| = 1;
    open (STDERR, ">>dog") or die "cannot open STDERR to dog\n";
    $| = 1;
    open (LOG, ">>dog") or die "cannot opent sonnabinny\n";
    $| = 1;

    print LOG "logme ", $cnt++, "\n";
    print STDERR "logerr ", $cnt++, "\n";
    print STDOUT "logout ", $cnt++, "\n";
    print LOG "logme ", $cnt++, "\n";
    print STDERR "logerr ", $cnt++, "\n";
    print STDOUT "logout ", $cnt++, "\n";
    print LOG "logme ", $cnt++, "\n";
    print STDERR "logerr ", $cnt++, "\n";
    print STDOUT "logout ", $cnt++, "\n";
    print LOG "logme ", $cnt++, "\n";
    print STDERR "logerr ", $cnt++, "\n";
    print STDOUT "logout ", $cnt++, "\n";
    print LOG "logme ", $cnt++, "\n";
    print STDERR "logerr ", $cnt++, "\n";
    print STDOUT "logout ", $cnt++, "\n";
    print LOG "logme ", $cnt++, "\n";
    print STDERR "logerr ", $cnt++, "\n";
    print STDOUT "logout ", $cnt++, "\n";

    @a = `cp sdjkfhjkh sdfsdf`;
    system ("cp sdsdsdsdsd sdsdsdsdd");

    $database = $ENV{ORACLE_SID};
    $dbh = DBI->connect("dbi:Oracle:$database", "gueST_ID",
    "GUEST_PWD");


    Output
    :!cat dog
    begin
    logerr 1
    logout 2
    logerr 4
    logout 5
    logerr 7
    logout 8
    logerr 10
    logout 11
    logerr 13
    logout 14
    logerr 16
    logout 17
    cp: cannot access sdjkfhjkh
    cp: cannot access sdsdsdsdsd
    DBI->connect(COMSYST) failed: ORA-01017: invalid username/password;
    logon denied (DBD ERROR: OCISessionBegin) at a.pl line 37
    logme 0
    logme 3
    logme 6
    logme 9
    logme 12
    logme 15

    I would have expected all of the lines to be written in the order of
    the number $cnt
    see how everthing to the LOG file handler comes at the end?
    Is there a way of avoiding this?
     
    Jon Landenburer, May 12, 2004
    #1
    1. Advertising

  2. Jon Landenburer

    Joe Smith Guest

    Jon Landenburer wrote:

    > open (STDOUT, ">dog") or "die cannot open STDOUT to dog\n";
    > print "begin\n";
    > close STDOUT;
    > open (STDOUT, ">>dog") or die "cannot open STDOUT to dog\n";
    > $| = 1;
    > open (STDERR, ">>dog") or die "cannot open STDERR to dog\n";
    > $| = 1;
    > open (LOG, ">>dog") or die "cannot opent sonnabinny\n";
    > $| = 1;


    Get rid of the three lines that set $| and replace them with
    select LOG; $| = 1;
    select STDERR; $| = 1;
    select STDOUT; $| = 1;

    Just be sure to have STDOUT selected at the end.

    Another way to do it is:

    open STDOUT, ">>", "dog" or die "cannot append to 'dog': $!\n";
    open STDERR, ">&STDOUT" or die "cannot dup STDERR to STDOUT: $!\n";
    select STDERR; $| = 1;
    open LOG, ">&STDOUT" or die "cannot dup LOG to STDOUT: $!\n";
    select LOG; $| = 1;
    select STDOUT; $| = 1;

    -Joe

    P.S. Next time, look at "perldoc -f open" first, and post to
    comp.lang.perl.misc instead of comp.lang.perl .
     
    Joe Smith, May 13, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Knop
    Replies:
    4
    Views:
    1,054
    Jan knob
    Nov 24, 2003
  2. Michael McGarry
    Replies:
    1
    Views:
    609
    Michael Fuhr
    Dec 16, 2004
  3. Lincoln Yeoh

    Redirecting stderr and stdout to syslog

    Lincoln Yeoh, Aug 5, 2008, in forum: Python
    Replies:
    2
    Views:
    2,131
    Lincoln Yeoh
    Aug 6, 2008
  4. Ian Hobson
    Replies:
    0
    Views:
    659
    Ian Hobson
    Sep 6, 2010
  5. it_says_BALLS_on_your forehead
    Replies:
    2
    Views:
    270
    Joe Smith
    Jan 10, 2006
Loading...

Share This Page