For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2006 > STDOUT and STDERR to same file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author STDOUT and STDERR to same file
Ken Foskey

2006-08-29, 9:57 pm

I have a daemon process that works but I am currently running it with

script.pl > error.log 2>&1

and I want to do the same thing without using the redirection,, remove
the human error when starting the program.

I can `open( STDERR, '>', 'error.log') ...` but is there a piece of
magic to duplicate that to STDOUT as well (ie same file output)

Ta
Ken Foskey

kens

2006-08-29, 9:57 pm


Ken Foskey wrote:
> I have a daemon process that works but I am currently running it with
>
> script.pl > error.log 2>&1
>
> and I want to do the same thing without using the redirection,, remove
> the human error when starting the program.
>
> I can `open( STDERR, '>', 'error.log') ...` but is there a piece of
> magic to duplicate that to STDOUT as well (ie same file output)
>
> Ta
> Ken Foskey


Look in the FAQ on duplicating a filehandle.

http://faq.perl.org/perlfaq5.html#How_do_I_dup_a_fileh

Ken

usenet@DavidFilmer.com

2006-08-29, 9:57 pm

Ken Foskey wrote:
> magic to duplicate that to STDOUT as well (ie same file output)


Yeah, check it out:

*STDERR = *STDOUT;

Now everything that goes to STDERR will go to STDOUT (which you can
redirect to a file or whatever). This is a great debugging tool that's
easy to do and un-do; you can do this sort of thing with any
filehandle. Perl is .

--
David Filmer (http://DavidFilmer.com)

Tom Phoenix

2006-08-29, 9:57 pm

On 8/29/06, Ken Foskey <foskey@optushome.com.au> wrote:

> I can `open( STDERR, '>', 'error.log') ...` but is there a piece of
> magic to duplicate that to STDOUT as well (ie same file output)


open(STDERR, '>', 'error.log') or die "Can't redirect stderr: $!";
open(STDOUT, ">&STDERR") or die "Can't redirect stdout: $!";

You should also be aware that STDOUT is normally buffered, while
STDERR is not; if you don't do anything about buffering, this may
result in an error message appearing unexpectedly early in the output.
That is, if your program dies partway through execution, the dying
words won't necessarily be the last thing in the file; part of a
buffer of ordinary output may follow.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Mumia W.

2006-08-29, 9:57 pm

On 08/29/2006 09:06 AM, Ken Foskey wrote:
> I have a daemon process that works but I am currently running it with
>
> script.pl > error.log 2>&1
>
> and I want to do the same thing without using the redirection,, remove
> the human error when starting the program.
>
> I can `open( STDERR, '>', 'error.log') ...` but is there a piece of
> magic to duplicate that to STDOUT as well (ie same file output)
>
> Ta
> Ken Foskey
>
>


"Perldoc -f open" shows you how to create duplicate file
handles, e.g.:

use strict;
use warnings;
open (FH, '>out') or die("Failure:$!\n");
open (STDOUT, '>&FH') or die ("Dup failure: $!\n");
open (STDERR, '>&FH') or die ("Dup failure: $!\n");
print "STDOUT text is here.\n";
print STDERR "This might be an ERROR message.\n";
close (FH);

__END__

HTH


Muttley Meen

2006-08-29, 9:57 pm

On 8/29/06, Ken Foskey <foskey@optushome.com.au> wrote:
> I have a daemon process that works but I am currently running it with
>
> script.pl > error.log 2>&1
>
> and I want to do the same thing without using the redirection,, remove
> the human error when starting the program.
>
> I can `open( STDERR, '>', 'error.log') ...` but is there a piece of
> magic to duplicate that to STDOUT as well (ie same file output)
>
> Ta
> Ken Foskey
>
>

From command line, you could use tee( man 1 tee).
After a quick search, I found a perl module that presumably does the
same thing:

http://72.14.221.104/search?q=cache...&client=firefox

Hope this helps.
Jeff Pang

2006-08-29, 9:57 pm

Hello,

In your daemon script,you can re-direct the STDIN/STDOUT/STDERR to the null device like '/dev/null'.

open (STDIN, "</dev/null");
open (STDOUT, ">/dev/null");
open (STDERR,">&STDOUT");

Then you can redefine the 'warn' and 'die' handler to the subroutines written by yourself.In these subroutines,you log all events to files.


$SIG{__DIE__}=\&log_die;
$SIG{__WARN__}=\&log_warn;


sub log_die
{
my $time=scalar localtime;
open (HDW,">>",$err_log);
print HDW $time," ",@_;
close HDW;
die @_;
}

sub log_warn
{
my $time=scalar localtime;
open (HDW,">>",$err_log);
print HDW $time," ",@_;
close HDW;
}


Hope this helps.

-----Original Message-----
>From: Ken Foskey <foskey@optushome.com.au>
>Sent: Aug 29, 2006 10:06 AM
>To: Beginners List <beginners@perl.org>
>Subject: STDOUT and STDERR to same file
>
>I have a daemon process that works but I am currently running it with
>
> script.pl > error.log 2>&1
>
>and I want to do the same thing without using the redirection,, remove
>the human error when starting the program.
>
>I can `open( STDERR, '>', 'error.log') ...` but is there a piece of
>magic to duplicate that to STDOUT as well (ie same file output)
>
>Ta
>Ken Foskey
>
>
>--
>To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>For additional commands, e-mail: beginners-help@perl.org
><http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>



--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com