/[cvs]/nfo/perl/libs/DesignPattern/Object.pm
ViewVC logotype

Diff of /nfo/perl/libs/DesignPattern/Object.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by joko, Tue Feb 11 11:04:27 2003 UTC revision 1.10 by joko, Tue May 13 08:46:08 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.10  2003/05/13 08:46:08  joko
6    ##  pod and comments
7    ##
8    ##  Revision 1.9  2003/03/27 15:44:32  joko
9    ##  fixes/enhancements to 'sub log_basic'
10    ##
11    ##  Revision 1.8  2003/02/19 00:36:59  joko
12    ##  + bugfix: this {logger} is the instance itself, so has to be fed with ( level => xyz and namespace => xyz )
13    ##  + minor modifications in behaviour
14    ##
15    ##  Revision 1.7  2003/02/18 18:34:35  joko
16    ##  + fix: just logs if possible (sub log_basic)
17    ##
18  ##  Revision 1.6  2003/02/11 11:04:27  joko  ##  Revision 1.6  2003/02/11 11:04:27  joko
19  ##  + metadata (args, caller, etc.) are now stored inside {__bridge}  ##  + metadata (args, caller, etc.) are now stored inside {__bridge}
20  ##  ##
# Line 22  Line 35 
35  ##  ##
36  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
37    
38    =pod
39    
40    =head1 NAME
41    
42      DesignPattern::Object
43    
44    
45    =head1 TODO
46    
47      o use Class::Loader
48    
49    
50    =cut
51    
52  package DesignPattern::Object;  package DesignPattern::Object;
53    
# Line 34  use Data::Dumper; Line 60  use Data::Dumper;
60  #use Devel::StackTrace;  #use Devel::StackTrace;
61    
62    
63  # get logger instance  my $_dp_globals;
64  my $logger = eval { Log::Dispatch::Config->instance; };  
65    $_dp_globals = {
66      TRACE => 0,
67    };
68    
69    
70  sub new {  sub new {
71        
# Line 45  sub new { Line 75  sub new {
75    # use already blessed reference, if passed in - else use the very classname    # use already blessed reference, if passed in - else use the very classname
76    my $class = ref ($classname) || $classname;    my $class = ref ($classname) || $classname;
77        
78    $logger->debug( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" ) if $logger;    #$self->log_basic( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" , 'debug');
79        
80    # the base for our object - a plain perl hash, which ....    # the base for our object - a plain perl hash, which ....
81    my $self = {};    my $self = {};
# Line 60  sub new { Line 90  sub new {
90    # TODO: what about logging in here? inherit from    # TODO: what about logging in here? inherit from
91    # DesignPattern::Object::Logger for this purpose....    # DesignPattern::Object::Logger for this purpose....
92    # ... or would this give us (harmful) circular module dependencies???    # ... or would this give us (harmful) circular module dependencies???
93    #$logger->debug( __PACKAGE__ . "->new( @args )" );      # this is not "common"!    #$self->log_basic( __PACKAGE__ . "->new( @args )", 'debug' );      # this is not "common"!
94    
95            
96    # remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize!    # remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize!
# Line 130  sub _abstract_function { Line 160  sub _abstract_function {
160    my $self_classname = ref $self;    my $self_classname = ref $self;
161    my $function_name = shift;    my $function_name = shift;
162    # was:    # was:
163    $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');
164    # is:    # is:
165    #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'.");
166    #exit;    #exit;
# Line 147  sub fromPackage { Line 177  sub fromPackage {
177    
178  #print Dumper($args);  #print Dumper($args);
179    
180    $logger->debug( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )" );    $self->log_basic( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )", 'debug');
181        
182    # perl-load    # perl-load
183      my $evstring = "use $pkgname;";      my $evstring = "use $pkgname;";
184      eval($evstring);      eval($evstring);
185        
186    # report errors    # report errors
187      # Could this be united with DesignPattern::Loader::checkExceptions?
188      if ($@) {      if ($@) {
189        # build error-messages        # build error-messages
190        my $errmsg_native = __PACKAGE__ . ':' . __LINE__ . " Error in eval \"$evstring\": " .  $@;        my $issuer = __PACKAGE__ . ':' . __LINE__;
191          my $errmsg_native = "Error in eval: " .  $@;
192        #my $classname = $self->{__classname};        #my $classname = $self->{__classname};
193        my $errmsg_critical = '';        my $errmsg_critical = '';
194        if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) {        if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) {
195          $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).";
196        } else {        } else {
197          $errmsg_critical = $errmsg_native;          $errmsg_critical = $errmsg_native;
198        }        }
199        # write error to logging-output (console|file|database)        # write error to logging-output (console|file|database)
200        $logger->debug( $errmsg_native );        $self->log_basic( $errmsg_native . " (issuer='$issuer', code='$evstring')", 'debug' );
201        $logger->critical( $errmsg_critical );        $self->log_basic( $errmsg_critical, 'warning' );
202        return;        return;
203      }      }
204        
205    # object-creation    # object-creation
206      my $object = $pkgname->new(@args);      my $object = $pkgname->new(@args);
207    
208      # trace
209        #print Dumper($object);
210    
211    # run boot-methods on object    # run boot-methods on object
212      $object->_init() if $object->can('_init');      $object->_init() if $object->can('_init');
213      $object->constructor() if $object->can('constructor');      $object->constructor() if $object->can('constructor');
# Line 180  sub fromPackage { Line 215  sub fromPackage {
215    return $object;    return $object;
216  }  }
217    
218    sub log_basic {
219      my $self = shift;
220      my $message = shift;
221      my $level = shift;
222      
223      #return;
224      $level ||= 'info';
225    
226      my $notSoGood = ($level =~ /warning|error|critical/);
227      
228      # STDERR?
229      if ($level && $notSoGood) {
230        print STDERR $level, ": ", $message, "\n";
231      }
232    
233      # STDOUT?
234      if ($_dp_globals->{TRACE} || $notSoGood) {
235        print $level, ": ", $message, "\n";
236      }
237    
238      # get logger instance
239      if (!$_dp_globals->{logger}) {
240        $_dp_globals->{logger} = eval { Log::Dispatch::Config->instance; };
241      }
242      
243      if ($_dp_globals->{logger}) {
244        $_dp_globals->{logger}->log( level => $level, message => $message);
245      #} else {
246        #print $level, ": ", $message, "\n";
247      }
248      
249    }
250    
251  1;  1;
252    __END__

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.10

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed