File Coverage

blib/lib/Dancer/Plugin/Tail.pm
Criterion Covered Total %
statement 23 27 85.1
branch n/a
condition n/a
subroutine 8 9 88.8
pod n/a
total 31 36 86.1


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Tail;
2              
3 1     1   17728 use 5.006;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         21  
5 1     1   3 use warnings FATAL => 'all';
  1         5  
  1         42  
6              
7 1     1   1988 use Dancer ':syntax';
  1         312578  
  1         8  
8 1     1   558 use Dancer::Exception ':all';
  1         4  
  1         207  
9 1     1   897 use Dancer::Plugin;
  1         1834  
  1         103  
10 1     1   9 use Dancer::Session;
  1         3  
  1         33  
11 1     1   758 use String::Random;
  1         3919  
  1         583  
12              
13              
14             my $STRRand = String::Random->new;
15              
16              
17             =head1 NAME
18              
19             Dancer::Plugin::Tail - Tail a file from Dancer
20              
21             =head1 VERSION
22              
23             Version 0.0003
24              
25             =cut
26              
27             our $VERSION = '0.0003';
28              
29             =head1 SYNOPSIS
30              
31             use Dancer;
32             use Dancer::Plugin::Tail;
33              
34              
35             =head1 DESCRIPTION
36              
37             This plugin will allow you to tail a file from within Dancer. It's designed to be unobtrusive. So, it is functional just by calling it from your scripts. Add or remove entries from the configuration file to activate or deactivate files that may be viewed. The plugin will dynamically generate a route in your application based on parameters from the configuration file.
38              
39              
40             =head1 CONFIGURATION
41              
42             plugins:
43             tail:
44             interval: 3000
45             tmpdir: '/tmp'
46             files:
47             id1: '/var/logs/access_log'
48             id2: '/var/logs/error_log'
49              
50             You may specify the route and access to files. The plugin will only read files so it must have read access to them. The above configuration will generate '/showittome' as a route.
51              
52             A sample HTML page is included in the samples directory. Use it as an example to build your own page.
53              
54              
55             =cut
56              
57             # Generate a route based on configuration settings
58              
59             my $conf = plugin_setting;
60              
61             get '/tail/:id/:curr_pos' => sub {
62              
63             my $id = params->{id}; # Id of file
64             my $curr_pos = params->{curr_pos} || 0; # Start reading from here
65              
66             my $log_file = '';
67              
68             if ( defined $conf->{files}->{$id} ) { # is it predefined?
69             $log_file = $conf->{files}->{$id};
70             } elsif ( session->{'tail_' . $id} ) { # is it a temp file?
71             $log_file = session->{'tail_' . $id};
72             } else {
73             send_error('404');
74             }
75              
76             # Only do it if the file is defined
77             if ( $log_file ne '' ) {
78              
79             debug "Tail file:$log_file";
80              
81             my ($output, $whence);
82              
83             open(my $IN, '<', $log_file); # Open file for reading
84              
85             # Add header if it's 1st request
86             if ( $curr_pos < 1 ) {
87             $output = "$log_file\n";
88             }
89              
90             # Determine where to start reading
91             if ( $curr_pos < 0 ) {
92             $whence = 2; # Relative to current position
93             } else {
94             $whence = 1; # Absolute current position
95             }
96              
97             seek( $IN, $curr_pos, $whence ); # Seek the place where we were last
98             while ( my $line = <$IN> ) { # Continue until end
99             $output .= $line ;
100             }
101              
102             my $file_end = tell($IN); # Figure out the end of the file
103             debug "File End: $file_end";
104             close($IN);
105              
106             # Return JSON
107             to_json( { new_curr_pos => $file_end,
108             interval => $conf->{interval},
109             output => $output } );
110             }
111             };
112              
113             =head1 C
114              
115             =cut
116              
117             register temp_file_to_tail => sub {
118 0     0     my $file_id = $STRRand->randregex( '[A-Za-z]{16}' ) . time;
119              
120 0           my $log_file = $conf->{tmpdir} . '/' . $file_id ;
121 0           session 'tail_' . $file_id => $log_file;
122              
123 0           return $file_id;
124             };
125              
126             register_plugin;
127              
128             =head1 AUTHOR
129              
130             Hagop "Jack" Bilemjian, C<< >>
131              
132             =head1 BUGS
133              
134             Please report any bugs or feature requests to C, or through
135             the web interface at L. I will be notified, and then you'll
136             automatically be notified of progress on your bug as I make changes.
137              
138              
139              
140              
141             =head1 SUPPORT
142              
143             You can find documentation for this module with the perldoc command.
144              
145             perldoc Dancer::Plugin::Tail
146              
147              
148             You can also look for information at:
149              
150             =over 4
151              
152             =item * RT: CPAN's request tracker (report bugs here)
153              
154             L
155              
156             =item * AnnoCPAN: Annotated CPAN documentation
157              
158             L
159              
160             =item * CPAN Ratings
161              
162             L
163              
164             =item * Search CPAN
165              
166             L
167              
168             =back
169              
170              
171             =head1 ACKNOWLEDGEMENTS
172              
173              
174             =head1 LICENSE AND COPYRIGHT
175              
176             Copyright 2015 Hagop "Jack" Bilemjian.
177              
178             This program is free software; you can redistribute it and/or modify it
179             under the terms of the the Artistic License (2.0). You may obtain a
180             copy of the full license at:
181              
182             L
183              
184             Any use, modification, and distribution of the Standard or Modified
185             Versions is governed by this Artistic License. By using, modifying or
186             distributing the Package, you accept this license. Do not use, modify,
187             or distribute the Package, if you do not accept this license.
188              
189             If your Modified Version has been derived from a Modified Version made
190             by someone other than you, you are nevertheless required to ensure that
191             your Modified Version complies with the requirements of this license.
192              
193             This license does not grant you the right to use any trademark, service
194             mark, tradename, or logo of the Copyright Holder.
195              
196             This license includes the non-exclusive, worldwide, free-of-charge
197             patent license to make, have made, use, offer to sell, sell, import and
198             otherwise transfer the Package with respect to any patent claims
199             licensable by the Copyright Holder that are necessarily infringed by the
200             Package. If you institute patent litigation (including a cross-claim or
201             counterclaim) against any party alleging that the Package constitutes
202             direct or contributory patent infringement, then this Artistic License
203             to you shall terminate on the date that such litigation is filed.
204              
205             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
206             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
207             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
208             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
209             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
210             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
211             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
212             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
213              
214              
215             =head1 SEE ALSO
216            
217             L
218            
219             L
220            
221             =cut
222              
223             1; # End of Dancer::Plugin::Tail
224              
225              
226              
227              
228