| 1 | package Labyrinth::Audit; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | $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 | 16 | require Exporter; | |||||||||||||||||||||||||||
| 61 | 16 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 16 | %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 | 16 | @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} ); | |||||||||||||||||||||||||||
| 75 | 16 | @EXPORT = ( @{$EXPORT_TAGS{'all'}} ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 78 | # Library Modules | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 16 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 16 | ▶ | use Log::LogLite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 84 | # Variables | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Log level constants | ||||||||||||||||||||||||||||
| 87 | 16 | our $LOG_LEVEL_DEBUG = 4; | |||||||||||||||||||||||||||
| 88 | 16 | our $LOG_LEVEL_INFO = 3; | |||||||||||||||||||||||||||
| 89 | 16 | our $LOG_LEVEL_WARN = 2; | |||||||||||||||||||||||||||
| 90 | 16 | our $LOG_LEVEL_ERROR = 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Default log level (can be over-ridden by Labyrinth) | ||||||||||||||||||||||||||||
| 93 | 16 | our $LOG_LEVEL = $LOG_LEVEL_ERROR; | |||||||||||||||||||||||||||
| 94 | 16 | our $VERBOSE = 0; | |||||||||||||||||||||||||||
| 95 | 16 | our $CALLER = 0; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 16 | my $firstpass = 1; | |||||||||||||||||||||||||||
| 98 | 16 | my $logfile = undef; | |||||||||||||||||||||||||||
| 99 | 16 | 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 | 0 | ▶ | sub DumpToFile { | ||||||||||||||||||||||||||
|
Subroutine
DumpToFile: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $fh = IO::File->new($file, 'w+') or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | print $fh join("\n",@_) . "\n"; | |||||||||||||||||||||||||||
| 124 | 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 | 4 | ▶ | sub SetLogFile { | ||||||||||||||||||||||||||
|
Subroutine
SetLogFile: called CC=10 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 4 | my %hash = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4 | ▶ | return unless($hash{FILE}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 4 | ▶ | return unless($hash{USER}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | ▶ | eval { if(!-e $hash{FILE}) { my $fh = IO::File->new("$hash{FILE}", 'w+'); $fh->close } }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 4 | ▶ | return if($@ || ! -w $hash{FILE}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not -w $hash{"FILE"}
MC/DC0%0/2
$@ or not -w $hash{"FILE"}
$@
not -w $hash{"FILE"}
Truth table33%1/3
A or not(B)
A= $@B= not -w $hash{"FILE"}
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 4 | $username = $hash{USER}; | |||||||||||||||||||||||||||
| 156 | 4 | ▶ | $LOG_LEVEL = $hash{LEVEL} if($hash{LEVEL}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 4 | ▶ | $CALLER = 1 if($hash{CALLER}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 4 | ▶ | if($hash{CLEAR}) { my $fh = IO::File->new("$hash{FILE}", 'w+'); $fh->close } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 4 | $logfile = Log::LogLite->new($hash{FILE},$LOG_LEVEL); | |||||||||||||||||||||||||||
| 162 | 4 | ▶ | return unless($logfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 4 | $logfile->template( "[<date>] <message>\n" ); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item LogRecord($level,@args) | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Record informational messages to Audit Log. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 20 | ▶ | sub LogRecord { | ||||||||||||||||||||||||||
|
Subroutine
LogRecord: called CC=10 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 20 | ▶ | my $level = shift || $LOG_LEVEL_DEBUG; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $Labyrinth::Audit::LOG_LEVEL_DEBUG
MC/DC0%0/2
shift() || $Labyrinth::Audit::LOG_LEVEL_DEBUG
shift()
$Labyrinth::Audit::LOG_LEVEL_DEBUG
Truth table66%2/3
A || B
A= shift()B= $Labyrinth::Audit::LOG_LEVEL_DEBUG
| |||||||||||||||||||||||||||||
| 174 | 20 | my $mess = ''; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 20 | ▶ | return unless($logfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||
| 179 | 18 | local $" = ","; | |||||||||||||||||||||||||||
| 180 | 18 | ▶ | $mess = "@_" if(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 18 | my $audit = "<:$username> [$level] $mess"; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 18 | ▶ | if($CALLER) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 18 | my $i = 1; | |||||||||||||||||||||||||||
| 187 | 18 | ▶ | while(my @calls = caller($i++)) {; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 10 | $audit .= " => CALLER($calls[1],$calls[2])"; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 18 | ▶ | print STDERR $mess . "\n" if($VERBOSE && $level <= $LOG_LEVEL_INFO); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Labyrinth::Audit::VERBOSE and $level <= $Labyrinth::Audit::LOG_LEVEL_INFO
MC/DC0%0/2
$Labyrinth::Audit::VERBOSE and $level <= $Labyrinth::Audit::LOG_LEVEL_INFO
$Labyrinth::Audit::VERBOSE
$level <= $Labyrinth::Audit::LOG_LEVEL_INFO
Truth table33%1/3
A and B
A= $Labyrinth::Audit::VERBOSEB= $level <= $Labyrinth::Audit::LOG_LEVEL_INFO
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 18 | ▶ | return if($level > $LOG_LEVEL); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 15 | ▶ | $logfile->write('-' x 40,$level) if($firstpass); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 15 | $logfile->write($audit,$level); | |||||||||||||||||||||||||||
| 198 | 15 | $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 | 6 | ▶ | sub LogError { LogRecord($LOG_LEVEL_ERROR,@_) if($LOG_LEVEL >= $LOG_LEVEL_ERROR); } | ||||||||||||||||||||||||||
|
Subroutine
LogError: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 4 | ▶ | sub LogWarning { LogRecord($LOG_LEVEL_WARN ,@_) if($LOG_LEVEL >= $LOG_LEVEL_WARN ); } | ||||||||||||||||||||||||||
|
Subroutine
LogWarning: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 4 | ▶ | sub LogInfo { LogRecord($LOG_LEVEL_INFO ,@_) if($LOG_LEVEL >= $LOG_LEVEL_INFO ); } | ||||||||||||||||||||||||||
|
Subroutine
LogInfo: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 4 | ▶ | sub LogDebug { LogRecord($LOG_LEVEL_DEBUG,@_) if($LOG_LEVEL >= $LOG_LEVEL_DEBUG); } | ||||||||||||||||||||||||||
|
Subroutine
LogDebug: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 16 | 1; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | __END__ | ||||||||||||||||||||||||||||