Show
Ignore:
Timestamp:
05/14/08 23:35:55 (19 months ago)
Author:
bchoate
Message:

Cleanup for TheSchwartz logging. Added support for 'leak' switch to help trace object leaks.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-38/lib/MT/TheSchwartz.pm

    r2336 r2337  
    3030    my $class = shift; 
    3131    $class->instance->SUPER::insert(@_); 
     32} 
     33 
     34sub default_logger { 
     35    my ($msg, $job) = @_; 
     36    # suppress TheSchwartz::Job's 'job completed' 
     37    return if $msg eq 'job completed'; 
     38 
     39    $msg =~ s/\s+$//; 
     40    print STDERR "$msg\n"; 
    3241} 
    3342 
     
    4150    # Reports object usage inbetween jobs if Devel::Leak::Object is loaded 
    4251    $OBJECT_REPORT = 1 if $Devel::Leak::Object::VERSION; 
     52 
     53    $param{verbose} = \&default_logger 
     54        if $param{verbose} && (ref $param{verbose} ne 'CODE'); 
    4355 
    4456    my $client = $class->SUPER::new(%param); 
     
    134146    my $did_work = 0; 
    135147 
     148    # holds state of objects at start 
     149    my %obj_start; 
    136150    if ($OBJECT_REPORT) { 
    137         Devel::Leak::Object::status(); 
    138         print "\n\n"; 
     151        %obj_start = %Devel::Leak::Object::OBJECT_COUNT; 
    139152    } 
    140153 
    141154    while (1) { 
     155        my %obj_pre; 
     156        if ($OBJECT_REPORT) { 
     157            %obj_pre = %Devel::Leak::Object::OBJECT_COUNT; 
     158        } 
     159 
    142160        if ($client->work_once) { 
    143161            $did_work = 1; 
     
    157175            $did_work = 0; 
    158176            if ($OBJECT_REPORT) { 
    159                 Devel::Leak::Object::status(); 
    160                 print "\n\n"; 
     177                leak_report(\%obj_start, \%obj_pre, \%Devel::Leak::Object::OBJECT_COUNT); 
    161178            } 
    162179        } 
    163180 
    164181        sleep $delay; 
     182    } 
     183} 
     184 
     185our %persistent; 
     186BEGIN { 
     187    %persistent = map { $_ => 1 } qw( MT::Task MT::Plugin MT::Component MT::ArchiveType MT::TaskMgr MT::WeblogPublisher MT::Serializer TheSchwartz::Job TheSchwartz::JobHandle ); 
     188} 
     189sub leak_report { 
     190    my ($start, $pre, $post) = @_; 
     191    my $reported; 
     192    foreach my $class (sort keys %$post) { 
     193        # skip reporting classes that are persistent in nature 
     194        next if exists $persistent{$class}; 
     195 
     196        my $post_count = $post->{$class}; 
     197        next if ! $post_count; 
     198        my $pre_count = $pre->{$class} || 0; 
     199        my $start_count = $start->{$class} || 0; 
     200        next if $post_count == 1;  # ignores most singletons 
     201        if (($pre_count != $post_count) || ($post_count != $start_count)) { 
     202            print "Leak report (class, total, delta from last job(s), delta since process start):\n" unless $reported; 
     203            printf "%-40s %-10d %-10d %-10d\n", $class, $post_count, $post_count - $pre_count, $post_count - $start_count; 
     204            $reported = 1; 
     205        } 
    165206    } 
    166207}