+proc file-read-lines {lvar body} {
+ upvar 1 $lvar l
+ global logfile poll_after lastactivity bufdata
+
+ if {![info exists logfile]} {
+ return
+ }
+ while 1 {
+ if {[catch { read $logfile } got]} {
+ file-error $got
+ return
+ }
+ if {[eof $logfile]} {
+ set ago [expr { [clock seconds] - $lastactivity }]
+ set poll_after [after [expr {( $ago < 10 ? 10 :
+ $ago > 3000 ? 3000 :
+ $ago ) * 100}] \
+ pollfile]
+ return
+ }
+ set lastactivity [clock seconds]
+
+ global bufdata
+ if {![regexp {^(.*?)[\r\n]+(.*)$} $got dummy lhs rhs]} {
+ append bufdata $got
+ return
+ }
+
+ set l "$bufdata$lhs"
+ set bufdata $rhs
+puts ">>$l<<"
+ uplevel 1 $body
+ }
+}
+
+proc with-file {body} {
+ global errorInfo errorCode
+ set rc [catch { uplevel 1 $body } result]
+ switch -exact $rc {
+ 0 {
+ # ok
+ return $result
+ }
+ 1 {
+ # error
+ shownotice red "Error reading logfile $currentfile:\n$emsg"
+ catch { close $logfile }
+ set logfile {}
+ }
+ default {
+ # 2 - 3 - 4 return, break, continue
+ return -code $rc $errorInfo $errorCode $result
+ }
+ 4 {
+ #
+ }
+
+
+proc pollfile {} {
+ global poll_after logfile currentfile
+ global errorCode errorInfo bufdata lastactivity
+
+ catch { after cancel $poll_after }
+ if {![string length $currentfile]} {
+ shownotice red "No log file selected. Use File / Open."
+ return
+ }
+ if {![info exists logfile]} {
+ set bufdata {}
+ set lastactivity [clock seconds]
+ if {[catch { set logfile [open $currentfile r] } emsg]} {
+ shownotice red "Error opening logfile $currentfile:\n$emsg"
+ return
+ }
+ shownotice \#000080 "Reading $currentfile"
+ if {[catch {
+ seek $logfile -1024 end
+ } emsg]} {
+ if {![string match {POSIX EINVAL *} $errorCode]} {
+ file-error $emsg
+ }
+ }
+ file-read-lines l { }
+ }
+ file-read-lines l {
+ process-line $l
+ }
+
+ with-file {
+
+ }
+
+ while 1 {
+ set data read
+ }
+}
+