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

How to monitor a log file in tcl/tk (async reads??)

602 views
Skip to first unread message

Bryan Cheung

unread,
Feb 8, 1996, 3:00:00 AM2/8/96
to

Hi,
I need to write a script that monitors a log file that is continuously
updated by a server process. The script would sit idle waiting for new
log entries, and process them as they come in. Can this be done in TCL??

Thanks,
-- Bryan Cheung
che...@EPLRX7.es.dupont.com

Larry V. Streepy Jr.

unread,
Feb 9, 1996, 3:00:00 AM2/9/96
to
Bryan Cheung wrote:
>
> Hi,
> I need to write a script that monitors a log file that is continuously
> updated by a server process. The script would sit idle waiting for new
> log entries, and process them as they come in. Can this be done in TCL??

Sure, take a look at the BLT package. Here is a URL for the Tcl/Tk
resource page listing lots of extensions. Look for BLT-1.9.0.

http://web.cs.ualberta.ca/~wade/Auto/HyperTcl/Tcl_extensions.html

--
Larry V. Streepy, Jr.
V.P. of Technology, Healthcare Communications, Inc.
str...@healthcare.com
(214) 851-7035

Kerry Schwab

unread,
Feb 9, 1996, 3:00:00 AM2/9/96
to
In article <311BA9...@healthcare.com>,

If you have tk4.0, the "fileevent" command would help. I understand that
in tcl7.5 they've moved the event loop into tcl. (instead of tk)

In that case you could do it from tclsh. If you wish to stick with tcl7.4,
see the scotty extensions that implement an event loop in tcl, and give
you access to fileevent in a tcl-only shell.

--
Kerry

Doug Hughes

unread,
Feb 10, 1996, 3:00:00 AM2/10/96
to
On 8 Feb 1996, Bryan Cheung wrote:

>
> Hi,
> I need to write a script that monitors a log file that is continuously
> updated by a server process. The script would sit idle waiting for new
> log entries, and process them as they come in. Can this be done in TCL??
>

> Thanks,
> -- Bryan Cheung
> che...@EPLRX7.es.dupont.com
>
>

Absolutely. I wrote such a program that watches syslog logs and will display
events in two different colors in near-realtime. I call it tklogger and it
is availbale as an example if you want it from ftp.eng.auburn.edu in
pub/doug directory.
You can also use the fileevent commands in tk (7.5a2 and below) or in Tcl
(7.5b1 and above)

____________________________________________________________________________
Doug Hughes Engineering Network Services
System/Net Admin Auburn University
do...@eng.auburn.edu
Pro is to Con as progress is to congress


Wayne Throop

unread,
Feb 11, 1996, 3:00:00 AM2/11/96
to
:: Bryan Cheung
:: I need to write a script that monitors a log file that is

:: continuously updated by a server process. The script would sit idle
:: waiting for new log entries, and process them as they come in. Can
:: this be done in TCL??

: "Larry V. Streepy Jr." <str...@healthcare.com>
: Sure, take a look at the BLT package. Here is a URL for the Tcl/Tk


: resource page listing lots of extensions. Look for BLT-1.9.0.
: http://web.cs.ualberta.ca/~wade/Auto/HyperTcl/Tcl_extensions.html

You can also very likely get away doing something like this in
a vanilla wish or tcl executable, with no extensions. For example

set l [open "| tail -f $logfilepath" r]
while {[gets $l r]} {
# do something with record $r of logfile $l
someprocessorother $r
}
close $l

with some embelishments (eg, a way to terminate the tail; this will loop
forever otherwise, handling of failed [open], and some other possible
improvements) might do the trick, if you just needed a watchdog process.
If you needed an X-displaying monitor process, then perhaps something
like this monitoring code

proc cycle {} {
# four times a minute...
global logfilesize
if $logfilesize!=[file size .nntpgrplog] {
set l [open $logfilename r]
seek $l $logfilesize
set t [read $l]
# deal with added text $t in logfile $l
someprocessorother $t
incr logfilesize [string length $t]
}
after 15000 cycle
}
cycle

Again, error recovery and such left simplistic here. Note that
both of these alternatives are actually polling the file. Presumably,
some solution with asynchronous IO could also be done, but the
monitoring of a logfile on disk is fairly inherently polled, whether
that is delegated to "tail" as in the first example, or done
explicitly as in the second.
--
Wayne Throop throopw%sheol...@dg-rtp.dg.com
thr...@aur.alcatel.com

Michael Schumacher

unread,
Feb 12, 1996, 3:00:00 AM2/12/96
to
Kerry Schwab (ksc...@nyx10.cs.du.edu) wrote:
: In article <311BA9...@healthcare.com>,

: Larry V. Streepy Jr. <str...@healthcare.com> wrote:
: >Bryan Cheung wrote:
: >>
: >> Hi,
: >> I need to write a script that monitors a log file that is continuously
: >> updated by a server process. The script would sit idle waiting for new
: >> log entries, and process them as they come in. Can this be done in TCL??

[...]

: If you have tk4.0, the "fileevent" command would help.

Not sure about this -- filevent is based on select(), and remember
that "tail -f" does explicitly _not_ use select(), but periodically
looks for changes of the file's access time. I forgot the reason for
this behaviour, so I'd be happy if someone could elaborate on this.


mike

Kerry Schwab

unread,
Feb 12, 1996, 3:00:00 AM2/12/96
to
In article <4fn392$r...@hades.rz.uni-sb.de>,

The reason is that you'd need to seek to get the new data, select won't be
useful after eof. My idea was to use fileevent on a pipe from tail -f.
tail seems to be reasonably small and tight, and reading from it seems to
make more sense than trying to re-do the algorithm in tcl. (unix was supposed
to be a toolbox, right ?)

--
Kerry

Ray Mosley

unread,
Feb 12, 1996, 3:00:00 AM2/12/96
to
In article <4fdql0$2a...@dssrv01.ds.dupont.com>, Bryan Cheung <che...@eplrx7.es.dupont.com> writes:
|>
|> Hi,
|> I need to write a script that monitors a log file that is continuously
|> updated by a server process. The script would sit idle waiting for new
|> log entries, and process them as they come in. Can this be done in TCL??
|>
|> Thanks,
|> -- Bryan Cheung
|> che...@EPLRX7.es.dupont.com

set lf [open logfile r]
set csize [file size logfile]
while {1} {
set nsize [file size logfile]
if {$nsize > $csize} then {
set lf [open logfile r]
seek $lf $csize start
set newdata [read $lf]
close $lf
set csize $nsize
##
## process new data here
##
}
exec -- sleep 5
}


You did not indicate how or when you needed to terminate the loop.
If the file remains unchanged for 30 seconds, you could exit the
loop.

Ray Mosley M/S 2201
Texas Instruments, Inc. MSG ID: HRNX
Information Technology Group Telecom Systems Division
Austin, Texas
============================================================
e-mail: mos...@tsd.itg.ti.com | VOICE: 512-250-6496
PAGER: 800-551-0495 | FAX: 512-250-7104
Romans 8:1; 15:7 Ephesians 4:26-27, 29, 31-32
=============================================================

0 new messages