File Coverage

blib/lib/Medusa.pm
Criterion Covered Total %
statement 60 64 93.7
branch 11 14 78.5
condition n/a
subroutine 13 13 100.0
pod 0 1 0.0
total 84 92 91.3


line stmt bran cond sub pod time code
1             package Medusa;
2              
3 5     5   590666 use 5.008003;
  5         19  
4 5     5   24 use strict;
  5         8  
  5         191  
5 5     5   63 use warnings;
  5         10  
  5         353  
6              
7 5     5   28 use B;
  5         11  
  5         183  
8 5     5   3147 use Data::Dumper;
  5         49283  
  5         5019  
9             our %LOG;
10              
11             BEGIN {
12             %LOG = (
13             LOGGER => 'Medusa::Logger',
14             LOG_LEVEL => 'debug',
15             LOG_FILE => 'audit.log',
16             LOG => sub {
17 0         0 (my $module = $LOG{LOGGER}) =~ s/::/\//g;
18 0         0 require $module . '.pm';
19             $LOG{LOGGER}->new(
20             file => $LOG{LOG_FILE},
21 0         0 );
22             },
23 5     5   421 LOG_FUNCTIONS => {
24             error => 'error',
25             info => 'info',
26             debug => 'debug',
27             }
28             );
29             }
30              
31             sub import {
32 7     7   196581 my ($pkg, @import) = @_;
33 7 100       42 if (scalar @import % 2) {
34 1         13 die "odd number of params passed in import";
35             }
36 6         16 my $caller = caller();
37             {
38 5     5   40 no strict 'refs';
  5         8  
  5         1114  
  6         8  
39 6         27 push @{"${caller}::ISA"}, $pkg;
  6         114  
40             }
41 6         110954 while (@import) {
42 2         5 my ($key, $val) = (shift @import, shift @import);
43 2         2205 $LOG{$key} = $val;
44             }
45             }
46              
47             sub MODIFY_CODE_ATTRIBUTES {
48 4     4   4178 my ($class,$code,@attrs) = @_;
49            
50 4 50       18 if (ref $LOG{LOG} eq 'CODE') {
51 0         0 $LOG{LOG} = $LOG{LOG}->();
52             }
53            
54 4 50       6 if (grep { $_ eq 'Audit' } @attrs) {
  4         17  
55 4         24 my $meta = B::svref_2object($code);
56 4         24 my $meth = $meta->GV->NAME;
57 4         48 my $caller = caller(1);
58 5     5   34 no strict 'refs';
  5         8  
  5         200  
59 5     5   32 no warnings 'redefine';
  5         8  
  5         2577  
60 4         32 *{"${caller}::$meth"} = sub {
61 4     4   320233 log_message(
62             sprintf(
63             "subroutine %s called with params:",
64             $meth
65             ),
66             @_
67             );
68 4         34 my @out = $code->(@_);
69 4         30 log_message(
70             sprintf(
71             "subroutine %s returned:",
72             $meth
73             ),
74             @out
75             );
76 4 100       27 return wantarray ? @out : shift @out;
77 4         50 };
78 4         16 return;
79             }
80              
81             }
82              
83             sub log_message {
84 10     10 0 106273 my $log_message = $_[0];
85 10         36 my $log_meth = $LOG{LOG_FUNCTIONS}{$LOG{LOG_LEVEL}};
86 10 100       106 if (@_ > 1) {
87 9         21 my $len = scalar @_ - 1;
88 9         32 for my $i (1 .. $len) {
89 15         53 my $data = Dumper($_[$i]);
90 15         824 $data =~ s/\$VAR1\s=\s//;
91 15 50       81 $data =~ s/(\s+)(['"][^"]+['"])*/defined $2 ? $2 : ""/gem;
  15         63  
92 15 100       59 $data =~ s/;$/ -/ unless $i == $len;
93 15         53 $log_message = sprintf("%s %s", $log_message, $data);
94             }
95             }
96 10         68 $LOG{LOG}->$log_meth($log_message);
97             }
98              
99             1;
100              
101             __END__