1package Log::Dispatch::File::Locked;
2{
3  $Log::Dispatch::File::Locked::VERSION = '2.34';
4}
5
6use strict;
7use warnings;
8
9use base qw( Log::Dispatch::File );
10
11use Fcntl qw(:DEFAULT :flock);
12
13sub _open_file {
14    my $self = shift;
15
16    $self->SUPER::_open_file();
17
18    my $fh = $self->{fh};
19
20    flock( $fh, LOCK_EX )
21        or die "Cannot lock '$self->{filename}' for writing: $!";
22
23    # just in case there was an append while we waited for the lock
24    seek( $fh, 0, 2 )
25        or die "Cannot seek to end of '$self->{filename}': $!";
26}
27
281;
29
30# ABSTRACT: Subclass of Log::Dispatch::File to facilitate locking
31
32__END__
33
34=pod
35
36=head1 NAME
37
38Log::Dispatch::File::Locked - Subclass of Log::Dispatch::File to facilitate locking
39
40=head1 VERSION
41
42version 2.34
43
44=head1 SYNOPSIS
45
46  use Log::Dispatch;
47
48  my $log = Log::Dispatch->new(
49      outputs => [
50          [
51              'File::Locked',
52              min_level => 'info',
53              filename  => 'Somefile.log',
54              mode      => '>>',
55              newline   => 1
56          ]
57      ],
58  );
59
60  $log->emerg("I've fallen and I can't get up");
61
62=head1 DESCRIPTION
63
64This module acts exactly like L<Log::Dispatch::File> except that it
65obtains an exclusive lock on the file before writing to it.
66
67=head1 AUTHOR
68
69Dave Rolsky <autarch@urth.org>
70
71=head1 COPYRIGHT AND LICENSE
72
73This software is Copyright (c) 2011 by Dave Rolsky.
74
75This is free software, licensed under:
76
77  The Artistic License 2.0 (GPL Compatible)
78
79=cut
80