Marcello has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I'm implementing a simple logging module for a project but ran into a problem which is probably easy to fix.
I open a filehandle to the logfile which remains open until the long-running process ends. However, the logfile may be removed by another non-Perl process (so a file lock doesn't help here). After this happened, the printing to the filehandle still returns 1 but the file is not recreated again. Here's in short my problem:
I can use other logging modules out there, but prefer to use my own light-weight module for simple logging.
Any help is as always appreciated! Cheers
I'm implementing a simple logging module for a project but ran into a problem which is probably easy to fix.
I open a filehandle to the logfile which remains open until the long-running process ends. However, the logfile may be removed by another non-Perl process (so a file lock doesn't help here). After this happened, the printing to the filehandle still returns 1 but the file is not recreated again. Here's in short my problem:
Is there a solution so I can detect that the file does not exists anymore? Both print and syswrite report no errors. Ofcourse I could use (-e $filename) but there's always this chance that the file is removed between the test and the print.use strict; use warnings; my $filename = 'logfile.txt'; open(my $handle, '>>', $filename) or die; unlink($filename); print $handle "First line\n"; syswrite($handle, "Second line\n"); close($handle);
I can use other logging modules out there, but prefer to use my own light-weight module for simple logging.
Any help is as always appreciated! Cheers
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Logging to a filehandle of a deleted file
by varian (Chaplain) on Feb 28, 2007 at 10:18 UTC | |
by Moron (Curate) on Feb 28, 2007 at 15:02 UTC | |
Re: Logging to a filehandle of a deleted file
by idsfa (Vicar) on Feb 28, 2007 at 14:30 UTC | |
Re: Logging to a filehandle of a deleted file
by cdarke (Prior) on Feb 28, 2007 at 14:11 UTC | |
Re: Logging to a filehandle of a deleted file
by Anonymous Monk on Feb 28, 2007 at 10:24 UTC | |
by Fletch (Bishop) on Feb 28, 2007 at 13:56 UTC |
Back to
Seekers of Perl Wisdom