| 1 |
|
| ##============================================================================== |
| 2 |
|
| ## Tk::Stderr - capture program standard error output |
| 3 |
|
| ##============================================================================== |
| 4 |
|
| ## $Id: Stderr.pm,v 1.2 2003/04/01 03:58:42 kevin Exp $ |
| 5 |
|
| ##============================================================================== |
| 6 |
|
| require 5.006; |
| 7 |
|
| |
| 8 |
|
| package Tk::Stderr; |
| 9 |
|
| use strict; |
| 10 |
|
| use warnings; |
| 11 |
|
| use vars qw($VERSION @ISA); |
| 12 |
|
| ($VERSION) = q$Revision: 1.2 $ =~ /Revision:\s+(\S+)/ or $VERSION = "0.0"; |
| 13 |
|
| use base qw(Tk::Derived Tk::MainWindow); |
| 14 |
|
| |
| 15 |
|
| use Tk::ROText; |
| 16 |
|
| use Tk::Frame; |
| 17 |
|
| |
| 18 |
|
| =pod |
| 19 |
|
| |
| 20 |
|
| =head1 NAME |
| 21 |
|
| |
| 22 |
|
| Tk::Stderr - capture standard error output, display in separate window |
| 23 |
|
| |
| 24 |
|
| =head1 SYNOPSIS |
| 25 |
|
| |
| 26 |
|
| use Tk::Stderr; |
| 27 |
|
| |
| 28 |
|
| $mw = MainWindow->new->InitStderr; |
| 29 |
|
| print STDERR 'stuff'; ## goes to standard error window |
| 30 |
|
| warn 'eek!'; ## likewise |
| 31 |
|
| |
| 32 |
|
| =head1 DESCRIPTION |
| 33 |
|
| |
| 34 |
|
| This module captures that standard error of a program and redirects it to a read |
| 35 |
|
| only text widget, which doesn't appear until necessary. When it does appear, the |
| 36 |
|
| user can close it; it'll appear again when there is more output. |
| 37 |
|
| |
| 38 |
|
| =cut |
| 39 |
|
| |
| 40 |
|
| ##============================================================================== |
| 41 |
|
| ## Populate |
| 42 |
|
| ##============================================================================== |
| 43 |
|
| sub Populate { |
| 44 |
|
| my ($mw, $args) = @_; |
| 45 |
|
| my $private = $mw->privateData; |
| 46 |
|
| $private->{ReferenceCount} = 0; |
| 47 |
|
| $private->{Enabled} = 0; |
| 48 |
|
| |
| 49 |
|
| $mw->SUPER::Populate($args); |
| 50 |
|
| |
| 51 |
|
| $mw->withdraw; |
| 52 |
|
| $mw->protocol(WM_DELETE_WINDOW => [ $mw => 'withdraw' ]); |
| 53 |
|
| |
| 54 |
|
| my $f = $mw->Frame( |
| 55 |
|
| Name => 'stderr_frame', |
| 56 |
|
| )->pack(-fill => 'both', -expand => 1); |
| 57 |
|
| |
| 58 |
|
| my $text = $f->Scrolled( |
| 59 |
|
| 'ROText', |
| 60 |
|
| Name => 'stderr_text', |
| 61 |
|
| -scrollbars => 'osoe', |
| 62 |
|
| )->pack(-fill => 'both', -expand => 1); |
| 63 |
|
| |
| 64 |
|
| $mw->Advertise('text' => $text); |
| 65 |
|
| |
| 66 |
|
| $mw->ConfigSpecs( |
| 67 |
|
| '-title' => [ qw/METHOD title Title/, 'Standard Error Output' ], |
| 68 |
|
| ); |
| 69 |
|
| |
| 70 |
|
| $mw->Redirect(1); |
| 71 |
|
| |
| 72 |
|
| return $mw; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| ##============================================================================== |
| 76 |
|
| ## Redirect |
| 77 |
|
| ##============================================================================== |
| 78 |
|
| sub Redirect { |
| 79 |
|
| my ($mw, $boolean) = @_; |
| 80 |
|
| my $private = $mw->privateData; |
| 81 |
|
| my $old = $private->{Enabled}; |
| 82 |
|
| |
| 83 |
|
| if ($old && !$boolean) { |
| 84 |
|
| untie *STDERR; |
| 85 |
|
| $SIG{__WARN__} = 'DEFAULT'; |
| 86 |
|
| } elsif (!$old && $boolean) { |
| 87 |
|
| tie *STDERR, 'Tk::Stderr::Handle', $mw; |
| 88 |
|
| $SIG{__WARN__} = sub { print STDERR @_ }; |
| 89 |
|
| } |
| 90 |
|
| $private->{Enabled} = $boolean; |
| 91 |
|
| return $old; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| ##============================================================================== |
| 95 |
|
| ## DecrementReferenceCount |
| 96 |
|
| ##============================================================================== |
| 97 |
|
| sub DecrementReferenceCount { |
| 98 |
|
| my ($mw) = @_; |
| 99 |
|
| my $private = $mw->privateData; |
| 100 |
|
| |
| 101 |
|
| if (--$private->{ReferenceCount} <= 0) { |
| 102 |
|
| $mw->destroy; |
| 103 |
|
| } |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| ##============================================================================== |
| 107 |
|
| ## IncrementReferenceCount |
| 108 |
|
| ##============================================================================== |
| 109 |
|
| sub IncrementReferenceCount { |
| 110 |
|
| my ($mw) = @_; |
| 111 |
|
| my $private = $mw->privateData; |
| 112 |
|
| |
| 113 |
|
| ++$private->{ReferenceCount}; |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| =pod |
| 117 |
|
| |
| 118 |
|
| =head1 METHODS |
| 119 |
|
| |
| 120 |
|
| These are actually added to the MainWindow class. |
| 121 |
|
| |
| 122 |
|
| =over 4 |
| 123 |
|
| |
| 124 |
|
| =item I<$mw>->InitStderr; |
| 125 |
|
| |
| 126 |
|
| The first time this method called, it does the following things: |
| 127 |
|
| |
| 128 |
|
| =over 4 |
| 129 |
|
| |
| 130 |
|
| =item o |
| 131 |
|
| |
| 132 |
|
| Creates a MainWindow holding a read-only scrollable text widget, and withdraws |
| 133 |
|
| this window until it's actually needed. |
| 134 |
|
| |
| 135 |
|
| =item o |
| 136 |
|
| |
| 137 |
|
| Ties STDERR to a special handle that adds the output to this text widget. |
| 138 |
|
| |
| 139 |
|
| =item o |
| 140 |
|
| |
| 141 |
|
| Installs a C<< $SIG{__WARN__} >> handler that redirects the output from B<warn> |
| 142 |
|
| to this window as well (by printing it to STDERR). |
| 143 |
|
| |
| 144 |
|
| =back |
| 145 |
|
| |
| 146 |
|
| On the remaining calls, it: |
| 147 |
|
| |
| 148 |
|
| =over 4 |
| 149 |
|
| |
| 150 |
|
| =item o |
| 151 |
|
| |
| 152 |
|
| Increments a reference count of "other" MainWindows. |
| 153 |
|
| |
| 154 |
|
| =item o |
| 155 |
|
| |
| 156 |
|
| Installs an OnDestroy handler that decrements this reference count, so that it |
| 157 |
|
| can detect when it's the only MainWindow left and destroy itself. |
| 158 |
|
| |
| 159 |
|
| =back |
| 160 |
|
| |
| 161 |
|
| =cut |
| 162 |
|
| |
| 163 |
|
| package MainWindow; |
| 164 |
|
| use strict; |
| 165 |
|
| use warnings; |
| 166 |
|
| |
| 167 |
|
| my $error_window; |
| 168 |
|
| |
| 169 |
|
| ##============================================================================== |
| 170 |
|
| ## InitStderr |
| 171 |
|
| ##============================================================================== |
| 172 |
|
| sub InitStderr { |
| 173 |
|
| my ($mw, $title) = @_; |
| 174 |
|
| |
| 175 |
|
| unless (defined $error_window) { |
| 176 |
|
| $error_window = Tk::Stderr->new; |
| 177 |
|
| $error_window->title($title) if defined $title; |
| 178 |
|
| } |
| 179 |
|
| $error_window->IncrementReferenceCount; |
| 180 |
|
| $mw->OnDestroy([ 'DecrementReferenceCount' => $error_window ]); |
| 181 |
|
| return $mw; |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
|
| =pod |
| 185 |
|
| |
| 186 |
|
| =item I<$errwin> = I<$mw>->StderrWindow; |
| 187 |
|
| |
| 188 |
|
| Returns a reference to the main window holding the text. You can use this to |
| 189 |
|
| configure the window itself or the widgets it contains. The only advertised |
| 190 |
|
| subwidget is 'text', which is the scrolled read-only text widget. |
| 191 |
|
| |
| 192 |
|
| =cut |
| 193 |
|
| |
| 194 |
|
| ##============================================================================== |
| 195 |
|
| ## StderrWindow |
| 196 |
|
| ##============================================================================== |
| 197 |
|
| sub StderrWindow { |
| 198 |
|
| return $error_window; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
|
| =pod |
| 202 |
|
| |
| 203 |
|
| =item I<$old> = I<$mw>->RedirectStderr(I<$boolean>); |
| 204 |
|
| |
| 205 |
|
| Enables or disables the redirection of standard error to the text window. Set |
| 206 |
|
| I<$boolean> to true to enable redirection, false to disable it. Returns the |
| 207 |
|
| previous value of the enabled flag. |
| 208 |
|
| |
| 209 |
|
| If B<InitStderr> has never been called, this routine will call it if I<$boolean> |
| 210 |
|
| is true. |
| 211 |
|
| |
| 212 |
|
| =cut |
| 213 |
|
| |
| 214 |
|
| ##============================================================================== |
| 215 |
|
| ## RedirectStderr |
| 216 |
|
| ##============================================================================== |
| 217 |
|
| sub RedirectStderr { |
| 218 |
|
| my ($mw, $boolean) = @_; |
| 219 |
|
| |
| 220 |
|
| unless (defined $error_window) { |
| 221 |
|
| $mw->InitStderr if $boolean; |
| 222 |
|
| return; |
| 223 |
|
| } |
| 224 |
|
| return $error_window->Redirect($boolean); |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
|
| =pod |
| 228 |
|
| |
| 229 |
|
| =back |
| 230 |
|
| |
| 231 |
|
| =head1 AUTHOR |
| 232 |
|
| |
| 233 |
|
| Kevin Michael Vail <F<kevin>@F<vaildc>.F<net>> |
| 234 |
|
| |
| 235 |
|
| =cut |
| 236 |
|
| |
| 237 |
|
| ##============================================================================== |
| 238 |
|
| ## Define the handle that actually implements things. |
| 239 |
|
| ##============================================================================== |
| 240 |
|
| BEGIN { |
| 241 |
|
| package Tk::Stderr::Handle; |
| 242 |
|
| use strict; |
| 243 |
|
| use warnings; |
| 244 |
|
| |
| 245 |
|
| ##========================================================================== |
| 246 |
|
| ## TIEHANDLE |
| 247 |
|
| ##========================================================================== |
| 248 |
|
| sub TIEHANDLE { |
| 249 |
|
| my ($class, $window) = @_; |
| 250 |
|
| bless \$window, $class; |
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
|
| ##========================================================================== |
| 254 |
|
| ## PRINT |
| 255 |
|
| ##========================================================================== |
| 256 |
|
| sub PRINT { |
| 257 |
|
| my $window = shift; |
| 258 |
|
| my $text = $$window->Subwidget('text'); |
| 259 |
|
| |
| 260 |
|
| $text->insert('end', $_) foreach (@_); |
| 261 |
|
| $text->see('end'); |
| 262 |
|
| $$window->deiconify; |
| 263 |
|
| $$window->raise; |
| 264 |
|
| $$window->focus; |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
|
| ##========================================================================== |
| 268 |
|
| ## PRINTF |
| 269 |
|
| ##========================================================================== |
| 270 |
|
| sub PRINTF { |
| 271 |
|
| my ($window, $format) = splice @_, 0, 2; |
| 272 |
|
| |
| 273 |
|
| $window->PRINT(sprintf $format, @_); |
| 274 |
|
| } |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
|
| 1; |
| 278 |
|
| |
| 279 |
|
| ##============================================================================== |
| 280 |
|
| ## $Log: Stderr.pm,v $ |
| 281 |
|
| ## Revision 1.2 2003/04/01 03:58:42 kevin |
| 282 |
|
| ## Add RedirectStderr method to allow redirection to be switched on and off. |
| 283 |
|
| ## |
| 284 |
|
| ## Revision 1.1 2003/03/26 21:48:43 kevin |
| 285 |
|
| ## Fix dependencies in Makefile.PL |
| 286 |
|
| ## |
| 287 |
|
| ## Revision 1.0 2003/03/26 19:11:32 kevin |
| 288 |
|
| ## Initial revision |
| 289 |
|
| ##============================================================================== |