]> gb7djk.dxcluster.net Git - spider.git/blob - gtkconsole/DebugHandler.pm
fix DXCron, lockout and close_gracefully
[spider.git] / gtkconsole / DebugHandler.pm
1 #
2 # Gtk Handler for Debug Files
3 #
4
5 package DebugHandler;
6
7 use strict;
8
9 use Gtk;
10 use DXVars;
11 use DXLog;
12 use DXUtil;
13
14 use vars qw(@ISA);
15 @ISA = qw(Gtk::Window);
16
17 sub new
18 {
19         my $pkg = shift;
20         my $parent = shift;
21         my $regexp = shift || '';
22         my $nolines = shift || 1;
23         
24         my $self = new Gtk::Window;
25         bless  $self, $pkg;
26         $self->set_default_size(400, 400);
27         $self->set_transient_for($parent) if $parent;
28         $self->signal_connect('destroy', sub {$self->destroy} );
29         $self->signal_connect('delete_event', sub {$self->destroy; return undef;});
30         $self->set_title("Debug Output - $regexp");
31         $self->border_width(0);
32         $self->show;
33         
34         my $box1 = new Gtk::VBox(0, 0);
35         $self->add($box1);
36         $box1->show;
37         
38         my $swin = new Gtk::ScrolledWindow(undef, undef);
39         $swin->set_policy('automatic', 'automatic');
40         $box1->pack_start($swin, 1, 1, 0);
41         $swin->show;
42         
43         my $button = new Gtk::Button('close');
44         $button->signal_connect('clicked', sub {$self->destroy});
45         $box1->pack_end($button, 0, 1, 0);
46         $button->show;
47         
48         my $clist = new_with_titles Gtk::CList('Time', 'Data');
49         $swin->add($clist);
50         $clist->show;
51         
52         $self->{fp} = DXLog::new('debug', 'dat', 'd');
53         
54         my @today = Julian::unixtoj(time);
55         my $fh = $self->{fh} = $self->{fp}->open(@today);
56         $fh->seek(0, 2);
57         $self->{regexp} = $regexp if $regexp;
58         $self->{nolines} = $nolines;
59         $self->{clist} = $clist;
60
61         $self->{id} = Gtk::Gdk->input_add($fh->fileno, ['read'], sub {$self->handleinp(@_); 1;}, $fh);
62         
63         $self->show_all;
64         return $self;
65 }
66
67 sub destroy
68 {
69         my $self = shift;
70         $self->{fp}->close;
71         Gtk::Gdk->input_remove($self->{id});
72         delete $self->{clist};
73 }
74
75 sub handleinp
76 {
77         my ($self, $socket, $fd, $flags) = @_;
78         if ($flags->{read}) {
79                 my $offset = exists $self->{rbuf} ? length $self->{rbuf} : 0; 
80                 my $l = sysread($socket, $self->{rbuf}, 1024, $offset);
81                 if (defined $l) {
82                         if ($l) {
83                                 while ($self->{rbuf} =~ s/^([^\015\012]*)\015?\012//) {
84                                         my $line = $1;
85                                         if ($self->{regexp}) {
86                                                 push @{$self->{prev}}, $line;
87                                                 shift @{$self->{prev}} while @{$self->{prev}} > $self->{nolines}; 
88                                                 if ($line =~ m{$self->{regexp}}oi) {
89                                                         $self->printit(@{$self->{prev}});       
90                                                         @{$self->{prev}} = [];
91                                                 }
92                                         } else {
93                                                 $self->printit($line);
94                                         }
95                                 }
96                         }
97                 }
98         }
99 }
100
101 sub printit
102 {
103         my $self = shift;
104         my $clist = $self->{clist};
105         while (@_) {
106                 my $line = shift;
107                 $line =~ s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02X", ord($1))/eg; 
108                 my @line =  split /\^/, $line, 2;
109                 my $t = shift @line;
110                 my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
111                 my $buf = sprintf "%02d:%02d:%02d", $hour, $min, $sec;
112                 $clist->append($buf, @line);
113         }
114 }
115 1;