line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
##################################################################### |
2
|
|
|
|
|
|
|
# This program is not guaranteed to work at all, and by using this # |
3
|
|
|
|
|
|
|
# program you release the author of any and all liability. # |
4
|
|
|
|
|
|
|
# # |
5
|
|
|
|
|
|
|
# You may use this code as long as you are in compliance with the # |
6
|
|
|
|
|
|
|
# license (see the LICENSE file) and this notice, disclaimer and # |
7
|
|
|
|
|
|
|
# comment box remain intact and unchanged. # |
8
|
|
|
|
|
|
|
# # |
9
|
|
|
|
|
|
|
# Package: Term::RouterCLI # |
10
|
|
|
|
|
|
|
# Class: Log # |
11
|
|
|
|
|
|
|
# Description: Methods for building a Router (Stanford) style CLI # |
12
|
|
|
|
|
|
|
# # |
13
|
|
|
|
|
|
|
# Written by: Bret Jordan (jordan at open1x littledot org) # |
14
|
|
|
|
|
|
|
# Created: 2011-04-26 # |
15
|
|
|
|
|
|
|
##################################################################### |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
package Term::RouterCLI::Log; |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
4
|
|
2014
|
use 5.8.8; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
229
|
|
23
|
4
|
|
|
4
|
|
20
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
104
|
|
24
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
90
|
|
25
|
4
|
|
|
4
|
|
21
|
use Term::RouterCLI::Debugger; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
105
|
|
26
|
4
|
|
|
4
|
|
20
|
use Log::Log4perl; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
43
|
|
27
|
4
|
|
|
4
|
|
181
|
use FileHandle; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
34
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
30
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $oDebugger = new Term::RouterCLI::Debugger(); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new |
37
|
|
|
|
|
|
|
{ |
38
|
6
|
|
|
6
|
0
|
13
|
my $pkg = shift; |
39
|
6
|
|
33
|
|
|
41
|
my $class = ref($pkg) || $pkg; |
40
|
|
|
|
|
|
|
|
41
|
6
|
|
|
|
|
11
|
my $self = {}; |
42
|
6
|
|
|
|
|
16
|
$self->{'_sName'} = $pkg; # Lets set the object name so we can use it in debugging |
43
|
6
|
|
|
|
|
18
|
bless ($self, $class); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Lets send any passed in arguments to the _init method |
46
|
6
|
|
|
|
|
36
|
$self->_init(@_); |
47
|
6
|
|
|
|
|
32
|
return $self; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _init |
51
|
|
|
|
|
|
|
{ |
52
|
6
|
|
|
6
|
|
11
|
my $self = shift; |
53
|
6
|
|
|
|
|
22
|
my %hParameters = @_; |
54
|
|
|
|
|
|
|
|
55
|
6
|
|
|
|
|
39
|
$self->{'_bEnabled'} = 1; |
56
|
6
|
|
|
|
|
13
|
$self->{'_oParent'} = undef; |
57
|
6
|
|
|
|
|
11
|
$self->{'_sDirectory'} = './logs/'; |
58
|
6
|
|
|
|
|
10
|
$self->{'_sFilename'} = undef; |
59
|
6
|
|
|
|
|
12
|
$self->{'_iFileLength'} = 500; |
60
|
6
|
|
|
|
|
15
|
$self->{'_iMaxFileLength'} = 50000; # Define an upper bound for sanity sakes |
61
|
6
|
|
|
|
|
14
|
$self->{'_oFileHandle'} = undef; |
62
|
6
|
|
|
|
|
15
|
$self->{'_aCurrentLogData'} = undef; |
63
|
6
|
|
|
|
|
11
|
$self->{'_iCurrentLogSize'} = undef; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Lets overwrite any defaults with values that are passed in |
66
|
6
|
50
|
|
|
|
28
|
if (%hParameters) |
67
|
|
|
|
|
|
|
{ |
68
|
6
|
|
|
|
|
16
|
foreach (keys (%hParameters)) { $self->{$_} = $hParameters{$_}; } |
|
12
|
|
|
|
|
36
|
|
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub DESTROY |
73
|
|
|
|
|
|
|
{ |
74
|
0
|
|
|
0
|
|
|
my $self = shift; |
75
|
0
|
|
|
|
|
|
$self = {}; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# ---------------------------------------- |
81
|
|
|
|
|
|
|
# Public Methods |
82
|
|
|
|
|
|
|
# ---------------------------------------- |
83
|
|
|
|
|
|
|
sub Enable |
84
|
|
|
|
|
|
|
{ |
85
|
|
|
|
|
|
|
# This method will enable this log method |
86
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
87
|
0
|
|
|
|
|
|
$self->{'_bEnabled'} = 1; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub Disable |
91
|
|
|
|
|
|
|
{ |
92
|
|
|
|
|
|
|
# This method will disable this log method |
93
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
$self->{'_bEnabled'} = 0; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub ExpandTildesInFilename |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
# This method will expand any tildes that are in the file name so that it will work right |
100
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
101
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
104
|
0
|
0
|
|
|
|
|
if (defined $self->{'_sFilename'}) |
105
|
|
|
|
|
|
|
{ |
106
|
0
|
0
|
0
|
|
|
|
$self->{'_sFilename'} =~ s/^~([^\/]*)/$1?(getpwnam($1))[7]:$ENV{HOME}||$ENV{LOGDIR}||(getpwuid($>))[7]/e; |
|
0
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
108
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub SetFilename |
112
|
|
|
|
|
|
|
{ |
113
|
|
|
|
|
|
|
# This method will set the filename for this logging method |
114
|
|
|
|
|
|
|
# Required: |
115
|
|
|
|
|
|
|
# string (filename) |
116
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
117
|
0
|
|
|
|
|
|
my $parameter = shift; |
118
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
121
|
0
|
0
|
|
|
|
|
if (defined $parameter) |
122
|
|
|
|
|
|
|
{ |
123
|
0
|
|
|
|
|
|
$self->{'_sFilename'} = $parameter; |
124
|
0
|
|
|
|
|
|
$self->ExpandTildesInFilename(); |
125
|
|
|
|
|
|
|
} |
126
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub SetFileLength |
130
|
|
|
|
|
|
|
{ |
131
|
|
|
|
|
|
|
# This method will set the length of the history file on disk which is limited to 50000 for sanity reasons |
132
|
|
|
|
|
|
|
# Required: |
133
|
|
|
|
|
|
|
# integer (length) |
134
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
135
|
0
|
|
|
|
|
|
my $parameter = shift; |
136
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
139
|
0
|
0
|
0
|
|
|
|
if (($parameter =~ /^\d+$/) && ($parameter > 0) && ($parameter < $self->{'_iMaxFileLength'})) |
|
|
|
0
|
|
|
|
|
140
|
|
|
|
|
|
|
{ |
141
|
0
|
|
|
|
|
|
$self->{'_iFileLength'} = $parameter; |
142
|
0
|
0
|
|
|
|
|
if ($self->{'_iFileLength'} eq $parameter) { $logger->info("File length value set to $parameter");} |
|
0
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
} |
144
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub SetCurrentLogSize |
148
|
|
|
|
|
|
|
{ |
149
|
|
|
|
|
|
|
# This method will capture the current size of the logging data array |
150
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
151
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
154
|
0
|
|
|
|
|
|
$self->{'_iCurrentLogSize'} = @{$self->{'_aCurrentLogData'}}; |
|
0
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - _iCurrentLogSize: $self->{'_iCurrentLogSize'}"); |
157
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub OpenFileHandle |
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
# This method will create a file handle for the audit log |
163
|
|
|
|
|
|
|
# Required: |
164
|
|
|
|
|
|
|
# string (handle type R=Read, W=Write, A=Append) |
165
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
166
|
0
|
|
|
|
|
|
my $parameter = shift; |
167
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
168
|
0
|
|
|
|
|
|
my $FILE = undef; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Make sure the file name and size have been defined |
173
|
0
|
0
|
0
|
|
|
|
if ((defined $self->{'_sFilename'}) && ($self->{'_iFileLength'} > 0)) |
174
|
|
|
|
|
|
|
{ |
175
|
|
|
|
|
|
|
# Open file depending on what we need. I tried to just use +>> but that does not truncate and clean |
176
|
|
|
|
|
|
|
# out the file so it makes it so I can not purge old data |
177
|
0
|
0
|
|
|
|
|
if ($parameter eq "W") |
|
|
0
|
|
|
|
|
|
178
|
|
|
|
|
|
|
{ |
179
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - Opening file hand for writing"); |
180
|
0
|
|
0
|
|
|
|
$FILE = new FileHandle(">$self->{'_sFilename'}") || warn "Can not open " . $self->{'_sFilename'} . " for writing $!\n"; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
elsif ($parameter eq "A") |
183
|
|
|
|
|
|
|
{ |
184
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - Opening file hand for appending"); |
185
|
0
|
|
0
|
|
|
|
$FILE = new FileHandle(">>$self->{'_sFilename'}") || warn "Can not open " . $self->{'_sFilename'} . " for appending $!\n"; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
else |
188
|
|
|
|
|
|
|
{ |
189
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - Opening file hand for reading"); |
190
|
0
|
|
0
|
|
|
|
$FILE = new FileHandle("<$self->{'_sFilename'}") || warn "Can not open " . $self->{'_sFilename'} . " for reading $!\n"; |
191
|
|
|
|
|
|
|
} |
192
|
0
|
|
|
|
|
|
$FILE->autoflush(1); |
193
|
0
|
|
|
|
|
|
$self->{'_oFileHandle'} = \$FILE; |
194
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - _oFileHandle: ${$self->{'_oFileHandle'}}"); |
|
0
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
} |
196
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub CloseFileHandle |
200
|
|
|
|
|
|
|
{ |
201
|
|
|
|
|
|
|
# This method will close the file handle |
202
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
203
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
204
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
205
|
|
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
if (defined $self->{'_oFileHandle'}) |
207
|
|
|
|
|
|
|
{ |
208
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - _oFileHandle: ${$self->{'_oFileHandle'}}"); |
|
0
|
|
|
|
|
|
|
209
|
0
|
|
|
|
|
|
${$self->{'_oFileHandle'}}->close; |
|
0
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
} |
211
|
0
|
|
|
|
|
|
$self->{'_oFileHandle'} = undef; |
212
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub ReadLogFile |
216
|
|
|
|
|
|
|
{ |
217
|
|
|
|
|
|
|
# This method will read the current log file if it exists and we need to do this before |
218
|
|
|
|
|
|
|
# we open the standard file handle as it will be setup for writing, and this one is for reading. |
219
|
|
|
|
|
|
|
# Return: |
220
|
|
|
|
|
|
|
# 0 = nothing was read |
221
|
|
|
|
|
|
|
# 1 = a log file was read |
222
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
223
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
224
|
0
|
|
|
|
|
|
my $retval = 0; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# If the log file is already on the system lets read its current contents in to memory |
229
|
0
|
0
|
0
|
|
|
|
if ((defined $self->{'_sFilename'}) && (-r $self->{'_sFilename'})) |
230
|
|
|
|
|
|
|
{ |
231
|
0
|
|
|
|
|
|
$self->OpenFileHandle("R"); |
232
|
0
|
|
|
|
|
|
my $FILE = ${$self->{'_oFileHandle'}}; |
|
0
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
my @aCurrentLogData = <$FILE>; |
234
|
0
|
|
|
|
|
|
my @aNewLogData; |
235
|
0
|
|
|
|
|
|
foreach (@aCurrentLogData) |
236
|
|
|
|
|
|
|
{ |
237
|
0
|
|
|
|
|
|
chomp(); |
238
|
0
|
|
|
|
|
|
push(@aNewLogData,$_); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
|
$self->{'_aCurrentLogData'} = \@aNewLogData; |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - _aCurrentLogData: ", ${$oDebugger->DumpArray($self->{'_aCurrentLogData'})}); |
|
0
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# Lets capture the current log size so we have it |
247
|
0
|
|
|
|
|
|
$self->SetCurrentLogSize(); |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
$self->CloseFileHandle(); |
250
|
0
|
|
|
|
|
|
$retval = 1; |
251
|
|
|
|
|
|
|
} |
252
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
253
|
0
|
|
|
|
|
|
return $retval; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub WriteExistingLogData |
257
|
|
|
|
|
|
|
{ |
258
|
|
|
|
|
|
|
# This method will write out the existing log data to the file making sure we keep in mind the |
259
|
|
|
|
|
|
|
# file lengths |
260
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
261
|
0
|
|
|
|
|
|
my $logger = $oDebugger->GetLogger($self); |
262
|
|
|
|
|
|
|
|
263
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Entering Method ###'); |
264
|
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
|
$self->OpenFileHandle("W"); |
266
|
0
|
|
|
|
|
|
my $FILE = ${$self->{'_oFileHandle'}}; |
|
0
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - FILE: $FILE"); |
269
|
|
|
|
|
|
|
|
270
|
0
|
|
|
|
|
|
my $iArrayOffsetNumber = 0; |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
# If there are more lines in the log data than the max file length then we should only save so |
273
|
|
|
|
|
|
|
# many lines so lets back down from the end and set an offset from which to start so that we |
274
|
|
|
|
|
|
|
# are not starting from array index 0. This is needed as the newest commands are at the end of |
275
|
|
|
|
|
|
|
# the array/buffer |
276
|
0
|
0
|
|
|
|
|
if ($self->{'_iFileLength'} < $self->{'_iCurrentLogSize'}) |
277
|
|
|
|
|
|
|
{ |
278
|
0
|
|
|
|
|
|
$iArrayOffsetNumber = $self->{'_iCurrentLogSize'} - $self->{'_iFileLength'}; |
279
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - iArrayOffsetNumber: $iArrayOffsetNumber"); |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Since arrays start at zero, we need to minus one off the end of the History Buffer Size |
283
|
0
|
|
|
|
|
|
foreach ($iArrayOffsetNumber..$self->{'_iCurrentLogSize'}-1) |
284
|
|
|
|
|
|
|
{ |
285
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - aCurrentLogData: $self->{'_aCurrentLogData'}->[$_]"); |
286
|
0
|
|
|
|
|
|
print $FILE "$self->{'_aCurrentLogData'}->[$_]\n"; |
287
|
|
|
|
|
|
|
} |
288
|
0
|
|
|
|
|
|
$self->CloseFileHandle(); |
289
|
0
|
|
|
|
|
|
$logger->debug("$self->{'_sName'} - ", '### Leaving Method ###'); |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
sub ClearExistingLogData |
293
|
|
|
|
|
|
|
{ |
294
|
|
|
|
|
|
|
# This method will clear out all existing log data from the array_ref in memory |
295
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
296
|
0
|
|
|
|
|
|
$self->{'_aCurrentLogData'} = undef; |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
return 1; |