在大文件中连续搜索模式的新术语有什么好方法?

时间:2015-03-16 06:40:14

标签: tcl

AUT为特定函数运行创建日志,并将日志附加到中央文件中。 要在此文件中搜索的行是:

LatestTimeStamp>MyFunction   SomeStep timeLapsed  SOME_TIME_VALUE

每次通过AUT生成日志时,都会生成与上面类似模式的新的多个日志,并且需要提取这些新日志。

我使用的简单方法是:

类结构

itcl::class clsLogs {
    variable _oldTimeStamp ""
    variable _logRec
    variable _runCtr 0
    method _extractInfoForRun {runType} {
        #read log
        catch {close $fp}
        set log [read [set fp [open [file join [file normalize $env(APPDATA)] Logs Action.log]]]]

        #garbage everything before old time stamp and collect all fresh log
        if {[info exists _oldTimeStamp] && $_oldTimeStamp!=""} {
            regsub [subst -nobackslashes -nocommands {.*$_oldTimeStamp[^\n]*\n}] [set freshLog $log] "" freshLog
        }

        #increment run counter for this run
        incr _runCtr

        #get all fresh entry lines for reporting timelapsed for different steps of MyFunction in this run
        set freshEntries [regexp -inline -all [subst -nocommands -nobackslashes {[^\n]*MyFunction[^\n]*timeLapsed[^\n]*}] $freshLog]

        #iterate and collect time lapsed info for each step of MyFunction for this run
        foreach ent $freshEntries { 
            regexp {(.*?)>.*>>MyFunction\s+(.*)\s+timeLapsed\s+(.*)$} $ent -> timeStamp runStep lapsedTime ;
            puts ************runTyp>$runTyp***********\n\t$ent\n\ttimeStamp->$timeStamp\nlapsedTime->$lapsedTime
            set _logRec(MyFunction_Run-$_runCtr:$runStep,lapsedTime) $lapsedTime
        }           

        #reset old time stamp variable for next run
        set _oldTimeStamp $timeStamp
    }
}

但是这个文件可能很大并且将所有内容存储在一个读取输出变量中可能会导致溢出:

set log [read [set fp [open [file join [file normalize $env(APPDATA)] Logs Action.log]]]]

是否有可能使用组合来获取文件指针的当前位置并使用它来偏移到最后一个光标位置然后从该位置开始每次读取? 同一个Tcl命令选项是什么?

1 个答案:

答案 0 :(得分:0)

所以这样做:

seek [set fp [open $file]] $_fOffset
set txt [read $fp]
set _fOffset [tell $fp]

在上下文中:

::itcl::class clsLogs {
    private {
        variable _fOffset 0     
    }
    public {
        method _fFreshRead {file args} {
            set options(-resetOffSet) false
            array set options $args         
            if {$options(-resetOffSet)} {
                set _fOffset 0
            }
            seek [set fp [open $file]] $_fOffset            
            set txt [read $fp]
            set _fOffset [tell $fp]
            close $fp
            return $txt
        }
    }
}
相关问题