File: LogDiff.pm

package info (click to toggle)
libapache2-mod-perl2 2.0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 12,016 kB
  • sloc: perl: 97,771; ansic: 14,493; makefile: 51; sh: 18
file content (94 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (7)
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
# please insert nothing before this line: -*- mode: cperl; cperl-indent-level: 4; cperl-continued-statement-offset: 4; indent-tabs-mode: nil -*-
package TestCommon::LogDiff;

use strict;
use warnings FATAL => 'all';

use POSIX ();

sub new {
    my $class = shift;
    my $path  = shift;

    open my $fh, "<$path" or die "Can't open $path: $!";
    seek $fh, 0, POSIX::SEEK_END();
    my $pos = tell $fh;

    my %self = (
        path => $path,
        fh   => $fh,
        pos  => $pos,
    );

    return bless \%self, $class;
}

sub DESTROY {
    my $self = shift;
    close $self->{fh};
}

sub diff {
    my $self = shift;

    # XXX: is it possible that some system will be slow to flush the
    # buffers and we may need to wait a bit and retry if we see no new
    # logged data?
    my $fh = $self->{fh};
    seek $fh, $self->{pos}, POSIX::SEEK_SET(); # not really needed

    local $/; # slurp mode
    my $diff = <$fh>;
    seek $fh, 0, POSIX::SEEK_END();
    $self->{pos} = tell $fh;

    return defined $diff ? $diff : '';
}

1;

__END__

=head1 NAME

TestCommon::LogDiff - get log file diffs

=head1 Synopsis

  use TestCommon::LogDiff;
  use Apache::Test;

  plan tests => 2;

  my $path = "/tmp/mylog";
  open my $fh, ">>$path" or die "Can't open $path: $!";

  my $logdiff = TestCommon::LogDiff->new($path);

  print $fh "foo 123\n";
  my $expected = qr/^foo/;
  ok t_cmp $logdiff->diff, $expected;

  print $fh "bar\n";
  my $expected = 'bar';
  ok t_cmp $logdiff->diff, $expected;


=head1 Description

Useful for testing the warning, error and other messages going into
the log file.

=head1 API

=head2 new

open the log file and point the filehandle pointer to its end.

=head2 diff

extract any newly logged information since the last check and move the
filehandle to the end of the file.

=cut