Problem with redirecting STDOUT and STDERR 
Author Message
 Problem with redirecting STDOUT and STDERR

I want to redirect standard out and stderr into a Log module that I
created. I created a Log.pm and performed the following tie:

# Redirect STDERR and STDOUT to logfile
tie( *STDOUT, 'Log', $syswatch->getval( 'logfile' ),
     $syswatch->getval( 'logfilerotateinterval' ) )
    or die "Can't tie variable: $!\n";
autoflush STDOUT 1;
*STDERR = *STDOUT;

I can do a print "blah" and a print STDERR "blah" and both go to the
Log module correctly. However here is the problem. I have insertable
modules that a user can add to the program by modifying a configfile.
Perl will run the code, and because it is user code, sometimes it
generates errors, like the following:

Undefined subroutine &Plugins::Foo::_do_check called at
lib/Plugins/Foo.pm line 48.

The problem is that this message is not getting redirected to the Log
module. I guess it is because the error message is generated by perl
and not by the program. The question is... How can I redirect those
errors to that Log module I wrote...

Of course the simple solution is to go ./program 2>&1 | ./logprogram
however I would like to avoid doing that where possible.

Thanks,
Scott



Mon, 19 Sep 2005 22:00:30 GMT  
 Problem with redirecting STDOUT and STDERR

Quote:

> I want to redirect standard out and stderr into a Log
> module that I created. I created a Log.pm and performed
> the following tie:

[snip]

Quote:
> The problem is that this message is not getting redirected
> to the Log module. I guess it is because the error message
> is generated by perl and not by the program. The question is...
> How can I redirect those errors to that Log module I wrote...


--
Steve



Mon, 19 Sep 2005 22:28:39 GMT  
 Problem with redirecting STDOUT and STDERR

This doesn't help. I still get the same message.

bash-2.03$ ./newsyswatch.pl
Successful startup, daemonizing...
bash-2.03$ Can't locate object method "do_check" via package
"Plugins::foo" at lib/Plugins/fo.pm line 48.


guess is that I am not redirecting STDERR correctly...

I've spent a couple of hours on this and came up with some clues... 1)
If I tie STDOUT and STDERR seperately perl will create two instances
of the log module which is bad... I've also found that open( STDERR,
">&STDOUT" ) doesn't work either... So once I tie STDOUT to the Log
module, how can I redirect STDERR to do the same thing as STDOUT?

Thanks,
Scott



Tue, 20 Sep 2005 14:16:53 GMT  
 Problem with redirecting STDOUT and STDERR

Quote:

> I want to redirect standard out and stderr into a Log module that I
> created. I created a Log.pm and performed the following tie:

> # Redirect STDERR and STDOUT to logfile
> tie( *STDOUT, 'Log', $syswatch->getval( 'logfile' ),
>      $syswatch->getval( 'logfilerotateinterval' ) )
>     or die "Can't tie variable: $!\n";
> autoflush STDOUT 1;
> *STDERR = *STDOUT;
>                   ...message is not getting redirected to the Log
> module. I guess it is because the error message is generated by perl
> and not by the program.

Tied STDERR was not honoured by Perl error messages in 5.6.

Do you have an old Perl?

Quote:
>    ...errors, like the following:

> Undefined subroutine &Plugins::Foo::_do_check called at
> lib/Plugins/Foo.pm line 48.

But hang on that is a true _error_ not just output to STDOUT.  Errors
are exceptions.  Uncaught exceptions are going to tear down your whole
stack of cards including your tied file handle.

You need to be trapping exceptions using the eval{} or $SIG{__DIE__}
mechanisms.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Tue, 20 Sep 2005 20:16:33 GMT  
 Problem with redirecting STDOUT and STDERR

Quote:



> This doesn't help. I still get the same message.

Hmm... Works here with 5.8.0, but not 5.6.1.

But you'll need a $SIG{__WARN__} handler in any event, because
internal warnings don't go through STDERR.

Quote:


> So my guess is that I am not redirecting STDERR correctly...

This might not be your fault.  OTOH it would be cleaner (and
marginally more efficient) to do something like

    my $object = tie *STDOUT, ...

Or you could let client code create the Log object ahead of
time and pass it as an argument to tie().

    my $logger = Log->new(%config);

    tie *STDOUT, 'Log', $logger;
    tie *STDERR, 'Log', $logger;


HTH
--
Steve



Tue, 20 Sep 2005 22:40:50 GMT  
 Problem with redirecting STDOUT and STDERR

Quote:


>>                   ...message is not getting redirected to the Log
>> module. I guess it is because the error message is generated by perl
>> and not by the program.

> Tied STDERR was not honoured by Perl error messages in 5.6.

5.8.0 still ignores the tie when issuing (internal) warnings.

--
Steve



Tue, 20 Sep 2005 22:57:40 GMT  
 Problem with redirecting STDOUT and STDERR

Quote:

> But you'll need a $SIG{__WARN__} handler in any event, because
> internal warnings don't go through STDERR.

Yeah this is a good idea... I kept it in there.

Quote:
> Or you could let client code create the Log object ahead of
> time and pass it as an argument to tie().

>     my $logger = Log->new(%config);

>     tie *STDOUT, 'Log', $logger;
>     tie *STDERR, 'Log', $logger;

This is a good idea... After a bit of modification, it works well.
Thanks,
Scott


Fri, 23 Sep 2005 20:42:30 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. HELP - with redirect of STDERR and STDOUT

2. redirect STDERR to STDOUT ?????

3. redirecting stderr and stdout

4. writing to terminal even with STDOUT and STDERR redirected

5. assigning STDERR whild redirecting STDOUT to a file

6. How to redirect STDOUT,STDERR and keep the message order

7. Redirect stderr to stdout w/ Perl under Unix?

8. Redirecting STDERR to STDOUT (on NT)

9. HELP - with redirect of STDERR and STDOUT

10. need to redirect STDOUT and STDERR

11. Redirecting stdout/stderr from a call to system()

12. Redirecting STDERR & STDOUT

 

 
Powered by phpBB® Forum Software