2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.9 2003/03/27 15:44:32 joko |
6 |
|
## fixes/enhancements to 'sub log_basic' |
7 |
|
## |
8 |
|
## Revision 1.8 2003/02/19 00:36:59 joko |
9 |
|
## + bugfix: this {logger} is the instance itself, so has to be fed with ( level => xyz and namespace => xyz ) |
10 |
|
## + minor modifications in behaviour |
11 |
|
## |
12 |
|
## Revision 1.7 2003/02/18 18:34:35 joko |
13 |
|
## + fix: just logs if possible (sub log_basic) |
14 |
|
## |
15 |
## Revision 1.6 2003/02/11 11:04:27 joko |
## Revision 1.6 2003/02/11 11:04:27 joko |
16 |
## + metadata (args, caller, etc.) are now stored inside {__bridge} |
## + metadata (args, caller, etc.) are now stored inside {__bridge} |
17 |
## |
## |
44 |
#use Devel::StackTrace; |
#use Devel::StackTrace; |
45 |
|
|
46 |
|
|
47 |
# get logger instance |
my $_dp_globals; |
48 |
my $logger = eval { Log::Dispatch::Config->instance; }; |
|
49 |
|
$_dp_globals = { |
50 |
|
TRACE => 0, |
51 |
|
}; |
52 |
|
|
53 |
|
|
54 |
sub new { |
sub new { |
55 |
|
|
59 |
# use already blessed reference, if passed in - else use the very classname |
# use already blessed reference, if passed in - else use the very classname |
60 |
my $class = ref ($classname) || $classname; |
my $class = ref ($classname) || $classname; |
61 |
|
|
62 |
$logger->debug( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" ) if $logger; |
#$self->log_basic( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" , 'debug'); |
63 |
|
|
64 |
# the base for our object - a plain perl hash, which .... |
# the base for our object - a plain perl hash, which .... |
65 |
my $self = {}; |
my $self = {}; |
74 |
# TODO: what about logging in here? inherit from |
# TODO: what about logging in here? inherit from |
75 |
# DesignPattern::Object::Logger for this purpose.... |
# DesignPattern::Object::Logger for this purpose.... |
76 |
# ... or would this give us (harmful) circular module dependencies??? |
# ... or would this give us (harmful) circular module dependencies??? |
77 |
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
#$self->log_basic( __PACKAGE__ . "->new( @args )", 'debug' ); # this is not "common"! |
78 |
|
|
79 |
|
|
80 |
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
144 |
my $self_classname = ref $self; |
my $self_classname = ref $self; |
145 |
my $function_name = shift; |
my $function_name = shift; |
146 |
# was: |
# was: |
147 |
$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'); |
148 |
# is: |
# is: |
149 |
#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'."); |
150 |
#exit; |
#exit; |
161 |
|
|
162 |
#print Dumper($args); |
#print Dumper($args); |
163 |
|
|
164 |
$logger->debug( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )" ); |
$self->log_basic( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )", 'debug'); |
165 |
|
|
166 |
# perl-load |
# perl-load |
167 |
my $evstring = "use $pkgname;"; |
my $evstring = "use $pkgname;"; |
170 |
# report errors |
# report errors |
171 |
if ($@) { |
if ($@) { |
172 |
# build error-messages |
# build error-messages |
173 |
my $errmsg_native = __PACKAGE__ . ':' . __LINE__ . " Error in eval \"$evstring\": " . $@; |
my $issuer = __PACKAGE__ . ':' . __LINE__; |
174 |
|
my $errmsg_native = "Error in eval: " . $@; |
175 |
#my $classname = $self->{__classname}; |
#my $classname = $self->{__classname}; |
176 |
my $errmsg_critical = ''; |
my $errmsg_critical = ''; |
177 |
if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) { |
if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) { |
178 |
$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)."; |
179 |
} else { |
} else { |
180 |
$errmsg_critical = $errmsg_native; |
$errmsg_critical = $errmsg_native; |
181 |
} |
} |
182 |
# write error to logging-output (console|file|database) |
# write error to logging-output (console|file|database) |
183 |
$logger->debug( $errmsg_native ); |
$self->log_basic( $errmsg_native . " (issuer='$issuer', code='$evstring')", 'debug' ); |
184 |
$logger->critical( $errmsg_critical ); |
$self->log_basic( $errmsg_critical, 'warning' ); |
185 |
return; |
return; |
186 |
} |
} |
187 |
|
|
188 |
# object-creation |
# object-creation |
189 |
my $object = $pkgname->new(@args); |
my $object = $pkgname->new(@args); |
190 |
|
|
191 |
|
# trace |
192 |
|
#print Dumper($object); |
193 |
|
|
194 |
# run boot-methods on object |
# run boot-methods on object |
195 |
$object->_init() if $object->can('_init'); |
$object->_init() if $object->can('_init'); |
196 |
$object->constructor() if $object->can('constructor'); |
$object->constructor() if $object->can('constructor'); |
198 |
return $object; |
return $object; |
199 |
} |
} |
200 |
|
|
201 |
|
sub log_basic { |
202 |
|
my $self = shift; |
203 |
|
my $message = shift; |
204 |
|
my $level = shift; |
205 |
|
|
206 |
|
#return; |
207 |
|
$level ||= 'info'; |
208 |
|
|
209 |
|
my $notSoGood = ($level =~ /warning|error|critical/); |
210 |
|
|
211 |
|
# STDERR? |
212 |
|
if ($level && $notSoGood) { |
213 |
|
print STDERR $level, ": ", $message, "\n"; |
214 |
|
} |
215 |
|
|
216 |
|
# STDOUT? |
217 |
|
if ($_dp_globals->{TRACE} || $notSoGood) { |
218 |
|
print $level, ": ", $message, "\n"; |
219 |
|
} |
220 |
|
|
221 |
|
# get logger instance |
222 |
|
if (!$_dp_globals->{logger}) { |
223 |
|
$_dp_globals->{logger} = eval { Log::Dispatch::Config->instance; }; |
224 |
|
} |
225 |
|
|
226 |
|
if ($_dp_globals->{logger}) { |
227 |
|
$_dp_globals->{logger}->log( level => $level, message => $message); |
228 |
|
#} else { |
229 |
|
#print $level, ": ", $message, "\n"; |
230 |
|
} |
231 |
|
|
232 |
|
} |
233 |
|
|
234 |
1; |
1; |
235 |
|
__END__ |