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   11454 use strict;
  22         45  
  22         641  
4 22     22   111 use warnings;
  22         232  
  22         976  
5              
6             our $VERSION = '2.71';
7              
8 22     22   132 use Fcntl qw(:DEFAULT :flock);
  22         44  
  22         8700  
9              
10 22     22   217 use base qw( Log::Dispatch::File );
  22         45  
  22         10749  
11              
12             sub log_message {
13 60     60 0 349 my $self = shift;
14 60         490 my %p = @_;
15              
16 60 100       354 if ( $self->{close_after_write} ) {
17 30         385 $self->_open_file;
18             }
19              
20 60         382 my $fh = $self->{fh};
21              
22 60 50       2081 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       1050 seek( $fh, 0, 2 )
27             or die "Cannot seek to end of '$self->{filename}': $!";
28              
29 60 50       402 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       2892 or die "Cannot write to '$self->{filename}': $!";
36             }
37              
38 60 50       988 flock( $fh, LOCK_UN ) or die "Cannot unlock '$self->{filename}'";
39 60 100       748 if ( $self->{close_after_write} ) {
40 30 50       641 close $fh
41             or die "Cannot close '$self->{filename}': $!";
42 30         557 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.71
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 obtains an
85             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 from
88             multiple processes, you should open the file with the append C<mode> (C<<< >>
89             >>>), or else it's quite likely that one process will overwrite another.
90              
91             =head1 SEE ALSO
92              
93             L<perlfunc/flock>
94              
95             =head1 SUPPORT
96              
97             Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
98              
99             =head1 SOURCE
100              
101             The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
102              
103             =head1 AUTHOR
104              
105             Dave Rolsky <autarch@urth.org>
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is Copyright (c) 2023 by Dave Rolsky.
110              
111             This is free software, licensed under:
112              
113             The Artistic License 2.0 (GPL Compatible)
114              
115             The full text of the license can be found in the
116             F<LICENSE> file included with this distribution.
117              
118             =cut