| 1 |
## ------------------------------------------------------------------------ |
| 2 |
## $Id: Trace.pm,v 1.1 2002/12/19 01:14:48 cvsjoko Exp $ |
| 3 |
## ------------------------------------------------------------------------ |
| 4 |
## $Log: Trace.pm,v $ |
| 5 |
## Revision 1.1 2002/12/19 01:14:48 cvsjoko |
| 6 |
## + initial check-in |
| 7 |
## |
| 8 |
## ------------------------------------------------------------------------ |
| 9 |
|
| 10 |
|
| 11 |
package DesignPattern::Object::Logger; |
| 12 |
|
| 13 |
use strict; |
| 14 |
use warnings; |
| 15 |
|
| 16 |
use Data::Dumper; |
| 17 |
|
| 18 |
# TODO: review, revise and rewire this! |
| 19 |
my $logger = Log::Dispatch::Config->instance; |
| 20 |
|
| 21 |
sub __init { |
| 22 |
my $self = shift; |
| 23 |
my $action = shift; |
| 24 |
my $message = shift; |
| 25 |
#my $options = shift; # ??? or: $self->{options} already...? |
| 26 |
|
| 27 |
# set passed-in arguments |
| 28 |
$self->{action} = $action; |
| 29 |
$self->{message} = $message; |
| 30 |
|
| 31 |
# set default arguments |
| 32 |
$self->{classname} = ref $self; |
| 33 |
$self->{caller} = caller; |
| 34 |
|
| 35 |
# set default values |
| 36 |
$self->{action} ||= ''; |
| 37 |
$self->{options}->{tag} ||= ''; |
| 38 |
$self->{level} ||= 'info'; |
| 39 |
} |
| 40 |
|
| 41 |
sub __out { |
| 42 |
my $self = shift; |
| 43 |
my $message = shift; |
| 44 |
|
| 45 |
# if message given here, append to message stored inside object |
| 46 |
$self->{message} .= ' - ' . $message if $message; |
| 47 |
|
| 48 |
# TODO: refactor this: |
| 49 |
# - push '$self->__get_prefix()' to additional log-entry-metadata, don't show it on/in any console/logfile output |
| 50 |
#$logger->log( level => $self->{level}, message => $self->__get_prefix_long() . $message ); |
| 51 |
#$logger->log( level => $self->{level}, message => $message ); |
| 52 |
#$logger->warning( __PACKAGE__ . "->_skip: '$caller' (mixed into '$classname'): ($options->{tag}) $message."); |
| 53 |
$logger->log( level => $self->{level}, message => $self->__get_prefix_short() . $self->{message} ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
sub __handle_options { |
| 58 |
my $self = shift; |
| 59 |
#my $options = shift; # ??? or: $self->{options} already...? |
| 60 |
} |
| 61 |
|
| 62 |
sub __get_prefix_long { |
| 63 |
my $self = shift; |
| 64 |
return __PACKAGE__ . "->$self->{action}: '$self->{caller}' (mixed into '$self->{classname}'): ($self->{options}->{tag}) - "; |
| 65 |
} |
| 66 |
|
| 67 |
sub __get_prefix_short { |
| 68 |
my $self = shift; |
| 69 |
my $tag_append = ''; |
| 70 |
$tag_append = "$self->{options}->{tag} - " if $self->{options}->{tag}; |
| 71 |
return $self->{classname} . "->$self->{action}: $tag_append"; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
sub log { |
| 77 |
my $self = shift; |
| 78 |
my $message = shift; |
| 79 |
$self->{level} = shift; # TODO: implement propagation of this into Log::Dispatch-object '$logger'! |
| 80 |
$self->{options} = shift; # TODO: implement additional behaviours: e.g. { exit => 1 } would exit after tracedump |
| 81 |
|
| 82 |
$self->__init('log', $message); |
| 83 |
$self->__out(); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
sub skip { |
| 88 |
my $self = shift; |
| 89 |
my $message = shift; |
| 90 |
my $condition = shift; |
| 91 |
$self->{level} = shift; # TODO: implement propagation of this into Log::Dispatch-object '$logger'! |
| 92 |
$self->{options} = shift; # TODO: implement additional behaviours: e.g. { exit => 1 } would exit after tracedump |
| 93 |
|
| 94 |
my $classname = ref $self; |
| 95 |
my $caller = caller; |
| 96 |
|
| 97 |
$self->{options}->{tag} ||= ''; |
| 98 |
|
| 99 |
if ($condition) { |
| 100 |
# V1: |
| 101 |
#$logger->warning( __PACKAGE__ . "->_skip: '$caller' (mixed into '$classname'): ($options->{tag}) $message."); |
| 102 |
# V2: |
| 103 |
$self->__init('skip', $message); |
| 104 |
$self->__out(); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
sub trace { |
| 110 |
my $self = shift; |
| 111 |
my $message = shift; |
| 112 |
my $data = shift; |
| 113 |
my $condition = shift; |
| 114 |
$self->{level} = shift; # TODO: implement propagation of this into Log::Dispatch-object '$logger'! |
| 115 |
$self->{options} = shift; # TODO: implement additional behaviours: e.g. { exit => 1 } would exit after tracedump |
| 116 |
|
| 117 |
my $classname = ref $self; |
| 118 |
my $caller = caller; |
| 119 |
|
| 120 |
$self->{options}->{tag} ||= ''; |
| 121 |
|
| 122 |
# don't do _any_ tracing if disabled |
| 123 |
return if $self->{TRACE_DISABLED}; |
| 124 |
|
| 125 |
# overwrite condition with local configured want-TRACE |
| 126 |
$condition ||= $self->{TRACE}; |
| 127 |
|
| 128 |
# dump data |
| 129 |
my $result = 0; |
| 130 |
if ($condition) { |
| 131 |
$message .= " - " . Dumper($data); |
| 132 |
|
| 133 |
# V1: |
| 134 |
# $self->__init('trace'); |
| 135 |
# $logger->info( __PACKAGE__ . "->trace: '$caller' (mixed into '$classname'): ($options->{tag}) $message."); |
| 136 |
|
| 137 |
# V2: |
| 138 |
$self->__init('trace', $message); |
| 139 |
$self->__out(); |
| 140 |
|
| 141 |
$result = 1; |
| 142 |
} else { |
| 143 |
$result = 0; |
| 144 |
} |
| 145 |
|
| 146 |
# handle additional options |
| 147 |
if ($self->{TRACE_OPTIONS}) { |
| 148 |
exit if $self->{options}->{exit}; |
| 149 |
} |
| 150 |
|
| 151 |
return $result; |
| 152 |
} |
| 153 |
|
| 154 |
1; |