File Coverage

blib/lib/DBIx/LogAny.pm
Criterion Covered Total %
statement 56 111 50.4
branch 17 54 31.4
condition 6 18 33.3
subroutine 12 23 52.1
pod 4 4 100.0
total 95 210 45.2


line stmt bran cond sub pod time code
1             # $Id$
2             package DBIx::LogAny;
3             require 5.008;
4              
5 2     2   47973 use strict;
  2         5  
  2         67  
6 2     2   13 use warnings;
  2         4  
  2         73  
7 2     2   11 use Carp qw(croak cluck);
  2         11  
  2         122  
8 2     2   391 use Log::Any;
  2         9024  
  2         11  
9 2     2   1258 use Data::Dumper;
  2         16498  
  2         177  
10 2     2   20 use Scalar::Util qw(blessed);
  2         6  
  2         227  
11              
12 2     2   944 use DBIx::LogAny::Constants qw (:masks $LogMask);
  2         7  
  2         438  
13 2     2   1180 use DBIx::LogAny::db;
  2         11  
  2         91  
14 2     2   1155 use DBIx::LogAny::st;
  2         9  
  2         2862  
15              
16             our $VERSION = '0.05';
17             require Exporter;
18             our @ISA = qw(Exporter DBI); # look in DBI for anything we don't do
19              
20             our @EXPORT = (); # export nothing by default
21             our @EXPORT_MASKS = qw(DBIX_LA_LOG_DEFAULT
22             DBIX_LA_LOG_ALL
23             DBIX_LA_LOG_INPUT
24             DBIX_LA_LOG_OUTPUT
25             DBIX_LA_LOG_CONNECT
26             DBIX_LA_LOG_TXN
27             DBIX_LA_LOG_ERRCAPTURE
28             DBIX_LA_LOG_WARNINGS
29             DBIX_LA_LOG_ERRORS
30             DBIX_LA_LOG_DBDSPECIFIC
31             DBIX_LA_LOG_DELAYBINDPARAM
32             DBIX_LA_LOG_SQL
33             DBIX_LA_LOG_STORE
34             );
35             our %EXPORT_TAGS= (masks => \@EXPORT_MASKS);
36             Exporter::export_ok_tags('masks'); # all tags must be in EXPORT_OK
37              
38             sub _dbix_la_debug {
39 15     15   46 my ($self, $h, $level, $thing, @args) = @_;
40              
41 15 50       34 $h = $self->{private_DBIx_LogAny} if !defined($h);
42              
43 15 50       50 return unless $h->{logger}->is_debug();
44              
45 15         152 local $Data::Dumper::Indent = 0;
46 15         40 local $Data::Dumper::Quotekeys = 0;
47              
48             $args[0] = $args[0]->{Statement} if blessed($args[0]) and
49             blessed($args[0])->isa('DBI::st') and
50 15 50 66     103 defined ($args[0]->{Statement});
      66        
51              
52 15 100       77 if (scalar(@args) > 1) {
    50          
    50          
    100          
53             $h->{logger}->debug(
54 2         15 Data::Dumper->Dump([\@args], [$thing]))
55             } elsif (ref($thing) eq 'CODE') {
56 0         0 $h->{logger}->debug($thing);
57             } elsif (ref($args[0])) {
58             $h->{logger}->debug(
59 0         0 Data::Dumper->Dump([$args[0]], [$thing]))
60             } elsif (scalar(@args) == 1) {
61 6 50       17 if (!defined($args[0])) {
62 0         0 $h->{logger}->debug("$thing:");
63             } else {
64 6         46 $h->{logger}->debug("$thing: " . DBI::neat($args[0]));
65             }
66             } else {
67 7         22 $h->{logger}->debug($thing);
68             }
69 15         1501 return;
70             }
71              
72             sub _dbix_la_info {
73 0     0   0 my ($self, $level, $thing) = @_;
74              
75 0         0 my $h = $self->{private_DBIx_LogAny};
76              
77 0 0       0 return unless $h->{logger}->is_info();
78              
79 0         0 $h->{logger}->info($thing);
80              
81 0         0 return;
82             }
83             sub _dbix_la_warning {
84 0     0   0 my ($self, $level, $thing, @args) = @_;
85              
86 0         0 my $h = $self->{private_DBIx_LogAny};
87              
88 0 0       0 return unless $h->{logger}->is_warn();
89              
90 0         0 local $Data::Dumper::Indent = 0;
91 0         0 local $Data::Dumper::Quotekeys = 0;
92              
93 0 0       0 if (scalar(@args) > 1) {
    0          
94             $h->{logger}->warn(
95 0     0   0 sub {Data::Dumper->Dump([\@args], [$thing])})
96 0         0 } elsif (ref($args[0])) {
97             $h->{logger}->warn(
98 0     0   0 sub {Data::Dumper->Dump([$args[0]], [$thing])})
99 0         0 } else {
100 0         0 $h->{logger}->warn("$thing: " . DBI::neat($args[0]));
101             }
102 0         0 return;
103             }
104              
105             sub _dbix_la_error {
106 0     0   0 my ($self, $level, $thing, @args) = @_;
107              
108 0         0 my $h = $self->{private_DBIx_LogAny};
109              
110 0 0       0 return unless $h->{logger}->is_error();
111              
112 0         0 local $Data::Dumper::Indent = 0;
113 0         0 local $Data::Dumper::Quotekeys = 0;
114              
115 0 0       0 if (scalar(@args) > 1) {
    0          
    0          
116             $h->{logger}->error(
117 0     0   0 sub {Data::Dumper->Dump([\@args], [$thing])})
118 0         0 } elsif (ref($thing) eq 'CODE') {
119 0         0 $h->{logger}->error($thing);
120             } elsif (ref($args[0])) {
121             $h->{logger}->error(
122 0     0   0 sub {Data::Dumper->Dump([$args[0]], [$thing])})
123 0         0 } else {
124 0         0 $h->{logger}->error("$thing: " . DBI::neat($args[0]));
125             }
126 0         0 return;
127             }
128              
129             sub _dbix_la_attr_map {
130             return {
131 0     0   0 dbix_la_logmask => 'logmask',
132             dbix_la_ignore_err_regexp => 'err_regexp',
133             dbix_la_category => 'category',
134             dbix_la_logger => 'logger'
135             };
136             }
137              
138             sub dbix_la_getattr {
139 0     0 1 0 my ($self, $item) = @_;
140              
141 0 0 0     0 croak ('wrong arguments - dbix_la_getattr(attribute_name)')
142             if (scalar(@_) != 2 || !defined($_[1]));
143              
144 0         0 my $m = _dbix_la_attr_map();
145              
146 0         0 my $h = $self->{private_DBIx_LogAny};
147              
148 0 0       0 if (!exists($m->{$item})) {
149 0         0 warn "$item does not exist";
150 0         0 return;
151             }
152 0         0 return $h->{$m->{$item}};
153             }
154              
155             sub dbix_la_setattr {
156 0     0 1 0 my ($self, $item, $value) = @_;
157              
158 0 0 0     0 croak ('wrong arguments - dbix_la_setattr(attribute_name, value)')
159             if (scalar(@_) != 3 || !defined($_[1]));
160              
161 0         0 my $m = _dbix_la_attr_map();
162              
163 0         0 my $h = $self->{private_DBIx_LogAny};
164              
165 0 0       0 if (!exists($m->{$item})) {
166 0         0 warn "$item does not exist";
167 0         0 return;
168             }
169 0         0 $h->{$m->{$item}} = $value;
170 0         0 1;
171             }
172              
173             sub connect {
174              
175 1     1 1 4718 my ($drh, $dsn, $user, $pass, $attr) = @_;
176              
177 1         12 my $dbh = $drh->SUPER::connect($dsn, $user, $pass, $attr);
178 1 50       71 return $dbh if (!$dbh);
179              
180 1         5 my $h = $dbh->{private_DBIx_LogAny};
181              
182 1 50       4 if ($h->{logmask} & DBIX_LA_LOG_CONNECT) {
183 1         3 local $Data::Dumper::Indent = 0;
184             $h->{logger}->debug(
185             "connect($h->{dbh_no}): " .
186             (defined($dsn) ? $dsn : '') . ', ' .
187             (defined($user) ? $user : '') . ', ' .
188             Data::Dumper->Dump([$attr], [qw(attr)]))
189 1 50       8 if $h->{logger}->is_debug;
    50          
    50          
190 2     2   23 no strict 'refs';
  2         6  
  2         598  
191 1         307 my $v = "DBD::" . $dbh->{Driver}->{Name} . "::VERSION";
192             $h->{logger}->info("DBI: " . $DBI::VERSION,
193             ", DBIx::LogAny: " . $DBIx::LogAny::VERSION .
194             ", Driver: " . $h->{driver} . "(" .
195             $$v . ")")
196 1 50       18 if $h->{logger}->is_info;
197             }
198              
199             #
200             # Enable dbms_output for DBD::Oracle else turn off DBDSPECIFIC as we have
201             # no support for DBDSPECIFIC in any other drivers.
202             # BUT only enable it if the log handle is doing debug as we only call
203             # dbms_output_get in that case.
204             #
205 1         94 $h->{dbd_specific} = 1;
206 1 50 33     4 if (($h->{logger}->is_debug()) &&
      33        
207             ($h->{logmask} & DBIX_LA_LOG_DBDSPECIFIC) &&
208             ($h->{driver} eq 'Oracle')) {
209 0         0 $dbh->func('dbms_output_enable');
210             } else {
211 1         58 $h->{logmask} &= ~DBIX_LA_LOG_DBDSPECIFIC;
212             }
213 1         3 $h->{dbd_specific} = 0;
214 1         5 return $dbh;
215             }
216              
217             sub dbix_la_logdie
218             {
219 0     0 1   my ($drh, $msg) = @_;
220 0           _error_handler($msg, $drh);
221 0           die "$msg";
222             }
223              
224             1;
225              
226             __END__