Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > redirecting stderr and stdout

Reply
Thread Tools

redirecting stderr and stdout

 
 
Jon Landenburer
Guest
Posts: n/a
 
      05-12-2004
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?
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      05-13-2004
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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting stdout and stderr for a windows service Ian Hobson Python 0 09-06-2010 02:29 PM
Redirecting stderr and stdout to syslog Lincoln Yeoh Python 2 08-06-2008 01:44 AM
script hangs when run from command line and redirecting stdout and stderr to file it_says_BALLS_on_your forehead Perl Misc 2 01-10-2006 11:31 AM
Redirecting stdin, stdout, and stderr to a window Michael McGarry Python 1 12-16-2004 11:29 PM
Redirecting Python stdout ,stderr and stdin Jan Knop Python 4 11-24-2003 10:14 AM



Advertisments