Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

error report

by steph_bow (Pilgrim)
on Mar 06, 2008 at 09:53 UTC ( [id://672415]=perlquestion: print w/replies, xml ) Need Help??

steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Dear Monks

Is there an easy way to produce at each run of my scripts, a text file which sums up all the warnings and messages of error that happened during the execution of the script ?

Thanks a lot

Replies are listed 'Best First'.
Re: error report
by TOD (Friar) on Mar 06, 2008 at 10:12 UTC
    try something like this:
    use Symbol 'gensym'; our $log = gensym; open $log, ">/path/to/logfile" or die $!; our $DEBUG = 1; sub add_log; local $SIG{__WARN__} = sub { &add_log(shift) }; local $SIG{__DIE__} = sub { &add_log(shift); exit 1 }; sub add_log { print $log, shift(); flush $log if $DEBUG; } sub DESTROY { close $log; }
    admitted this is more of a rough draft, but the method should be clear from these few lines.
    --------------------------------
    masses are the opiate for religion.
Re: error report
by nefigah (Monk) on Mar 06, 2008 at 10:12 UTC
    Since these messages are normally sent to STDERR, you can achieve this by redirecting STDERR to the text file of your choice. Check out http://perldoc.perl.org/functions/open.html, especially the part about halfway down that starts, "You may also, in the Bourne shell tradition, specify an EXPR beginning with..."

    Good luck!
Re: error report
by romandas (Pilgrim) on Mar 06, 2008 at 10:35 UTC
    Besides redirecting to STDERR within the program, you can also redirect it from the shell if you run it at the commandline.

    In both Linux and Windows, you can do this:
    c:\perl_program.pl 2> error.log

    or

    $./perl_program 2> error.log

    This allows you to change the error file on the fly, or see it onscreen without changing the code. You can redirect STDOUT the same way, just leave off the 2 (or use 1; same thing).
      If in a *nix or Cygwin environment I will usually "| tee <filename>" which duplicates the output to both the console and the named file.
Re: error report
by sundialsvc4 (Abbot) on Mar 06, 2008 at 22:11 UTC

    Note that in most (if not all) shells, using two greater-than signs (e.g. >>filename) will append the output to the specified file (or create it if it does not yet exist), whereas using only one such character will replace it.

    In Unix-ish environments, all programs have one “standard input” file (STDIN), one “standard output” (STDOUT), and one “ standard error-output” (STDERR). The default is that STDIN reads from the terminal (or from an input-pipe), and both STDOUT and STDERR write to the terminal.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://672415]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-19 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found