in reply to Getting around a possible file lock
Minimally tested:
use strict; use File::Basename; errlog_on(); warn "$0 started ". localtime() . $/; warn; errlog_rotate(); warn; # ... { my ( $errlog_file, $orig_stderr ); INIT { our ( $prog, $path ) = fileparse( $0, '.pl' ); $errlog_file = "${path}${prog}.err"; } sub errlog_on { $orig_stderr and defined fileno $orig_stderr or open $orig_stderr, '>&', STDERR or die "Failed to dup STDERR"; open STDERR, '>>', $errlog_file or die "invisible error"; } sub errlog_off { return unless defined fileno STDERR; defined fileno $orig_stderr or die "Can't find original STDERR\n"; close STDERR or die "failed to close $errlog_file"; open STDERR, '>&', $orig_stderr or die "Can't restore STDERR\n"; close $orig_stderr or die "failed to close STDERR dup\n"; } sub errlog_rotate { errlog_off(); log_rotate( $errlog_file ); # backs up $errlog_file # and truncates it; dies # on failure errlog_on(); } }
Update: Fixed a few bugs. In particular, I followed my own advice and used File::Basename to parse the name of the script (for reasons I did not bother to investigate further, the regex in the original code, which I simply copied for this snippet, was producing wrong values for $path and $prog). Also added a check to ensure that $orig_stderr was defined before applying fileno to it (fileno on undef is fatal).
the lowliest monk
|
|---|