Re: redirecting STDERR with IO::Tee

2009-06-25 Thread Jenda Krynicky
Date sent:  Mon, 22 Jun 2009 10:53:06 -0700
From:   pa...@compugenic.com
To: beginners@perl.org
Subject:redirecting STDERR with IO::Tee

 I have a script which runs mostly via a cron job and sometimes
 interactively.  I would like STDERR to automatically print to both the
 console and to a logfile simultaneously. 
 
 Right now I've gotten as far as merging both file handles with IO::Tee
 but I'm not sure if I'm heading down the right path. 

 my $merged_stderr = IO::Tee-new( \*STDERR, new IO::File($errlog) );
 
 This only works when explicitely naming the file handle in a print
 statement.  How can I take it to the next step, which is to have
 STDERR automatically print to that file handle? 

#!perl
use strict;
use IO::Tee;

warn Before the change;
open my $oldSTDERR, 'STDERR' or die;
close STDERR;
open my $DBG, '', 'debug.log' or die;
*STDERR = IO::Tee-new( $oldSTDERR, $DBG) or die;

warn after the change;

print to STDOUT\n;
print STDERR to STDERR\n;

system ('dir sdf_sgerdfwerg');

die Bleargh;
__END__

it's not possible to tee (this way) the STDERR of the child 
processes, the best you can do is to decide whether you want them to 
go to the file or the ordinary error output. Try to escape the close 
STDERR;.

HTH, Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: redirecting STDERR with IO::Tee

2009-06-24 Thread Jeff Pang
2009/6/23  pa...@compugenic.com:
 I have a script which runs mostly via a cron job and sometimes interactively. 
  I would like STDERR to automatically print to both the console and to a 
 logfile simultaneously.

 Right now I've gotten as far as merging both file handles with IO::Tee but 
 I'm not sure if I'm heading down the right path.
 my $merged_stderr = IO::Tee-new( \*STDERR, new IO::File($errlog) );

 This only works when explicitely naming the file handle in a print statement. 
  How can I take it to the next step, which is to have STDERR automatically 
 print to that file handle?



Hi,

What causes writting of STDERR?
If it's due to die and warn, you could redirect them to the customized routines.
Something like:


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

sub log_die
{
my $time=scalar localtime;
open (HDW,,$err_log);
my $old=select HDW;$|=1;select $old;
print HDW $time,  ,@_;
close HDW;
die @_;
}

sub log_warn
{
my $time=scalar localtime;
open (HDW,,$err_log);
my $old=select HDW;$|=1;select $old;
print HDW $time,  ,@_;
close HDW;
}

-- 
In this magical land, everywhere
is in full bloom with flowers of evil.
 - Jeff Pang (CN)

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: redirecting STDERR with IO::Tee

2009-06-24 Thread pablo
On Wed, Jun 24, 2009 at 05:52:36PM +0800, Jeff Pang wrote:
 2009/6/23  pa...@compugenic.com:
  I have a script which runs mostly via a cron job and sometimes 
  interactively.  I would like STDERR to automatically print to both the 
  console and to a logfile simultaneously.
snip

 
 Hi,
 
 What causes writting of STDERR?
 If it's due to die and warn, you could redirect them to the customized 
 routines.
 Something like:
 
 
 $SIG{__DIE__}=\log_die;
 $SIG{__WARN__}=\log_warn;
 
snip

I would but this doesn't properly redirect other errors or warnings as
with system().  I actually created a new post due to lack of response to
this one, the new post is How to print errors to both STDERR  a
file?.

Basically I think if this was STDOUT, I'd use IO::Tee to merge STDOUT 
a log file and use the new filehandle in a 'select' statement to set it
as the default.  But because this is STDERR I am not sure if this is
possible with Perl.

My goal is to learn something new about Perl and do this the right way,
as this task will be quite common for me.  There are a lot of script I
run via cron jobs which I periodically run manually on the console.

Any suggestions would be appreciated.


Pablo

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Redirecting STDERR

2005-10-07 Thread Timothy Johnson
It partly depends on your operating system, I think.  That's fine on
Windows, but on UNIX I think that under some if not all circumstances
your change will persist after the script finishes executing.  I've
never had to do it, but I've seen one method that goes like this:

open(OLD_STDERR,STDERR) or die Failed to save STDERR;
open(STDERR,script.err) or die Failed to redirect STDERR;

do something...

open(STDERR,OLD_STDERR) or die Failed to restore STDERR;


-Original Message-
From: Ryan Frantz [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 07, 2005 12:10 PM
To: beginners@perl.org
Subject: Redirecting STDERR

Perlers,

I have a script where I redirect STDERR to a file so that I can capture
'die' messages like so:

use warnings;
use strict;

my $logfile = /some/path/logfile.txt;

open STDERR, $logfile;

something or die Unable to do something()\n;

close STDERR;

Is it kosher to do this?  Or is there a more preferred method to
redirect 'die' messages?



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Redirecting STDERR

2005-10-07 Thread Jeff Pan
hi,
maybe u should redefined the __DIE__ handle for the reason of safety.
such as:

$SIG{__DIE__}=\log_die;
sub log_die
{
open (HDW,,$err_log);
select HDW;$|=1;select STDOUT;
print HDW @_;
close HDW;
die @_;
}


2005/10/8, Timothy Johnson [EMAIL PROTECTED]:
 It partly depends on your operating system, I think.  That's fine on
 Windows, but on UNIX I think that under some if not all circumstances
 your change will persist after the script finishes executing.  I've
 never had to do it, but I've seen one method that goes like this:

 open(OLD_STDERR,STDERR) or die Failed to save STDERR;
 open(STDERR,script.err) or die Failed to redirect STDERR;

 do something...

 open(STDERR,OLD_STDERR) or die Failed to restore STDERR;


 -Original Message-
 From: Ryan Frantz [mailto:[EMAIL PROTECTED]
 Sent: Friday, October 07, 2005 12:10 PM
 To: beginners@perl.org
 Subject: Redirecting STDERR

 Perlers,

 I have a script where I redirect STDERR to a file so that I can capture
 'die' messages like so:

 use warnings;
 use strict;

 my $logfile = /some/path/logfile.txt;

 open STDERR, $logfile;

 something or die Unable to do something()\n;

 close STDERR;

 Is it kosher to do this?  Or is there a more preferred method to
 redirect 'die' messages?



 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Redirecting STDERR

2005-10-07 Thread Stephen Kratzer
Opening the filehandle STDERR to a file will not persist beyond termination of 
the script, nor will it affect other programs running at the same time as the 
script. It affects only the running script. STDERR of the shell is different 
from that of perl.

On Friday 07 October 2005 16:56, Timothy Johnson wrote:
 It partly depends on your operating system, I think.  That's fine on
 Windows, but on UNIX I think that under some if not all circumstances
 your change will persist after the script finishes executing.  I've
 never had to do it, but I've seen one method that goes like this:

 open(OLD_STDERR,STDERR) or die Failed to save STDERR;
 open(STDERR,script.err) or die Failed to redirect STDERR;

 do something...

 open(STDERR,OLD_STDERR) or die Failed to restore STDERR;


 -Original Message-
 From: Ryan Frantz [mailto:[EMAIL PROTECTED]
 Sent: Friday, October 07, 2005 12:10 PM
 To: beginners@perl.org
 Subject: Redirecting STDERR

 Perlers,

 I have a script where I redirect STDERR to a file so that I can capture
 'die' messages like so:

 use warnings;
 use strict;

 my $logfile = /some/path/logfile.txt;

 open STDERR, $logfile;

 something or die Unable to do something()\n;

 close STDERR;

 Is it kosher to do this?  Or is there a more preferred method to
 redirect 'die' messages?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Redirecting STDERR to 2 locations simultaneously

2001-11-13 Thread Crowder, Rod

Try the Module IO::Tee, I haven't used it, but it's description sounds to be
what you need



 -Original Message-
 From: Peter Scott [mailto:[EMAIL PROTECTED]]
 Sent: 13 November 2001 07:01
 To: [EMAIL PROTECTED]
 Subject: Re: Redirecting STDERR to 2 locations simultaneously
 
 
 
 I have been trying to figure out if it possible to redirect 
 STDERR to 2 
 locations at once.  Specifically, I want certain errors to 
 be redirected 
 to both STDOUT and a log file.  I have been unable to do 
 this, and am not 
 even sure it is possible.
 
 Redirecting to one or the other is now problem, but a 
 solution that allows 
 me to redirect to both simultaneously remains elusive.  Any 
 thoughts or 
 suggestions would be appreciated.  Thanks
 
 If you're on Unix, look up the 'tee' command.
 --
 Peter Scott
 Pacific Systems Design Technologies
 http://www.perldebugged.com
 
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Redirecting STDERR to 2 locations simultaneously

2001-11-12 Thread Peter Scott


I have been trying to figure out if it possible to redirect STDERR to 2 
locations at once.  Specifically, I want certain errors to be redirected 
to both STDOUT and a log file.  I have been unable to do this, and am not 
even sure it is possible.

Redirecting to one or the other is now problem, but a solution that allows 
me to redirect to both simultaneously remains elusive.  Any thoughts or 
suggestions would be appreciated.  Thanks

If you're on Unix, look up the 'tee' command.
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]