File Coverage

blib/lib/Log/Dispatch/File/Locked.pm
Criterion Covered Total %
statement 25 26 96.1
branch 10 18 55.5
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 40 50 80.0


line stmt bran cond sub pod time code
1             package Log::Dispatch::File::Locked;
2              
3 22     22   11085 use strict;
  22         66  
  22         686  
4 22     22   111 use warnings;
  22         45  
  22         975  
5              
6             our $VERSION = '2.70';
7              
8 22     22   111 use Fcntl qw(:DEFAULT :flock);
  22         66  
  22         7997  
9              
10 22     22   177 use base qw( Log::Dispatch::File );
  22         44  
  22         11250  
11              
12             sub log_message {
13 60     60 0 250 my $self = shift;
14 60         211 my %p = @_;
15              
16 60 100       227 if ( $self->{close_after_write} ) {
17 30         32050 $self->_open_file;
18             }
19              
20 60         409 my $fh = $self->{fh};
21              
22 60 50       16891 flock( $fh, LOCK_EX )
23             or die "Cannot lock '$self->{filename}' for writing: $!";
24              
25             # just in case there was an append while we waited for the lock
26 60 50       647 seek( $fh, 0, 2 )
27             or die "Cannot seek to end of '$self->{filename}': $!";
28              
29 60 50       430 if ( $self->{syswrite} ) {
30             defined syswrite( $fh, $p{message} )
31 0 0       0 or die "Cannot write to '$self->{filename}': $!";
32             }
33             else {
34             print $fh $p{message}
35 60 50       2096 or die "Cannot write to '$self->{filename}': $!";
36             }
37              
38 60 50       554 flock( $fh, LOCK_UN ) or die "Cannot unlock '$self->{filename}'";
39 60 100       428 if ( $self->{close_after_write} ) {
40 30 50       362 close $fh
41             or die "Cannot close '$self->{filename}': $!";
42 30         344 delete $self->{fh};
43             }
44             }
45              
46             1;
47              
48             # ABSTRACT: Subclass of Log::Dispatch::File to facilitate locking
49              
50             __END__
51              
52             =pod
53              
54             =encoding UTF-8
55              
56             =head1 NAME
57              
58             Log::Dispatch::File::Locked - Subclass of Log::Dispatch::File to facilitate locking
59              
60             =head1 VERSION
61              
62             version 2.70
63              
64             =head1 SYNOPSIS
65              
66             use Log::Dispatch;
67              
68             my $log = Log::Dispatch->new(
69             outputs => [
70             [
71             'File::Locked',
72             min_level => 'info',
73             filename => 'Somefile.log',
74             mode => '>>',
75             newline => 1
76             ]
77             ],
78             );
79              
80             $log->emerg("I've fallen and I can't get up");
81              
82             =head1 DESCRIPTION
83              
84             This module acts exactly like L<Log::Dispatch::File> except that it
85             obtains an exclusive lock on the file while opening it.
86              
87             Note that if you are using this output because you want to write to a file
88             from multiple processes, you should open the file with the append C<mode>
89             (C<<< >> >>>), or else it's quite likely that one process will overwrite
90             another.
91              
92             =head1 SEE ALSO
93              
94             L<perlfunc/flock>
95              
96             =head1 SUPPORT
97              
98             Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
99              
100             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
101              
102             =head1 SOURCE
103              
104             The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
105              
106             =head1 AUTHOR
107              
108             Dave Rolsky <autarch@urth.org>
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This software is Copyright (c) 2020 by Dave Rolsky.
113              
114             This is free software, licensed under:
115              
116             The Artistic License 2.0 (GPL Compatible)
117              
118             The full text of the license can be found in the
119             F<LICENSE> file included with this distribution.
120              
121             =cut