This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
since
executable file
·189 lines (135 loc) · 3.26 KB
/
since
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
#!/usr/bin/env perl
=head1 NAME
since - Show any new content since the last time a file was read.
=cut
=head1 SYNOPSIS
General Options:
--help Show the help information for this script.
--verbose Show useful debugging information.
=cut
=head1 ABOUT
since allows you to show output appended to a file since the last
time it was executed. The state of a file is recorded in a trivial
database in the file ~/.sincedb
This script is useful for tailing logs:
=for example begin
$ ./since /var/log/messages
$ logger "testing the log"
$ ./since /var/log/messages
Apr 20 11:24:37 precious skx: testing the log
=for example end
=cut
=head1 AUTHOR
Steve
--
http://www.steve.org.uk/
=cut
=head1 LICENSE
Copyright (c) 2013 by Steve Kemp. All rights reserved.
This script is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
use strict;
use warnings;
use NDBM_File;
use Fcntl qw/SEEK_SET O_RDWR O_CREAT/;
use Getopt::Long;
use Pod::Usage;
#
# Get the options, either defaults or from the command line.
#
my %config = parsedOptions();
#
# Ensure we have filename(s)
#
if ( !scalar(@ARGV) )
{
print STDERR "Usage: $0 file1 file2 .. fileN\n";
exit(2);
}
#
# Create the database, using a tie.
#
my %states;
tie( %states, 'NDBM_File', $config{ 'db' }, O_CREAT | O_RDWR, oct(660) ) or
die("Error tying the database $config{'db'} : $!");
#
# For each file on the command line.
#
while ( my $filename = shift )
{
$config{ 'verbose' } && print "Reading $filename\n";
if ( !-e $filename )
{
print STDERR "Skipping missing file $filename\n";
next;
}
if ( -d $filename )
{
print STDERR "since cannot handle directories: $filename\n";
next;
}
#
# Get the details of the file.
#
my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks
) = stat($filename);
if ( !defined $dev )
{
print STDERR "Failed to stat($filename): $!\n";
exit(2);
}
my $state_key = "$dev/$ino";
my $file;
if ( !open( $file, "<", $filename ) )
{
print STDERR "cannot open $filename : $!";
next;
}
#
# If we have a previously recorded offset then use it.
#
my $offset = $states{ $state_key } || 0;
if ( $offset <= $size )
{
sysseek( $file, $offset, SEEK_SET );
}
#
# Read until EOF
#
my $buffer;
while ( ( my $read_count = sysread( $file, $buffer, 4096 ) ) > 0 )
{
$offset += $read_count;
print $buffer;
}
# Nothing to read anymore
close($file);
# Update the database with the offset we've now reached.
$states{ $state_key } = $offset;
}
#
# persist the database, via the tie.
#
untie(%states);
#
# All done.
#
exit(0);
=begin doc
Parse the options and return suitable values.
=end doc
=cut
sub parsedOptions
{
my %vars;
$vars{ 'db' } = $ENV{ HOME } . "/.sincedb";
exit
if (
!GetOptions( "help" => \$vars{ 'help' },
"verbose" => \$vars{ 'verbose' } ) );
pod2usage(1) if ( $vars{ 'help' } );
return (%vars);
}