File Coverage

blib/lib/Labyrinth/Audit.pm
Criterion Covered Total %
statement 52 56 92.8
branch 23 38 60.5
condition 4 9 44.4
subroutine 11 12 91.6
pod 7 7 100.0
total 97 122 79.5


line stmt bran cond sub pod time code
1             package Labyrinth::Audit;
2              
3 16     16   130273 use warnings;
  16         23  
  16         438  
4 16     16   54 use strict;
  16         13  
  16         407  
5              
6 16     16   50 use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
  16         16  
  16         1904  
7             $VERSION = '5.32';
8              
9             =head1 NAME
10              
11             Labyrinth::Audit - Audit Handler for Labyrinth.
12              
13             =head1 SYNOPSIS
14              
15             use Labyrinth::Audit;
16              
17             SetLogFile(%hash);
18             LogRecord($level,@args);
19              
20             # examples
21             SetLogFile(
22             FILE => $logfile,
23             USER => $username,
24             LEVEL => $LOG_LEVEL_INFO,
25             CLEAR => 1,
26             CALLER => 1
27             );
28              
29             LogRecord($LOG_LEVEL_INFO,'Process Started');
30              
31             =head1 DESCRIPTION
32              
33             The Audit package contains a number of variables and functions that can be
34             used within the framework to provide error, debugging and trace information.
35              
36             =head1 EXPORT
37              
38             DumpToFile
39              
40             SetLogFile
41             LogRecord
42              
43             LogError
44             LogWarning
45             LogInfo
46             LogDebug
47              
48             $LOG_LEVEL_ERROR
49             $LOG_LEVEL_WARN
50             $LOG_LEVEL_INFO
51             $LOG_LEVEL_DEBUG
52              
53             $LOG_LEVEL
54              
55             =cut
56              
57             # -------------------------------------
58             # Export Details
59              
60             require Exporter;
61             @ISA = qw(Exporter);
62              
63             %EXPORT_TAGS = (
64             'all' => [ qw(
65             DumpToFile
66             SetLogFile LogRecord
67             LogError LogWarning LogInfo LogDebug
68             $LOG_LEVEL_DEBUG $LOG_LEVEL_INFO
69             $LOG_LEVEL_WARN $LOG_LEVEL_ERROR
70             $LOG_LEVEL
71             ) ],
72             );
73              
74             @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );
75             @EXPORT = ( @{$EXPORT_TAGS{'all'}} );
76              
77             # -------------------------------------
78             # Library Modules
79              
80 16     16   6350 use IO::File;
  16         97171  
  16         1507  
81 16     16   6562 use Log::LogLite;
  16         48030  
  16         9534  
82              
83             # -------------------------------------
84             # Variables
85              
86             # Log level constants
87             our $LOG_LEVEL_DEBUG = 4;
88             our $LOG_LEVEL_INFO = 3;
89             our $LOG_LEVEL_WARN = 2;
90             our $LOG_LEVEL_ERROR = 1;
91              
92             # Default log level (can be over-ridden by Labyrinth)
93             our $LOG_LEVEL = $LOG_LEVEL_ERROR;
94             our $VERBOSE = 0;
95             our $CALLER = 0;
96              
97             my $firstpass = 1;
98             my $logfile = undef;
99             my $username;
100              
101             # -------------------------------------
102             # The Subs
103              
104             =head1 FUNCTIONS
105              
106             =head2 Audit Log Handling
107              
108             Audit Log functions enable tracing of actions for a user at a given time.
109              
110             =over 4
111              
112             =item DumpToFile($file,@blocks)
113              
114             Writes blocks (separated by new lines) to the given file. Creates the file if
115             it doesn't exist, and overwrites if it does.
116              
117             =cut
118              
119             sub DumpToFile {
120 0     0 1 0 my $file = shift;
121              
122 0 0       0 my $fh = IO::File->new($file, 'w+') or return;
123 0         0 print $fh join("\n",@_) . "\n";
124 0         0 $fh->close;
125             }
126              
127             =item SetLogFile(%hash)
128              
129             Hash table entries can be as follows:
130              
131             FILE => $logfile,
132             USER => $username,
133             LEVEL => $LOG_LEVEL_INFO,
134             CLEAR => 1;
135              
136             Note that FILE and USER are mandatory.
137              
138             Sets the path of the file to be used as the log, together with the current
139             username accessing the application.
140              
141             Note that if there is any failure, such as no file access, the audit trail is
142             disabled.
143              
144             =cut
145              
146             sub SetLogFile {
147 4     4 1 7197 my %hash = @_;
148              
149 4 50       13 return unless($hash{FILE});
150 4 50       6 return unless($hash{USER});
151              
152 4 50       5 eval { if(!-e $hash{FILE}) { my $fh = IO::File->new("$hash{FILE}", 'w+'); $fh->close } };
  4         73  
  4         23  
  4         434  
153 4 50 33     92 return if($@ || ! -w $hash{FILE});
154              
155 4         5 $username = $hash{USER};
156 4 50       10 $LOG_LEVEL = $hash{LEVEL} if($hash{LEVEL});
157 4 50       5 $CALLER = 1 if($hash{CALLER});
158              
159 4 50       9 if($hash{CLEAR}) { my $fh = IO::File->new("$hash{FILE}", 'w+'); $fh->close }
  4         14  
  4         261  
160              
161 4         44 $logfile = Log::LogLite->new($hash{FILE},$LOG_LEVEL);
162 4 50       527 return unless($logfile);
163 4         81 $logfile->template( "[] \n" );
164             }
165              
166             =item LogRecord($level,@args)
167              
168             Record informational messages to Audit Log.
169              
170             =cut
171              
172             sub LogRecord {
173 18   66 18 1 63 my $level = shift || $LOG_LEVEL_DEBUG;
174 18         19 my $mess = '';
175              
176 18 50       25 return unless($logfile);
177              
178             {
179 18         11 local $" = ",";
  18         19  
180 18 50       42 $mess = "@_" if(@_);
181             }
182              
183 18         33 my $audit = "<:$username> [$level] $mess";
184              
185 18 50       22 if($CALLER) {
186 18         28 my $i = 1;
187 18         88 while(my @calls = caller($i++)) {;
188 10         41 $audit .= " => CALLER($calls[1],$calls[2])";
189             }
190             }
191              
192 18 50 33     34 print STDERR $mess . "\n" if($VERBOSE && $level <= $LOG_LEVEL_INFO);
193              
194 18 100       28 return if($level > $LOG_LEVEL);
195              
196 15 100       23 $logfile->write('-' x 40,$level) if($firstpass);
197 15         243 $logfile->write($audit,$level);
198 15         1711 $firstpass = 0;
199             }
200              
201             =item LogError(@args)
202              
203             Shorthand call for Error messages.
204              
205             =item LogWarning(@args)
206              
207             Shorthand call for Warning messages.
208              
209             =item LogInfo(@args)
210              
211             Shorthand call for Information messages.
212              
213             =item LogDebug(@args)
214              
215             Shorthand call for Debug messages.
216              
217             =cut
218              
219 4 50   4 1 18 sub LogError { LogRecord($LOG_LEVEL_ERROR,@_) if($LOG_LEVEL >= $LOG_LEVEL_ERROR); }
220 4 100   4 1 20 sub LogWarning { LogRecord($LOG_LEVEL_WARN ,@_) if($LOG_LEVEL >= $LOG_LEVEL_WARN ); }
221 4 100   4 1 22 sub LogInfo { LogRecord($LOG_LEVEL_INFO ,@_) if($LOG_LEVEL >= $LOG_LEVEL_INFO ); }
222 4 100   4 1 20 sub LogDebug { LogRecord($LOG_LEVEL_DEBUG,@_) if($LOG_LEVEL >= $LOG_LEVEL_DEBUG); }
223              
224             1;
225              
226             __END__