home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Locked.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-27  |  1.2 KB  |  61 lines

  1. package Log::Dispatch::File::Locked;
  2.  
  3. use strict;
  4.  
  5. use base qw( Log::Dispatch::File );
  6.  
  7. use Fcntl qw(:DEFAULT :flock);
  8.  
  9.  
  10. sub _open_file
  11. {
  12.     my $self = shift;
  13.  
  14.     $self->SUPER::_open_file();
  15.  
  16.     my $fh = $self->{fh};
  17.  
  18.     flock($fh, LOCK_EX)
  19.         or die "Cannot lock '$self->{filename}' for writing: $!";
  20.  
  21.     # just in case there was an append while we waited for the lock
  22.     seek($fh, 0, 2)
  23.         or die "Cannot seek to end of '$self->{filename}': $!";
  24. }
  25.  
  26.  
  27. 1;
  28.  
  29. __END__
  30.  
  31. =head1 NAME
  32.  
  33. Log::Dispatch::File::Locked - Extension to Log::Dispatch::File to facilitate locking
  34.  
  35. =head1 SYNOPSIS
  36.  
  37.   use Log::Dispatch::File::Locked;
  38.  
  39.   my $file = Log::Dispatch::File::Locked->new( name      => 'locked_file1',
  40.                                                min_level => 'info',
  41.                                                filename  => 'Somefile.log',
  42.                                              );
  43.  
  44.   $file->log( level => 'emerg', message => "I've fallen and I can't get up\n" );
  45.  
  46. =head1 DESCRIPTION
  47.  
  48. This module acts exactly like Log::Dispatch::File except that it
  49. obtains an exclusive lock on the file before writing to it.
  50.  
  51. =head1 METHODS
  52.  
  53. All methods are inherited from Log::Dispatch::File.
  54.  
  55. =head1 AUTHOR
  56.  
  57. Dave Rolsky, <autarch@urth.org>
  58.  
  59. =cut
  60.  
  61.