Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

stderr to script-logfile with CGI::Fast

by poulhs (Beadle)
on Apr 06, 2011 at 13:00 UTC ( #897768=perlquestion: print w/replies, xml ) Need Help??
poulhs has asked for the wisdom of the Perl Monks concerning the following question:

Seeking the wisdom of the monks...

I am trying to redirect stderr to a script-specific logfile from a CGI::Fast script:

#!/usr/bin/perl use strict; use warnings; use English qw( no_match_vars ); use Fcntl; use CGI::Fast; our $errlog; BEGIN { $errlog = "/var/tmp/foo.log"; sysopen( STDERR, $errlog, O_WRONLY | O_CREAT | O_APPEND ) or warn( "Failed to open $errlog: $ERRNO" ); } warn( "OK\n" ); while (my $q = CGI::Fast->new()) { print $q->header(), $q->start_html( sprintf( "%2.2d:%2.2d:%2.2d", ( localtime($^T) ) +[2,1,0] ) ), $q->h1( scalar localtime( $^T ) ), $q->code( "pid=$$ errlog=$errlog" ), $q->end_html,"\n"; warn( "NOT OK\n" ); } __END__

The "problem" seems to be, that CGI::Fast->new() (which is calling FCGI::accept()), resets STDERR. I've even tried misc. stuff after the CGI::Fast->new(), like reopening sysopen( STDERR, $errlog, ... ) and more obfuscating stuff like my $req = $FCGI::global_request; followed by misc. dup'ing and/or *STDERR = ...

Anyone got a solution to this problem (or tinks it is unsolvable)?

ps: I have no problems with $SIG{__WARN__} and $SIG{__DIE__} handlers, but would really like to redirect STDERR completely.

amen

Replies are listed 'Best First'.
Re: stderr to script-logfile with CGI::Fast
by Anonymous Monk on Apr 06, 2011 at 13:06 UTC
    Try
    BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or die("Unable to open mycgi-log: $!\n"); carpout(LOG); }

      Thanks, but that did not do the trick, CGI::Fast->new() (actually FCGI), reopens the file-handle.

      anyone got an idea how to change this behavior?

Re: stderr to script-logfile with CGI::Fast
by poulhs (Beadle) on Apr 06, 2011 at 18:56 UTC

    The cause of the "problem"

    From http://cpansearch.perl.org/src/BOBTFISH/FCGI-0.70/os_unix.c:

    /* * Close STDIN unconditionally. It's used by the parent * process for CGI communication. The FastCGI applciation * will be replacing this with the FastCGI listenFd IF * STDIN_FILENO is the same as FCGI_LISTENSOCK_FILENO * (which it is on Unix). Regardless, STDIN, STDOUT, and * STDERR will be closed as the FastCGI process uses a * multiplexed socket in their place. */

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://897768]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2017-09-24 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (275 votes). Check out past polls.

    Notices?