Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Get your script warnings on the page it generates

by BooK (Curate)
on Jan 18, 2001 at 04:34 UTC ( #52650=snippet: print w/replies, xml ) Need Help??

You use -w and maybe diagnostics in all your CGI scripts, but looking for the warnings in /var/log/httpd/error_log is too much for you...

Instead of letting them clutter the log file, check them out on the generated page instead!


# your CGI stuff

END   { close STDERR;
        print '<hr><b>Script warnings</b>',
              '<small><pre>', <DEBUG>, '</pre></small>'
            unless eof DEBUG;
Replies are listed 'Best First'.
(tye)Re: Get your script warnings on the page it generates
by tye (Sage) on Jan 18, 2001 at 05:12 UTC

    Won't this cause your script to hang if more than one "bufferful" of warnings gets generated?

            - tye (but my friends call me "Tye")

      You are absolutely right, it would! (and I didn't think of it... Just wanted to avoid checking that log file)

      I guess this is the reason why this kind of tricks should be use while coding the script, and not in production...

      But I wouldn't want to show such intimate stuff as warnings to the people who surf my site...

      Update 2: When Apache waits too long for a script and there is still no output, it seems to kill the subprocess... Then we enter the END block, and the warnings are printed (I guess).

      Update: If you are really concerned about this, you can always fork a child, and use the pipe to pass it the warnings... The child will store them in an array or a string, and pass them back when your end of the pipe closes... Much heavier, but feasible.

      BEGIN { pipe(READ, WRITE); *STDERR = *WRITE; if(!open(DEBUG, "-|") { # child close WRITE; my @w = <READ>; print @w; exit } } END { close WRITE; print '<hr><b>Script warnings</b>', '<small><pre>', <DEBUG>, '</pre></small>' }
      (This is untested)

      Question: is the END block executed by the child process? If so, where goes the probable warning about the closed DEBUG handle?

Re: Get your script warnings on the page it generates
by Gloom (Monk) on Jan 22, 2001 at 21:23 UTC
    why don't you catch'n display your warnings like this :
    $SIG{__WARN__} = sub { print "<HR><B>Warning : </B>$_[0]<HR>"; };

      This one is probably the best way to do it. I can save the warnings into a scalar for later use...

      If only I had toroughly read Amelia... Actually, the only problem really is that it works only with 5.6. (As if my script didn't already use 5.6 specific stuff already!)

      Great, I'll do this and share! Thanks!

Re: Get your script warnings on the page it generates
by merlyn (Sage) on Jan 18, 2001 at 20:00 UTC
Re: Get your script warnings on the page it generates
by marius (Hermit) on Jan 18, 2001 at 21:09 UTC
    Another handy module that takes care of this for you (TMTOWTDI) is CGI::Carp. I've used it quite effectively for debugging in the past. I also like merlyn's idea of having them emailed to you -- I do that with my 404 errors, which gets obnoxious when your friends find out about it ;-)

    use CGI::Carp qw(FatalsToBrowser);

      But warnings aren't fatal...

      use CGI::Carp qw(fatalsToBrowser); will send die/croak/confess messages to the browser, but not warn/carp/cluck messages.

        Good call, chipmunk. I presume you could use carpout() from CGI::Carp to redirect warn/carp/cluck calls to STDOUT.

        Additionally as of CGI::Carp v1.20:
        use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
        With this, you can call
        to past warnings to the browser after the call.

        Being proven wrong is one of the best reasons to go RTFM, I guess. =]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://52650]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-12-12 22:39 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (340 votes). Check out past polls.