use File::Temp;
use constant STATUS_DIR => '/tmp/status-dir';
{
my $fh; # hidden from outer code
# status is logged only when you have created
# the status dir by hand; this lets you turn off
# logging by deleting the status dir
if ( -d STATUS_DIR ) {
$fh = new File::Temp( template => "$$-XXXXXX",
dir => STATUS_DIR,
suffix => ".txt",
unlink => 1 )
or die "can't open temp file: $!";
$fh->autoflush(1);
}
sub log_status {
print $fh "$$ - ", scalar localtime, " - ", @_, "\n"
if $fh;
}
sub close_status {
$fh->close if $fh;
}
}
####
# here is the meat of our worker code
log_status("worker starting; config=blah...");
# ... work ...
log_status("worker starting stage two");
# ... more work ...
log_status("worker starting stage three");
sleep 30; # it's a long one
# finally! whew, that was hard work :)
log_status("worker exiting");
close_status();
####
$ cat /tmp/status-dir/14416-UXL0uq.txt
14416 - Fri Nov 12 12:40:06 2004 - worker starting; config=blah...
14416 - Fri Nov 12 12:40:06 2004 - worker starting stage two
14416 - Fri Nov 12 12:40:06 2004 - worker starting stage three