Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tailing a file in TCL

2 views
Skip to first unread message

Gregory M. Messner

unread,
Mar 9, 1997, 3:00:00 AM3/9/97
to

I have been trying to write a little TCL/tk program to tail a log file. It
sort of works but the read_cb is call continously, what am I doing wrong?

<------------------------------ CODE
FOLLOWS---------------------------------->

proc read_cb {} {
global fd
gets $fd line
.text insert end "$line\n"
}

text .text
button .b -text "Done" -command { global exit_app; set exit_app 1 }
pack .text .b
update

set fd [ open fax1 r ]
fconfigure $fd -blocking 0

fileevent $fd readable read_cb
tkwait variable exit_app

Carl D. Roth

unread,
Mar 9, 1997, 3:00:00 AM3/9/97
to

--Multipart_Sun_Mar__9_15:13:51_1997-1
Content-Type: text/plain; charset=US-ASCII

"Gregory M. Messner" <gmes...@4dcomm.com> writes:

> I have been trying to write a little TCL/tk program to tail a log file. It
> sort of works but the read_cb is call continously, what am I doing wrong?

> proc read_cb {} {


> global fd
> gets $fd line
> .text insert end "$line\n"
> }
>
> text .text
> button .b -text "Done" -command { global exit_app; set exit_app 1 }
> pack .text .b
> update
>
> set fd [ open fax1 r ]
> fconfigure $fd -blocking 0
>
> fileevent $fd readable read_cb
> tkwait variable exit_app

End-of-file is a valid fileevent, so your program spins after the last
line in the file.

I doubt (some one correct me here) that there is a clean way to do
this properly in tcl without polling. Here's what I would suggest:

proc read_cb {} {
global fd fd_pos fd_idle

if {[tell $fd] > $fd_pos} {
while {[gets $fd line] > -1} {


.text insert end "$line\n"
}
}

set fd_pos [tell $fd]
set fd_idle [after 1000 read_cb]
}

set fd_pos 0
read_cb

--Multipart_Sun_Mar__9_15:13:51_1997-1
Content-Type: text/plain; charset=US-ASCII

Carl D. Roth <ro...@cse.ucsc.edu> (finger for PGP public key)

--Multipart_Sun_Mar__9_15:13:51_1997-1--

Christian Krone

unread,
Mar 10, 1997, 3:00:00 AM3/10/97
to

Hello,

Carl D. Roth wrote:
> "Gregory M. Messner" <gmes...@4dcomm.com> writes:
> but the read_cb is call continously, what am I doing wrong?
> > proc read_cb {} {
> > global fd
> > gets $fd line

> > "$line\n"


> > }
> End-of-file is a valid fileevent, so your program spins after
> the last line in the file.
> I doubt (some one correct me here) that there is a clean way
> to do this properly in tcl without polling.

Here is my standard file event handler,
which tests the eof condition:

proc read_cb {inpipe} {
if [eof $inpipe] {
close $inpipe
} else {
.text insert end "[gets $inpipe]\n"
}
}
fileevent $fd readable [list read_cb $fd]

Hope, that helps
Krischan
--
Christian Krone, Varziner Str. 12, D-12161 Berlin
kris...@cs.tu-berlin.de
http://www.cs.tu-berlin.de/~krischan

Jose L Porcayo

unread,
Mar 10, 1997, 3:00:00 AM3/10/97
to

The end of file also triggers the fileevent. You need to
check this condition with the eof command before trying to
read from the file. Example:

proc read_cb {} {
global fd
if {![eof $fd]} {


gets $fd line
.text insert end "$line\n"
}
}

--
Jose L Porcayo email: 10366...@compuserve.com
Consultoria y Metodologia

0 new messages