如何在TCL中跟踪日志文件

Vah*_*agn 6 linux logging tcl tail

假设有一个文件,log.txt并且会永久地附加某种日志.

我想在TCL环境中跟踪该文件.

我试过这个但是没用.

set log [open log.txt a]

for { } { true } { update; after 1000 } {

    # expected to get here the appended part
    read $log

    seek $log 0 end

}
Run Code Online (Sandbox Code Playgroud)

是否可以通过相同的文件句柄读取修改后的文件log,或者我必须关闭并重新打开文件log.txt

tail -f在TCL中是否有一种等效的Linux命令?

kos*_*tix 4

只需使用tail. 它比你更了解如何处理复杂的情况(你可以查看它的源代码)。

在我的一个项目中,我有这样的东西来监视由专有工具生成的跟踪文件:

set fd [open [list | tail --follow=name --retry --lines 0 $opt(trace) 2>@1]]
chan event $fd readable [list FollowTrace $fd]

proc FollowTrace fd {
  if {[gets $fd line] < 0} {
    set code [catch {close $fd} err]
    if {$code == 0} {
      set ::result 0
    } else {
      puts stderr $err
      set ::result 1
    }
    return
  }

  switch -regexp -matchvar parts -- $line {
    {Tm_Session::Open.*FileName=([^,]+)} {
       TryMakeLock [FullPathname [lindex $parts 1]]
     }
     {Tm_Session::Close.*FileName=([^,]+)} {
        StartUpload [lindex $parts 1]
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

一般的想法是,您tail在给定的文件上生成,然后进入事件循环并tail逐行处理输出。