line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::Tail; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
643234
|
use Dancer2::Core::Types qw(Bool HashRef Str); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
1567
|
use Dancer2::Plugin; |
|
1
|
|
|
|
|
11420
|
|
|
1
|
|
|
|
|
6
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
2795
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
7
|
1
|
|
|
1
|
|
477
|
use Session::Token; |
|
1
|
|
|
|
|
7607
|
|
|
1
|
|
|
|
|
1181
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Dancer2::Plugin::Tail - Tail a file from Dancer2 |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.016 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.016'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Dancer2; |
26
|
|
|
|
|
|
|
use Dancer2::Plugin::Tail; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This plugin will allow you to tail a file from within Dancer2. It's designed to be unobtrusive. So, it is functional just by calling it from your scripts. Edit entries in the Dancer configuration to setup routes and activate files that may be tailed. Additionally, you may define or restrict definition of tailed files. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 CONFIGURATION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
You may specify the route to files. The plugin will only read files so Dancer2 must have read access to them. The following configuration will generate two routes: '/tail/display' and '/tail/read'. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
A sample HTML page with Bootstrap and jQuery is included in the samples directory. Use it as an example to build your own page. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
plugins: |
41
|
|
|
|
|
|
|
Tail: |
42
|
|
|
|
|
|
|
update_interval: 3000 |
43
|
|
|
|
|
|
|
stop_on_empty_cnt: 5 |
44
|
|
|
|
|
|
|
tmpdir: '/tmp' |
45
|
|
|
|
|
|
|
display: |
46
|
|
|
|
|
|
|
method: 'get' |
47
|
|
|
|
|
|
|
url: '/tail/display' |
48
|
|
|
|
|
|
|
template: 'tail.tt' |
49
|
|
|
|
|
|
|
layout: 'nomenu.tt' |
50
|
|
|
|
|
|
|
data: |
51
|
|
|
|
|
|
|
method: 'get' |
52
|
|
|
|
|
|
|
url: '/tail/read' |
53
|
|
|
|
|
|
|
files: |
54
|
|
|
|
|
|
|
id1: |
55
|
|
|
|
|
|
|
heading: 'Server Access Log' |
56
|
|
|
|
|
|
|
file: '/var/logs/access_log' |
57
|
|
|
|
|
|
|
id2: |
58
|
|
|
|
|
|
|
heading: 'Server Error Log' |
59
|
|
|
|
|
|
|
file: '/var/logs/error_log' |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item I |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Specify an update interval. Default is 3 seconds (3000). This value is passed to your web page or window. See example that's included. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item I |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Specify the number of empty responses before stopping. Default is 10. This value is passed to your web page or window. See example that's included. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item I |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
location of user generted files to tail. Default is '/tmp'. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item I |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Defines display settings. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=over 4 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item I |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Default 'get'. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item I |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Route in Dancer2 to display template for tailing. |
92
|
|
|
|
|
|
|
Default = '/tail/display' |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item I |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Template of tail screen. |
98
|
|
|
|
|
|
|
Default = 'tail.tt' |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item I |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Layout of template. This is useful when opening a window to tail files. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=back |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=item I |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Defines file tail settings. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over 4 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item I |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Default 'get'. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item I |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Route in Dancer2 to tail files. |
121
|
|
|
|
|
|
|
Default = '/tail/read' |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=back |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item I |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
List of predefined files that can be tailed. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=over 4 |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item I |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Define a unique ID for this file |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=over 4 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item I |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This is a heading or title of the html page to be passed to the template. Use it as a short description to the file you're taiing. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item I |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Full path and file name to tail. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=back |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
B that you B have a session provider configured to dynamically tail files using this plugin. This plugin requires sessions in order to track information about user defined tailed files for the logged in user. |
149
|
|
|
|
|
|
|
Please see L for information on how to configure session management within your application. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=back |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=back |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 display_tail |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This function displays the specified template with the data from the configuration file. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 define_file_to_tail |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
A function called to dynamically define a file to tail. This is useful for launching long running applications and having their out put tailed. In general, this will generate a 32 character string that you should use to direct the output of your process. Then, Dancer2::Plugin::Tail will tail the content of this file. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 _tail_the_file |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This private function does the actual job of tailing the file. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 _new_file_id |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
An internal functio that returns a 32 character string when called from define_file_to_tail. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# |
179
|
|
|
|
|
|
|
# Accessors |
180
|
|
|
|
|
|
|
# |
181
|
|
|
|
|
|
|
has update_interval => ( |
182
|
|
|
|
|
|
|
is => 'ro', |
183
|
|
|
|
|
|
|
isa => Str, |
184
|
|
|
|
|
|
|
from_config => 1, |
185
|
|
|
|
|
|
|
default => sub { '3000' }, # 3 second interval |
186
|
|
|
|
|
|
|
); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
has stop_on_empty_cnt => ( |
189
|
|
|
|
|
|
|
is => 'ro', |
190
|
|
|
|
|
|
|
isa => Str, |
191
|
|
|
|
|
|
|
from_config => 1, |
192
|
|
|
|
|
|
|
default => sub { '10' }, # 10 Empty or 30 secs i |
193
|
|
|
|
|
|
|
# (update_interval * stop_on_empty_cnt) = 30 |
194
|
|
|
|
|
|
|
); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
has data_method => ( |
197
|
|
|
|
|
|
|
is => 'ro', |
198
|
|
|
|
|
|
|
isa => Str, |
199
|
|
|
|
|
|
|
from_config => 'data.method', |
200
|
|
|
|
|
|
|
default => sub { 'get' } |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
has data_url => ( |
204
|
|
|
|
|
|
|
is => 'ro', |
205
|
|
|
|
|
|
|
isa => Str, |
206
|
|
|
|
|
|
|
from_config => 'data.url', |
207
|
|
|
|
|
|
|
default => sub { '/tail/read' } |
208
|
|
|
|
|
|
|
); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
has display_method => ( |
211
|
|
|
|
|
|
|
is => 'ro', |
212
|
|
|
|
|
|
|
isa => Str, |
213
|
|
|
|
|
|
|
from_config => 'display.method', |
214
|
|
|
|
|
|
|
default => sub { 'get' } |
215
|
|
|
|
|
|
|
); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
has display_url => ( |
218
|
|
|
|
|
|
|
is => 'ro', |
219
|
|
|
|
|
|
|
isa => Str, |
220
|
|
|
|
|
|
|
from_config => 'display.url', |
221
|
|
|
|
|
|
|
default => sub { '/tail/display' }, |
222
|
|
|
|
|
|
|
); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
has display_template => ( |
225
|
|
|
|
|
|
|
is => 'ro', |
226
|
|
|
|
|
|
|
isa => Str, |
227
|
|
|
|
|
|
|
from_config => 'display.template', |
228
|
|
|
|
|
|
|
default => sub { 'tail.tt' }, |
229
|
|
|
|
|
|
|
); |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
has display_layout => ( |
232
|
|
|
|
|
|
|
is => 'ro', |
233
|
|
|
|
|
|
|
isa => Str, |
234
|
|
|
|
|
|
|
from_config => 'display.layout', |
235
|
|
|
|
|
|
|
default => sub { '' }, |
236
|
|
|
|
|
|
|
); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
has tmpdir => ( |
239
|
|
|
|
|
|
|
is => 'ro', |
240
|
|
|
|
|
|
|
isa => Str, |
241
|
|
|
|
|
|
|
from_config => 1, |
242
|
|
|
|
|
|
|
default => sub { '/tmp' }, # write to /tmp |
243
|
|
|
|
|
|
|
); |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
has files => ( |
246
|
|
|
|
|
|
|
is => 'ro', |
247
|
|
|
|
|
|
|
isa => HashRef, |
248
|
|
|
|
|
|
|
from_config => 1, |
249
|
|
|
|
|
|
|
default => sub { {} }, # Empty |
250
|
|
|
|
|
|
|
); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub _new_file_id { |
253
|
0
|
|
|
0
|
|
0
|
Session::Token->new( length => 32 )->get; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# Generate routes based on configuration settings |
257
|
|
|
|
|
|
|
sub BUILD { |
258
|
1
|
|
|
1
|
0
|
5614
|
my $plugin = shift; |
259
|
1
|
|
|
|
|
12
|
my $app = $plugin->app; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
# Setup route to display a template for the tail |
262
|
1
|
|
|
|
|
21
|
my $disp_method = $plugin->display_method; |
263
|
1
|
|
|
|
|
39
|
my $disp_url = $plugin->display_url; |
264
|
|
|
|
|
|
|
|
265
|
1
|
|
|
|
|
22
|
$plugin->app->log( debug => "Adding Route "); |
266
|
1
|
|
|
|
|
36816
|
$plugin->app->log( debug => " Method " . $disp_method); |
267
|
1
|
|
|
|
|
456
|
$plugin->app->log( debug => " Regexp " . $disp_url); |
268
|
1
|
|
|
|
|
468
|
$plugin->app->add_route( |
269
|
|
|
|
|
|
|
method => $disp_method, |
270
|
|
|
|
|
|
|
regexp => qr!$disp_url!, |
271
|
|
|
|
|
|
|
code => \&display_tail, |
272
|
|
|
|
|
|
|
); |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
# Setup a route to return json data of the file |
275
|
1
|
|
|
|
|
4020
|
my $data_url = $plugin->data_url; |
276
|
1
|
|
|
|
|
39
|
my $data_method = $plugin->data_method; |
277
|
|
|
|
|
|
|
|
278
|
1
|
|
|
|
|
27
|
$plugin->app->log( debug => "Adding Route "); |
279
|
1
|
|
|
|
|
438
|
$plugin->app->log( debug => " Method " . $data_method); |
280
|
1
|
|
|
|
|
419
|
$plugin->app->log( debug => " Regexp " . $data_url); |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Use regexp to match part of the file, then splat inside code |
283
|
1
|
|
|
|
|
424
|
$plugin->app->add_route( |
284
|
|
|
|
|
|
|
method => $data_method, |
285
|
|
|
|
|
|
|
regexp => qr!^$data_url!, |
286
|
|
|
|
|
|
|
code => \&_tail_the_file, |
287
|
|
|
|
|
|
|
); |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
} ### BUILD |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
# Function to display template |
293
|
|
|
|
|
|
|
sub display_tail { |
294
|
0
|
|
|
0
|
0
|
|
my $app = shift; |
295
|
0
|
|
|
|
|
|
my $plugin = $app->with_plugin('Tail'); |
296
|
|
|
|
|
|
|
|
297
|
0
|
|
|
|
|
|
my $error = undef; |
298
|
0
|
|
|
|
|
|
my $params = $app->request->params; |
299
|
|
|
|
|
|
|
|
300
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "Params:"); |
301
|
0
|
|
|
|
|
|
$plugin->app->log( debug => $params ); |
302
|
|
|
|
|
|
|
|
303
|
0
|
|
|
|
|
|
my $tail_file_id = $params->{'tail_file_id'}; ### ID in config |
304
|
0
|
|
0
|
|
|
|
my $curr_pos = $params->{'curr_pos'} || 0; ### Current position to read from |
305
|
|
|
|
|
|
|
|
306
|
0
|
|
|
|
|
|
my $tail_id = 'tail-' . $tail_file_id; ### ID from session |
307
|
|
|
|
|
|
|
|
308
|
0
|
|
|
|
|
|
my $file = undef; |
309
|
0
|
|
|
|
|
|
my $tail_file = undef; |
310
|
0
|
|
|
|
|
|
my $tail_heading = undef; |
311
|
0
|
|
|
|
|
|
my $files = $plugin->files; |
312
|
|
|
|
|
|
|
|
313
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "Files:" ); |
314
|
0
|
|
|
|
|
|
$plugin->app->log( debug => $files ); |
315
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "File id:" . $tail_file_id ); |
316
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "tail id:" . $tail_id ); |
317
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "if " . $files->{$tail_file_id} ); |
318
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "file:" . $files->{$tail_file_id}->{file} ); |
319
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "heading" . $files->{$tail_file_id}->{heading} ); |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# Predefined |
322
|
0
|
0
|
|
|
|
|
if ( $files->{$tail_file_id} ) { |
323
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "Predefined file_id=" . $tail_file_id ); |
324
|
0
|
|
|
|
|
|
$tail_file = $files->{$tail_file_id}->{file} ; |
325
|
0
|
|
|
|
|
|
$tail_heading = $files->{$tail_file_id}->{heading}; |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
# User defined |
328
|
|
|
|
|
|
|
} else { |
329
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "User defined tail_id=" . $tail_id ); |
330
|
0
|
|
|
|
|
|
my $file = $plugin->app->session->read($tail_id); |
331
|
0
|
|
|
|
|
|
$tail_file = $file->{file}; |
332
|
0
|
|
|
|
|
|
$tail_heading = $file->{heading}; |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
|
335
|
0
|
0
|
0
|
|
|
|
if ( ! defined $tail_file |
336
|
|
|
|
|
|
|
|| $tail_file eq '' ) { |
337
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "file id " . $file_id . " is not defined." ); |
338
|
0
|
|
|
|
|
|
$error = 'The file-id you specified does not exist.' ; |
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
|
341
|
0
|
|
|
|
|
|
$app->template($plugin->display_template, |
342
|
|
|
|
|
|
|
{ tail_file_id => $tail_file_id, |
343
|
|
|
|
|
|
|
curr_pos => $curr_pos, |
344
|
|
|
|
|
|
|
heading => $tail_heading, |
345
|
|
|
|
|
|
|
data_method => $plugin->data_method, |
346
|
|
|
|
|
|
|
data_url => $plugin->data_url, |
347
|
|
|
|
|
|
|
update_interval => $plugin->update_interval, |
348
|
|
|
|
|
|
|
stop_on_empty_cnt => $plugin->stop_on_empty_cnt, |
349
|
|
|
|
|
|
|
error => $error }, |
350
|
|
|
|
|
|
|
{ layout => $plugin->display_layout }) ; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
# Function for a user to dynamically define a file |
354
|
|
|
|
|
|
|
sub define_file_to_tail { |
355
|
0
|
|
|
0
|
0
|
|
my ( $plugin, $tail ) = @_; |
356
|
|
|
|
|
|
|
|
357
|
0
|
|
|
|
|
|
my $file_id = _new_file_id(); # Create a new file id |
358
|
|
|
|
|
|
|
|
359
|
0
|
0
|
|
|
|
|
$tail->{heading} = ' ' if ( ! defined $tail->{heading} ); |
360
|
0
|
0
|
|
|
|
|
if ( ! defined $tail->{file} ) { |
361
|
0
|
|
|
|
|
|
$tail->{file} = $plugin->tmpdir . '/' . $file_id ; |
362
|
|
|
|
|
|
|
} else { |
363
|
0
|
0
|
|
|
|
|
if ( ! -e $tail->{file} ) { |
364
|
0
|
|
|
|
|
|
open( my $touchfile, ">>", $tail->{file} ) ; |
365
|
0
|
|
|
|
|
|
print $touchfile " "; |
366
|
0
|
|
|
|
|
|
close($touchfile); |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
|
|
370
|
0
|
|
|
|
|
|
my $tail_id = 'tail-' . $file_id ; |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
# Store the file name into session |
373
|
0
|
|
|
|
|
|
$plugin->app->session->write( $tail_id => $tail ); |
374
|
|
|
|
|
|
|
|
375
|
0
|
|
|
|
|
|
return $file_id; |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
# Function to tail a file |
379
|
|
|
|
|
|
|
sub _tail_the_file { |
380
|
0
|
|
|
0
|
|
|
my $app = shift; |
381
|
0
|
|
|
|
|
|
my $plugin = $app->with_plugin('Tail'); |
382
|
|
|
|
|
|
|
|
383
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "In _tail_the_file " ); |
384
|
|
|
|
|
|
|
|
385
|
0
|
|
|
|
|
|
my $params = $app->request->params; |
386
|
|
|
|
|
|
|
|
387
|
0
|
|
|
|
|
|
my $tail_file_id = $params->{'tail_file_id'}; |
388
|
0
|
|
|
|
|
|
my $curr_pos = $params->{'curr_pos'}; |
389
|
|
|
|
|
|
|
|
390
|
0
|
|
|
|
|
|
my $tail_id = 'tail-' . $tail_file_id ; |
391
|
|
|
|
|
|
|
|
392
|
0
|
|
|
|
|
|
my $files = $plugin->files; |
393
|
0
|
|
|
|
|
|
my $file = undef; |
394
|
0
|
|
|
|
|
|
my $tail_file = undef; |
395
|
0
|
|
|
|
|
|
my $tail_heading = undef; |
396
|
|
|
|
|
|
|
|
397
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "files " ); |
398
|
0
|
|
|
|
|
|
$plugin->app->log( debug => $files ); |
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
# Predefined |
401
|
0
|
0
|
|
|
|
|
if ( $files->{$tail_file_id} ) { |
402
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "Predefined " . $tail_file_id ); |
403
|
0
|
|
|
|
|
|
$tail_file = $files->{$tail_file_id}->{file} ; |
404
|
0
|
|
|
|
|
|
$tail_heading = $files->{$tail_file_id}->{heading}; |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
# User defined |
407
|
|
|
|
|
|
|
} else { |
408
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "User defined " . $tail_id ); |
409
|
0
|
|
|
|
|
|
my $file = $plugin->app->session->read($tail_id); |
410
|
0
|
|
|
|
|
|
$tail_file = $file->{file}; |
411
|
0
|
|
|
|
|
|
$tail_heading = $file->{heading}; |
412
|
|
|
|
|
|
|
} |
413
|
|
|
|
|
|
|
|
414
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "File to tail is " . $tail_file ); |
415
|
0
|
0
|
0
|
|
|
|
if ( $tail_file |
416
|
|
|
|
|
|
|
&& -e $tail_file ) { |
417
|
|
|
|
|
|
|
|
418
|
0
|
|
|
|
|
|
my ($output, $whence); |
419
|
|
|
|
|
|
|
|
420
|
0
|
|
|
|
|
|
open(my $IN, '<', $tail_file); # Open file for reading |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
# Add header if it's 1st request |
423
|
0
|
0
|
|
|
|
|
if ( $curr_pos < 1 ) { |
424
|
0
|
|
|
|
|
|
$output = "$tail_file\n"; |
425
|
|
|
|
|
|
|
} |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
# Determine where to start reading |
428
|
0
|
0
|
|
|
|
|
if ( $curr_pos < 0 ) { |
429
|
0
|
|
|
|
|
|
$whence = 2; # Relative to current position |
430
|
|
|
|
|
|
|
} else { |
431
|
0
|
|
|
|
|
|
$whence = 1; # Absolute current position |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
|
434
|
0
|
|
|
|
|
|
seek( $IN, $curr_pos, $whence ); # Seek the place |
435
|
|
|
|
|
|
|
# where we were last |
436
|
0
|
|
|
|
|
|
while ( my $line = <$IN> ) { # Continue until end |
437
|
0
|
|
|
|
|
|
$output .= $line ; |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
|
440
|
0
|
|
|
|
|
|
my $file_end = tell($IN); # Figure out the end of the file |
441
|
0
|
|
|
|
|
|
close($IN); |
442
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "Returning JSON:" ); |
443
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "new_curr_pos " . $file_end); |
444
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "interval " . $plugin->update_interval); |
445
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "output " . $output ); |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
# Return JSON |
449
|
0
|
|
|
|
|
|
$app->send_as( JSON => { new_curr_pos => $file_end, |
450
|
|
|
|
|
|
|
interval => $plugin->update_interval, |
451
|
|
|
|
|
|
|
output => $output } ); |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
} else { ### if -e |
454
|
0
|
|
|
|
|
|
$plugin->app->log( debug => "No file to tail or file does not exist " . $tail_file ); |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
# setup keywords |
460
|
|
|
|
|
|
|
plugin_keywords qw( define_file_to_tail ); |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
=head1 AUTHOR |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
Hagop "Jack" Bilemjian, C<< >> |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
=head1 SUPPORT |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
perldoc Dancer2::Plugin::Tail |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
You can also look for information at: |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
=over |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
=item * Report bugs on github |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
L |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
L |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
=item * CPAN Ratings |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
L |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
=item * Search metaCPAN |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
L |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
=back |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
Copyright 2016-2017 Hagop "Jack" Bilemjian. |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
502
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
503
|
|
|
|
|
|
|
copy of the full license at: |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
L |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
508
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
509
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
510
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
513
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
514
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
517
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
518
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
520
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
521
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
522
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
523
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
524
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
525
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
526
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
529
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
530
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
531
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
532
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
533
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
534
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
535
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=head1 SEE ALSO |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
L |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
L |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=cut |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
1; # End of Dancer2::Plugin::Tail |
548
|
|
|
|
|
|
|
|