File Coverage

blib/lib/Tail/Tool/Plugin/GroupLines.pm
Criterion Covered Total %
statement 18 43 41.8
branch 0 16 0.0
condition 0 8 0.0
subroutine 6 8 75.0
pod 1 1 100.0
total 25 76 32.8


line stmt bran cond sub pod time code
1             package Tail::Tool::Plugin::GroupLines;
2              
3             # Created on: 2011-04-04 14:42:01
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 1     1   1706 use Moose;
  1         3  
  1         5  
10 1     1   5939 use warnings;
  1         2  
  1         25  
11 1     1   5 use version;
  1         3  
  1         8  
12 1     1   80 use Carp;
  1         2  
  1         69  
13 1     1   5 use English qw/ -no_match_vars /;
  1         2  
  1         7  
14 1     1   379 use AnyEvent;
  1         4  
  1         422  
15              
16             extends 'Tail::Tool::PreProcess';
17             with 'Tail::Tool::RegexList';
18              
19             our $VERSION = version->new('0.4.8');
20              
21             has end => (
22             is => 'rw',
23             isa => 'Bool',
24             );
25             has allow_empty => (
26             is => 'rw',
27             isa => 'Bool',
28             default => 1,
29             );
30             has files => (
31             is => 'rw',
32             isa => 'HashRef[HashRef]',
33             );
34              
35             sub process {
36 0     0 1   my ($self, $line, $file) = @_;
37              
38 0 0         if ( !$self->files ) {
39 0           $self->files({});
40             }
41              
42 0 0         my $match = grep {my $r = $_->regex; $_->enabled && $line =~ /$r/ } @{ $self->regex };
  0            
  0            
  0            
43              
44 0 0 0       if ( $match || $self->files->{$file->name}{show} ) {
45 0 0         if ( $self->end ) {
46 0           $line = "$self->files->{$file->name}{line}$line";
47 0           $self->files->{$file->name}{line} = '';
48             }
49             else {
50 0           my $new_line = $self->files->{$file->name}{line};
51 0           $self->files->{$file->name}{line} = $line;
52 0           $line = $new_line;
53             }
54 0 0         undef $self->files->{$file->name}{watcher} if !$self->files->{$file->name}{show};
55 0           undef $self->files->{$file->name}{show};
56             }
57             else {
58 0           $self->files->{$file->name}{line} .= $line;
59 0           $line = undef;
60 0 0 0       if ( !$self->end && !$self->files->{$file->name}{watcher} ) {
61             # create a timer that will cause log lines to be written 2s after
62             # they are first encounted when the match method is on the start of
63             # the line, other wise these lines wont be shown until the next line
64             # is found which may be some time.
65 0   0       my $size = $file->size || 0;
66             $self->files->{$file->name}{watcher} = AE::timer 2, 0, sub {
67 0     0     $self->allow_empty(1);
68 0           $self->files->{$file->name}{show} = 1;
69 0 0         $file->run() if $file->size >= $size;
70 0           };
71             }
72             }
73              
74 0 0         return defined $line ? ($line) : ();
75             }
76              
77             1;
78              
79             __END__
80              
81             =head1 NAME
82              
83             Tail::Tool::Plugin::GroupLines - Groups real lines of a log file so that other plugins treat then as one line.
84              
85             =head1 VERSION
86              
87             This documentation refers to Tail::Tool::Plugin::GroupLines version 0.4.8.
88              
89             =head1 SYNOPSIS
90              
91             use Tail::Tool::Plugin::GroupLines;
92              
93             # Brief but working code example(s) here showing the most common usage(s)
94             # This section will be as far as many users bother reading, so make it as
95             # educational and exemplary as possible.
96              
97              
98             =head1 DESCRIPTION
99              
100             Groups lines together so that they are presented as one line to other plugins.
101             It does this by checking each real log file line and checking if it matches
102             regex to indicate the start of a line (or if end is true the regex matches
103             the end of lines).
104              
105             An example of why you might want to do this:
106              
107             A log file with long data dumps that span many lines may be hard to filter out
108             but if those lines can be grouped together then only part of the line need
109             match for all lines to be excluded.
110              
111             =head1 SUBROUTINES/METHODS
112              
113             =head2 C<process ($line, $file)>
114              
115             =head1 DIAGNOSTICS
116              
117             =head1 CONFIGURATION AND ENVIRONMENT
118              
119             =head1 DEPENDENCIES
120              
121             =head1 INCOMPATIBILITIES
122              
123             =head1 BUGS AND LIMITATIONS
124              
125             There are no known bugs in this module.
126              
127             Please report problems to Ivan Wills (ivan.wills@gmail.com).
128              
129             Patches are welcome.
130              
131             =head1 AUTHOR
132              
133             Ivan Wills - (ivan.wills@gmail.com)
134              
135             =head1 LICENSE AND COPYRIGHT
136              
137             Copyright (c) 2011 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia).
138             All rights reserved.
139              
140             This module is free software; you can redistribute it and/or modify it under
141             the same terms as Perl itself. See L<perlartistic>. This program is
142             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
143             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
144             PARTICULAR PURPOSE.
145              
146             =cut