/usr/share/perl5/Log/Log4perl/Config/Watch.pm is in liblog-log4perl-perl 1.41-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | package Log::Log4perl::Config::Watch;
use constant _INTERNAL_DEBUG => 0;
our $NEXT_CHECK_TIME;
our $SIGNAL_CAUGHT;
our $L4P_TEST_CHANGE_DETECTED;
our $L4P_TEST_CHANGE_CHECKED;
###########################################
sub new {
###########################################
my($class, %options) = @_;
my $self = { file => "",
check_interval => 30,
l4p_internal => 0,
signal => undef,
%options,
_last_checked_at => 0,
_last_timestamp => 0,
};
bless $self, $class;
if($self->{signal}) {
# We're in signal mode, set up the handler
print "Setting up signal handler for '$self->{signal}'\n" if
_INTERNAL_DEBUG;
# save old signal handlers; they belong to other appenders or
# possibly something else in the consuming application
my $old_sig_handler = $SIG{$self->{signal}};
$SIG{$self->{signal}} = sub {
print "Caught $self->{signal} signal\n" if _INTERNAL_DEBUG;
$self->force_next_check();
$old_sig_handler->(@_) if $old_sig_handler and ref $old_sig_handler eq 'CODE';
};
# Reset the marker. The handler is going to modify it.
$self->{signal_caught} = 0;
$SIGNAL_CAUGHT = 0 if $self->{l4p_internal};
} else {
# Just called to initialize
$self->change_detected(undef, 1);
$self->file_has_moved(undef, 1);
}
return $self;
}
###########################################
sub force_next_check {
###########################################
my($self) = @_;
$self->{signal_caught} = 1;
$self->{next_check_time} = 0;
if( $self->{l4p_internal} ) {
$SIGNAL_CAUGHT = 1;
$NEXT_CHECK_TIME = 0;
}
}
###########################################
sub force_next_check_reset {
###########################################
my($self) = @_;
$self->{signal_caught} = 0;
$SIGNAL_CAUGHT = 0 if $self->{l4p_internal};
}
###########################################
sub file {
###########################################
my($self) = @_;
return $self->{file};
}
###########################################
sub signal {
###########################################
my($self) = @_;
return $self->{signal};
}
###########################################
sub check_interval {
###########################################
my($self) = @_;
return $self->{check_interval};
}
###########################################
sub file_has_moved {
###########################################
my($self, $time, $force) = @_;
my $task = sub {
my @stat = stat($self->{file});
my $has_moved = 0;
if(! $stat[0]) {
# The file's gone, obviously it got moved or deleted.
print "File is gone\n" if _INTERNAL_DEBUG;
return 1;
}
my $current_inode = "$stat[0]:$stat[1]";
print "Current inode: $current_inode\n" if _INTERNAL_DEBUG;
if(exists $self->{_file_inode} and
$self->{_file_inode} ne $current_inode) {
print "Inode changed from $self->{_file_inode} to ",
"$current_inode\n" if _INTERNAL_DEBUG;
$has_moved = 1;
}
$self->{_file_inode} = $current_inode;
return $has_moved;
};
return $self->check($time, $task, $force);
}
###########################################
sub change_detected {
###########################################
my($self, $time, $force) = @_;
my $task = sub {
my @stat = stat($self->{file});
my $new_timestamp = $stat[9];
$L4P_TEST_CHANGE_CHECKED = 1;
if(! defined $new_timestamp) {
if($self->{l4p_internal}) {
# The file is gone? Let it slide, we don't want L4p to re-read
# the config now, it's gonna die.
return undef;
}
$L4P_TEST_CHANGE_DETECTED = 1;
return 1;
}
if($new_timestamp > $self->{_last_timestamp}) {
$self->{_last_timestamp} = $new_timestamp;
print "Change detected (file=$self->{file} store=$new_timestamp)\n"
if _INTERNAL_DEBUG;
$L4P_TEST_CHANGE_DETECTED = 1;
return 1; # Has changed
}
print "$self->{file} unchanged (file=$new_timestamp ",
"stored=$self->{_last_timestamp})!\n" if _INTERNAL_DEBUG;
return ""; # Hasn't changed
};
return $self->check($time, $task, $force);
}
###########################################
sub check {
###########################################
my($self, $time, $task, $force) = @_;
$time = time() unless defined $time;
if( $self->{signal_caught} or $SIGNAL_CAUGHT ) {
$force = 1;
$self->force_next_check_reset();
print "Caught signal, forcing check\n" if _INTERNAL_DEBUG;
}
print "Soft check (file=$self->{file} time=$time)\n" if _INTERNAL_DEBUG;
# Do we need to check?
if(!$force and
$self->{_last_checked_at} +
$self->{check_interval} > $time) {
print "No need to check\n" if _INTERNAL_DEBUG;
return ""; # don't need to check, return false
}
$self->{_last_checked_at} = $time;
# Set global var for optimizations in case we just have one watcher
# (like in Log::Log4perl)
$self->{next_check_time} = $time + $self->{check_interval};
$NEXT_CHECK_TIME = $self->{next_check_time} if $self->{l4p_internal};
print "Hard check (file=$self->{file} time=$time)\n" if _INTERNAL_DEBUG;
return $task->($time);
}
1;
__END__
=head1 NAME
Log::Log4perl::Config::Watch - Detect file changes
=head1 SYNOPSIS
use Log::Log4perl::Config::Watch;
my $watcher = Log::Log4perl::Config::Watch->new(
file => "/data/my.conf",
check_interval => 30,
);
while(1) {
if($watcher->change_detected()) {
print "Change detected!\n";
}
sleep(1);
}
=head1 DESCRIPTION
This module helps detecting changes in files. Although it comes with the
C<Log::Log4perl> distribution, it can be used independently.
The constructor defines the file to be watched and the check interval
in seconds. Subsequent calls to C<change_detected()> will
=over 4
=item *
return a false value immediately without doing physical file checks
if C<check_interval> hasn't elapsed.
=item *
perform a physical test on the specified file if the number
of seconds specified in C<check_interval>
have elapsed since the last physical check. If the file's modification
date has changed since the last physical check, it will return a true
value, otherwise a false value is returned.
=back
Bottom line: C<check_interval> allows you to call the function
C<change_detected()> as often as you like, without paying the performing
a significant performance penalty because file system operations
are being performed (however, you pay the price of not knowing about
file changes until C<check_interval> seconds have elapsed).
The module clearly distinguishes system time from file system time.
If your (e.g. NFS mounted) file system is off by a constant amount
of time compared to the executing computer's clock, it'll just
work fine.
To disable the resource-saving delay feature, just set C<check_interval>
to 0 and C<change_detected()> will run a physical file test on
every call.
If you already have the current time available, you can pass it
on to C<change_detected()> as an optional parameter, like in
change_detected($time)
which then won't trigger a call to C<time()>, but use the value
provided.
=head2 SIGNAL MODE
Instead of polling time and file changes, C<new()> can be instructed
to set up a signal handler. If you call the constructor like
my $watcher = Log::Log4perl::Config::Watch->new(
file => "/data/my.conf",
signal => 'HUP'
);
then a signal handler will be installed, setting the object's variable
C<$self-E<gt>{signal_caught}> to a true value when the signal arrives.
Comes with all the problems that signal handlers go along with.
=head2 TRIGGER CHECKS
To trigger a physical file check on the next call to C<change_detected()>
regardless if C<check_interval> has expired or not, call
$watcher->force_next_check();
on the watcher object.
=head2 DETECT MOVED FILES
The watcher can also be used to detect files that have moved. It will
not only detect if a watched file has disappeared, but also if it has
been replaced by a new file in the meantime.
my $watcher = Log::Log4perl::Config::Watch->new(
file => "/data/my.conf",
check_interval => 30,
);
while(1) {
if($watcher->file_has_moved()) {
print "File has moved!\n";
}
sleep(1);
}
The parameters C<check_interval> and C<signal> limit the number of physical
file system checks, simililarily as with C<change_detected()>.
=head1 LICENSE
Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt>
and Kevin Goess E<lt>cpan@goess.orgE<gt>.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Please contribute patches to the project on Github:
http://github.com/mschilli/log4perl
Send bug reports or requests for enhancements to the authors via our
MAILING LIST (questions, bug reports, suggestions/patches):
log4perl-devel@lists.sourceforge.net
Authors (please contact them via the list above, not directly):
Mike Schilli <m@perlmeister.com>,
Kevin Goess <cpan@goess.org>
Contributors (in alphabetical order):
Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton
Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony
Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy
Grundman, Paul Harrington, Alexander Hartmaier David Hull,
Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter,
Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope,
Lars Thegler, David Viner, Mac Yang.
|