| 1 | package Log::Log4perl::Config::Watch; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 55 | ▶ | use constant _INTERNAL_DEBUG => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 55 | our $NEXT_CHECK_TIME; | |||||||||||||||||||||||||||
| 6 | 55 | our $SIGNAL_CAUGHT; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 55 | our $L4P_TEST_CHANGE_DETECTED; | |||||||||||||||||||||||||||
| 9 | 55 | our $L4P_TEST_CHANGE_CHECKED; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | ########################################### | ||||||||||||||||||||||||||||
| 12 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | ########################################### | ||||||||||||||||||||||||||||
| 14 | 25 | my($class, %options) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 25 | my $self = { file => "", | |||||||||||||||||||||||||||
| 17 | check_interval => 30, | ||||||||||||||||||||||||||||
| 18 | l4p_internal => 0, | ||||||||||||||||||||||||||||
| 19 | signal => undef, | ||||||||||||||||||||||||||||
| 20 | %options, | ||||||||||||||||||||||||||||
| 21 | _last_checked_at => 0, | ||||||||||||||||||||||||||||
| 22 | _last_timestamp => 0, | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 25 | bless $self, $class; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 25 | ▶ | if($self->{signal}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | # We're in signal mode, set up the handler | ||||||||||||||||||||||||||||
| 29 | 10 | print "Setting up signal handler for '$self->{signal}'\n" if | |||||||||||||||||||||||||||
| 30 | _INTERNAL_DEBUG; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # save old signal handlers; they belong to other appenders or | ||||||||||||||||||||||||||||
| 33 | # possibly something else in the consuming application | ||||||||||||||||||||||||||||
| 34 | 10 | my $old_sig_handler = $SIG{$self->{signal}}; | |||||||||||||||||||||||||||
| 35 | 4 | ▶ | $SIG{$self->{signal}} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 36 | 4 | print "Caught $self->{signal} signal\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 37 | 4 | $self->force_next_check(); | |||||||||||||||||||||||||||
| 38 | 4 | ▶ | $old_sig_handler->(@_) if $old_sig_handler and ref $old_sig_handler eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$old_sig_handler and ref $old_sig_handler eq "CODE"
MC/DC50%1/2
$old_sig_handler and ref $old_sig_handler eq "CODE"
$old_sig_handler
ref $old_sig_handler eq "CODE"
Truth table66%2/3
A and B
A= $old_sig_handlerB= ref $old_sig_handler eq "CODE"
| |||||||||||||||||||||||||||||
| 39 | 10 | }; | |||||||||||||||||||||||||||
| 40 | # Reset the marker. The handler is going to modify it. | ||||||||||||||||||||||||||||
| 41 | 10 | $self->{signal_caught} = 0; | |||||||||||||||||||||||||||
| 42 | 10 | ▶ | $SIGNAL_CAUGHT = 0 if $self->{l4p_internal}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | # Just called to initialize | ||||||||||||||||||||||||||||
| 45 | 15 | $self->change_detected(undef, 1); | |||||||||||||||||||||||||||
| 46 | 15 | $self->file_has_moved(undef, 1); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 25 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ########################################### | ||||||||||||||||||||||||||||
| 53 | 5 | ▶ | sub force_next_check { | ||||||||||||||||||||||||||
|
Subroutine
force_next_check: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | ########################################### | ||||||||||||||||||||||||||||
| 55 | 5 | my($self) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 5 | $self->{signal_caught} = 1; | |||||||||||||||||||||||||||
| 58 | 5 | $self->{next_check_time} = 0; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | if( $self->{l4p_internal} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | $SIGNAL_CAUGHT = 1; | |||||||||||||||||||||||||||
| 62 | 1 | $NEXT_CHECK_TIME = 0; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ########################################### | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub force_next_check_reset { | ||||||||||||||||||||||||||
|
Subroutine
force_next_check_reset: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | ########################################### | ||||||||||||||||||||||||||||
| 69 | 1 | my($self) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | $self->{signal_caught} = 0; | |||||||||||||||||||||||||||
| 72 | 1 | ▶ | $SIGNAL_CAUGHT = 0 if $self->{l4p_internal}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | ########################################### | ||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub file { | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | ########################################### | ||||||||||||||||||||||||||||
| 78 | 3 | my($self) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | return $self->{file}; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | ########################################### | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub signal { | ||||||||||||||||||||||||||
|
Subroutine
signal: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | ########################################### | ||||||||||||||||||||||||||||
| 86 | 1 | my($self) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | return $self->{signal}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | ########################################### | ||||||||||||||||||||||||||||
| 92 | 2 | ▶ | sub check_interval { | ||||||||||||||||||||||||||
|
Subroutine
check_interval: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | ########################################### | ||||||||||||||||||||||||||||
| 94 | 2 | my($self) = @_; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | return $self->{check_interval}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ########################################### | ||||||||||||||||||||||||||||
| 100 | 25 | ▶ | sub file_has_moved { | ||||||||||||||||||||||||||
|
Subroutine
file_has_moved: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | ########################################### | ||||||||||||||||||||||||||||
| 102 | 25 | my($self, $time, $force) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 22 | ▶ | my $task = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 105 | 22 | my @stat = stat($self->{file}); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 22 | my $has_moved = 0; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 22 | ▶ | if(! $stat[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | # The file's gone, obviously it got moved or deleted. | ||||||||||||||||||||||||||||
| 111 | 4 | print "File is gone\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 112 | 4 | return 1; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 18 | my $current_inode = "$stat[0]:$stat[1]"; | |||||||||||||||||||||||||||
| 116 | 18 | print "Current inode: $current_inode\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 18 | ▶ | if(exists $self->{_file_inode} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $self->{"_file_inode"} and $self->{"_file_inode"} ne $current_inode
MC/DC100%2/2
exists $self->{"_file_inode"} and $self->{"_file_inode"} ne $current_inode
exists $self->{"_file_inode"}
$self->{"_file_inode"} ne $current_inode
Truth table100%3/3
A and B
A= exists $self->{"_file_inode"}B= $self->{"_file_inode"} ne $current_inode
| |||||||||||||||||||||||||||||
| 119 | $self->{_file_inode} ne $current_inode) { | ||||||||||||||||||||||||||||
| 120 | 1 | print "Inode changed from $self->{_file_inode} to ", | |||||||||||||||||||||||||||
| 121 | "$current_inode\n" if _INTERNAL_DEBUG; | ||||||||||||||||||||||||||||
| 122 | 1 | $has_moved = 1; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 18 | $self->{_file_inode} = $current_inode; | |||||||||||||||||||||||||||
| 126 | 18 | return $has_moved; | |||||||||||||||||||||||||||
| 127 | 25 | }; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 25 | return $self->check($time, $task, $force); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | ########################################### | ||||||||||||||||||||||||||||
| 133 | 22 | ▶ | sub change_detected { | ||||||||||||||||||||||||||
|
Subroutine
change_detected: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | ########################################### | ||||||||||||||||||||||||||||
| 135 | 22 | my($self, $time, $force) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 21 | ▶ | my $task = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||
| 138 | 21 | my @stat = stat($self->{file}); | |||||||||||||||||||||||||||
| 139 | 21 | my $new_timestamp = $stat[9]; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 21 | $L4P_TEST_CHANGE_CHECKED = 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 21 | ▶ | if(! defined $new_timestamp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if($self->{l4p_internal}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | # The file is gone? Let it slide, we don't want L4p to re-read | ||||||||||||||||||||||||||||
| 146 | # the config now, it's gonna die. | ||||||||||||||||||||||||||||
| 147 | 0 | return undef; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | $L4P_TEST_CHANGE_DETECTED = 1; | |||||||||||||||||||||||||||
| 150 | 0 | return 1; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 21 | ▶ | if($new_timestamp > $self->{_last_timestamp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 18 | $self->{_last_timestamp} = $new_timestamp; | |||||||||||||||||||||||||||
| 155 | 18 | print "Change detected (file=$self->{file} store=$new_timestamp)\n" | |||||||||||||||||||||||||||
| 156 | if _INTERNAL_DEBUG; | ||||||||||||||||||||||||||||
| 157 | 18 | $L4P_TEST_CHANGE_DETECTED = 1; | |||||||||||||||||||||||||||
| 158 | 18 | return 1; # Has changed | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | print "$self->{file} unchanged (file=$new_timestamp ", | |||||||||||||||||||||||||||
| 162 | "stored=$self->{_last_timestamp})!\n" if _INTERNAL_DEBUG; | ||||||||||||||||||||||||||||
| 163 | 3 | return ""; # Hasn't changed | |||||||||||||||||||||||||||
| 164 | 22 | }; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 22 | return $self->check($time, $task, $force); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | ########################################### | ||||||||||||||||||||||||||||
| 170 | 47 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | ########################################### | ||||||||||||||||||||||||||||
| 172 | 47 | my($self, $time, $task, $force) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 47 | ▶ | $time = time() unless defined $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 47 | ▶ | if( $self->{signal_caught} or $SIGNAL_CAUGHT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"signal_caught"} or $SIGNAL_CAUGHT
MC/DC50%1/2
$self->{"signal_caught"} or $SIGNAL_CAUGHT
$self->{"signal_caught"}
$SIGNAL_CAUGHT
Truth table66%2/3
A or B
A= $self->{"signal_caught"}B= $SIGNAL_CAUGHT
| |||||||||||||||||||||||||||||
| 177 | 1 | $force = 1; | |||||||||||||||||||||||||||
| 178 | 1 | $self->force_next_check_reset(); | |||||||||||||||||||||||||||
| 179 | 1 | print "Caught signal, forcing check\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 47 | print "Soft check (file=$self->{file} time=$time)\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Do we need to check? | ||||||||||||||||||||||||||||
| 186 | 47 | ▶ | if(!$force and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $force and $self->{"_last_checked_at"} + $self->{"check_interval"} > $time
MC/DC100%2/2
not $force and $self->{"_last_checked_at"} + $self->{"check_interval"} > $time
not $force
$self->{"_last_checked_at"} + $self->{"check_interval"} > $time
Truth table100%3/3
not(A) and B
A= not $forceB= $self->{"_last_checked_at"} + $self->{"check_interval"} > $time
| |||||||||||||||||||||||||||||
| 187 | $self->{_last_checked_at} + | ||||||||||||||||||||||||||||
| 188 | $self->{check_interval} > $time) { | ||||||||||||||||||||||||||||
| 189 | 4 | print "No need to check\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 190 | 4 | return ""; # don't need to check, return false | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 43 | $self->{_last_checked_at} = $time; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Set global var for optimizations in case we just have one watcher | ||||||||||||||||||||||||||||
| 196 | # (like in Log::Log4perl) | ||||||||||||||||||||||||||||
| 197 | 43 | $self->{next_check_time} = $time + $self->{check_interval}; | |||||||||||||||||||||||||||
| 198 | 43 | ▶ | $NEXT_CHECK_TIME = $self->{next_check_time} if $self->{l4p_internal}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 43 | print "Hard check (file=$self->{file} time=$time)\n" if _INTERNAL_DEBUG; | |||||||||||||||||||||||||||
| 201 | 43 | return $task->($time); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 55 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||