[tpm] transparently detecting when a file goes missing
Indy Singh
indy at indigostar.com
Fri Sep 7 08:08:07 PDT 2012
I think you answered the question yourself.
Tie the filehandle to your package and in the PRINT handler do the open/print/close. The docs can be found here:
http://perldoc.perl.org/perltie.html#Tying-FileHandles
Here is an example:
tie *STDOUT, Shout;
print "aaa\n";
package Shout;
sub TIEHANDLE { my $i; bless \$i, shift }
sub PRINT {
my $fh;
open $fh, ">>out.txt";
$r = shift; $$r++;
print $fh @_;
close $fh;
}
Indy Singh
IndigoSTAR Software -- www.indigostar.com
From: Fulko Hew
Sent: Friday, September 07, 2012 10:31 AM
To: TPM
Subject: [tpm] transparently detecting when a file goes missing
Since the list has been quiet lately, I thought I'd ask the
brilliant minds for a suggestion.
...logging...
[ and yes I know about all the mega-logging frameworks, but ...]
To add simple logging simply, to my app, I decided to close and
reopen STDOUT and STDERR pointing at a log file at the start of my app.
This enables me to simply print and have stuff captured without
having the overhead of a framework.
close STDOUT; open STDOUT, ">>", "$filename"; STDOUT->autoflush(1);
...
print "log this\n";
This technique works great until someone/something comes along
and (re)moves the log file out from under me (like logrotate).
My logging is still happening, but to the original file, and not
the 'new' (empty) file. Since I never re-open the file,
my information isn't going where I expect it to.
If I open/write/close on every message it would work right,
but then my simple approach of catching prints doesn't.
Is there some magic way to detect this occurrence and auto-reopen
the 'new' file?
[Something like tie'ing to a handle's output functionality and
so it could open/print/close everytime instead.]
TIA
Fulko
--------------------------------------------------------------------------------
_______________________________________________
toronto-pm mailing list
toronto-pm at pm.org
http://mail.pm.org/mailman/listinfo/toronto-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20120907/db305678/attachment.html>
More information about the toronto-pm
mailing list