| 1 | package Tk::ErrorDump; | ||
| 2 | |||
| 3 | use vars qw($VERSION); | ||
| 4 | $VERSION = '0.02'; | ||
| 5 | |||
| 6 | use English; | ||
| 7 | use Tk (); | ||
| 8 | use base qw(Tk::Toplevel); | ||
| 9 | use Tk::ROText; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | |||
| 13 | Construct Tk::Widget 'ErrorDump'; | ||
| 14 | |||
| 15 | my $ED_OBJECT; | ||
| 16 | |||
| 17 | sub Populate { | ||
| 18 | |||
| 19 | # ErrorDump constructor. Uses `new' method from base class | ||
| 20 | # to create object container then creates the dialog toplevel and the | ||
| 21 | # traceback toplevel. | ||
| 22 | |||
| 23 | my($cw, $args) = @_; | ||
| 24 | |||
| 25 | $cw->minsize(1, 1); | ||
| 26 | $cw->title('Dump Stack Trace for Error'); | ||
| 27 | $cw->iconname('Stack Trace'); | ||
| 28 | |||
| 29 | my $labframe = $cw->Frame->pack(-side => 'top', -fill => 'x', -expand => 1); | ||
| 30 | my $t_bitmap = $labframe->Label( | ||
| 31 | -bitmap => 'error' | ||
| 32 | )->grid(-column => 0, -row => 0, -sticky => 'e'); | ||
| 33 | |||
| 34 | my $t_label = $labframe->Label( | ||
| 35 | -text => 'on-the-fly-text', | ||
| 36 | -justify => 'left', | ||
| 37 | )->grid(-column => 1, -row => 0, -sticky => 'w', -pady => 4); | ||
| 38 | |||
| 39 | my $t_text = $cw->ROText( | ||
| 40 | -relief => 'sunken', | ||
| 41 | -bd => 2, | ||
| 42 | -width => 60, | ||
| 43 | -height => 20, | ||
| 44 | )->pack(-side => 'top', -fill => 'both', -expand => 1); | ||
| 45 | |||
| 46 | my $t_ok = $cw->Button( | ||
| 47 | -text => 'OK', | ||
| 48 | -command => [ | ||
| 49 | sub { | ||
| 50 | my $cw = shift; | ||
| 51 | # execute any cleanup code if it was defined | ||
| 52 | my $c = $cw->{Configure}{'-dumpcode'}; | ||
| 53 | &$c(undef, @{$cw->{ErrorInfo}}) if defined $c; | ||
| 54 | $cw->withdraw; | ||
| 55 | }, $cw, | ||
| 56 | ] | ||
| 57 | )->pack(-side => 'left', -anchor => 'center', -padx => '3m', -pady => '2m', -expand => 1); | ||
| 58 | |||
| 59 | my $t_save = $cw->Button( | ||
| 60 | -text => 'Save Dump', | ||
| 61 | -command => [ | ||
| 62 | sub { | ||
| 63 | shift->Dump; | ||
| 64 | }, $cw, | ||
| 65 | ] | ||
| 66 | )->pack(-side => 'left', -anchor => 'center', -padx => '3m', -pady => '2m', -expand => 1); | ||
| 67 | |||
| 68 | $cw->withdraw; | ||
| 69 | |||
| 70 | $cw->Advertise(error_label => $t_label); # advertise dialog widget | ||
| 71 | $cw->Advertise(text => $t_text); # advertise text widget | ||
| 72 | $cw->ConfigSpecs( | ||
| 73 | -dumpcode => [PASSIVE => undef, undef, undef], | ||
| 74 | -filtercode => [PASSIVE => undef, undef, undef], | ||
| 75 | -icon => [ PASSIVE => undef, undef, undef ], | ||
| 76 | -defaultfile => [ PASSIVE => undef, undef, undef ]); | ||
| 77 | $ED_OBJECT = $cw; | ||
| 78 | return $cw; | ||
| 79 | |||
| 80 | } # end new, ErrorDialog constructor | ||
| 81 | # | ||
| 82 | # request a Save file, then dump our | ||
| 83 | # traceback, then let app dump whatever it needs to | ||
| 84 | # | ||
| 85 | sub Dump { | ||
| 86 | my ($cw) = @_; | ||
| 87 | # | ||
| 88 | # open saveas dialog | ||
| 89 | # | ||
| 90 | my $dumpfile = $cw->getSaveFile( | ||
| 91 | -title => 'Save Project As', | ||
| 92 | -initialfile => $ED_OBJECT->{Configure}{'-defaultfile'}); | ||
| 93 | my $fh; | ||
| 94 | |||
| 95 | print $fh "--- ERROR ---\n", | ||
| 96 | (shift @{$cw->{ErrorInfo}}), "\n", | ||
| 97 | "---- Begin Traceback ----\n", | ||
| 98 | join("\n", @{$cw->{ErrorInfo}}), "\n" | ||
| 99 | if ($dumpfile && open($fh, ">>$dumpfile")); | ||
| 100 | |||
| 101 | # execute any cleanup code if it was defined | ||
| 102 | my $c = $cw->{Configure}{'-dumpcode'}; | ||
| 103 | &$c($fh, @{$cw->{ErrorInfo}}) | ||
| 104 | if (defined($c) && (ref $c) && (ref $c eq 'CODE')); | ||
| 105 | close $fh; | ||
| 106 | $cw->withdraw; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub Tk::Error { | ||
| 110 | |||
| 111 | # Post a dialog box with the error message and give the user a chance | ||
| 112 | # to see a more detailed stack trace. | ||
| 113 | |||
| 114 | my($w, $error, @msgs) = @_; | ||
| 115 | |||
| 116 | my $grab = $w->grab('current'); | ||
| 117 | $grab->Unbusy if (defined $grab); | ||
| 118 | # | ||
| 119 | # create widget if not exists | ||
| 120 | # | ||
| 121 | $w->ErrorDump if not defined $ED_OBJECT; | ||
| 122 | my $cw = $ED_OBJECT; | ||
| 123 | # | ||
| 124 | # apply filter if defined | ||
| 125 | # | ||
| 126 | my $c = $cw->{Configure}{'-filtercode'}; | ||
| 127 | ($error, @msgs) = &$c($error, @msgs) | ||
| 128 | if (defined($c) && (ref $c) && (ref $c eq 'CODE')); | ||
| 129 | |||
| 130 | $cw->{ErrorInfo} = [ ($error, @msgs) ]; | ||
| 131 | my $lbl = $cw->Subwidget('error_label'); | ||
| 132 | $lbl->configure(-text => $error); | ||
| 133 | my $t = $cw->Subwidget('text'); | ||
| 134 | my $icon = $cw->{Configure}{-icon}; | ||
| 135 | $cw->Icon(-image => $icon) if $icon; | ||
| 136 | $t->bell; | ||
| 137 | $t->configure(-background => 'white'); | ||
| 138 | |||
| 139 | chop $error; | ||
| 140 | $t->delete('0.0', 'end'); | ||
| 141 | $t->insert('end', "\n"); | ||
| 142 | $t->mark('set', 'ltb', 'end'); | ||
| 143 | $t->insert('end', "--- Begin Traceback ---\n$error\n"); | ||
| 144 | my $msg; | ||
| 145 | for $msg (@msgs) { | ||
| 146 | $t->insert('end', "$msg\n"); | ||
| 147 | } | ||
| 148 | $t->yview('ltb'); | ||
| 149 | $cw->deiconify; | ||
| 150 | $cw->raise(); | ||
| 151 | |||
| 152 | # $w->break if ($ans =~ /skip/i); | ||
| 153 | |||
| 154 | } # end Tk::Error | ||
| 155 | |||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | |||
| 160 | __END__ |