2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.7 2003/02/18 18:34:35 joko |
6 |
|
## + fix: just logs if possible (sub log_basic) |
7 |
|
## |
8 |
## Revision 1.6 2003/02/11 11:04:27 joko |
## Revision 1.6 2003/02/11 11:04:27 joko |
9 |
## + metadata (args, caller, etc.) are now stored inside {__bridge} |
## + metadata (args, caller, etc.) are now stored inside {__bridge} |
10 |
## |
## |
37 |
#use Devel::StackTrace; |
#use Devel::StackTrace; |
38 |
|
|
39 |
|
|
40 |
# get logger instance |
my $_dp_globals; |
41 |
my $logger = eval { Log::Dispatch::Config->instance; }; |
|
42 |
|
$_dp_globals = { |
43 |
|
TRACE => 0, |
44 |
|
}; |
45 |
|
|
46 |
|
|
47 |
sub new { |
sub new { |
48 |
|
|
52 |
# use already blessed reference, if passed in - else use the very classname |
# use already blessed reference, if passed in - else use the very classname |
53 |
my $class = ref ($classname) || $classname; |
my $class = ref ($classname) || $classname; |
54 |
|
|
55 |
$logger->debug( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" ) if $logger; |
#$self->log_basic( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" , 'debug'); |
56 |
|
|
57 |
# the base for our object - a plain perl hash, which .... |
# the base for our object - a plain perl hash, which .... |
58 |
my $self = {}; |
my $self = {}; |
67 |
# TODO: what about logging in here? inherit from |
# TODO: what about logging in here? inherit from |
68 |
# DesignPattern::Object::Logger for this purpose.... |
# DesignPattern::Object::Logger for this purpose.... |
69 |
# ... or would this give us (harmful) circular module dependencies??? |
# ... or would this give us (harmful) circular module dependencies??? |
70 |
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
#$self->log_basic( __PACKAGE__ . "->new( @args )", 'debug' ); # this is not "common"! |
71 |
|
|
72 |
|
|
73 |
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
137 |
my $self_classname = ref $self; |
my $self_classname = ref $self; |
138 |
my $function_name = shift; |
my $function_name = shift; |
139 |
# was: |
# was: |
140 |
$logger->warning( __PACKAGE__ . ": function '$function_name' is an abstract method, please implement it in '$self_classname'."); |
$self->log_basic( __PACKAGE__ . ": function '$function_name' is an abstract method, please implement it in '$self_classname'.", 'warning'); |
141 |
# is: |
# is: |
142 |
#die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'."); |
#die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'."); |
143 |
#exit; |
#exit; |
154 |
|
|
155 |
#print Dumper($args); |
#print Dumper($args); |
156 |
|
|
157 |
$logger->debug( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )" ); |
$self->log_basic( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )", 'debug'); |
158 |
|
|
159 |
# perl-load |
# perl-load |
160 |
my $evstring = "use $pkgname;"; |
my $evstring = "use $pkgname;"; |
167 |
#my $classname = $self->{__classname}; |
#my $classname = $self->{__classname}; |
168 |
my $errmsg_critical = ''; |
my $errmsg_critical = ''; |
169 |
if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) { |
if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) { |
170 |
$errmsg_critical = "Could not instantiate object from package '$pkgname' - location of '$1' failed."; |
$errmsg_critical = "Could not instantiate object from package '$pkgname' ('$1' not found)."; |
171 |
} else { |
} else { |
172 |
$errmsg_critical = $errmsg_native; |
$errmsg_critical = $errmsg_native; |
173 |
} |
} |
174 |
# write error to logging-output (console|file|database) |
# write error to logging-output (console|file|database) |
175 |
$logger->debug( $errmsg_native ); |
$self->log_basic( $errmsg_native, 'debug' ); |
176 |
$logger->critical( $errmsg_critical ); |
$self->log_basic( $errmsg_critical, 'warning' ); |
177 |
return; |
return; |
178 |
} |
} |
179 |
|
|
187 |
return $object; |
return $object; |
188 |
} |
} |
189 |
|
|
190 |
|
sub log_basic { |
191 |
|
my $self = shift; |
192 |
|
my $message = shift; |
193 |
|
my $level = shift; |
194 |
|
|
195 |
|
if ($_dp_globals->{TRACE} || ($level && $level =~ /warning|error|critical/)) { |
196 |
|
print $level, ": ", $message, "\n"; |
197 |
|
} |
198 |
|
|
199 |
|
# get logger instance |
200 |
|
if (!$_dp_globals->{logger}) { |
201 |
|
$_dp_globals->{logger} = eval { Log::Dispatch::Config->instance; }; |
202 |
|
} |
203 |
|
|
204 |
|
if ($_dp_globals->{logger}) { |
205 |
|
$_dp_globals->{logger}->log($level, $message); |
206 |
|
#} else { |
207 |
|
#print $level, ": ", $message, "\n"; |
208 |
|
} |
209 |
|
|
210 |
|
} |
211 |
|
|
212 |
1; |
1; |
213 |
|
__END__ |