Node.js ReadLine逐行循环

时间:2018-06-20 17:35:28

标签: node.js readline

自动重新加载脚本时出错。

我想逐行阅读文件文本,并在发现一些单词时打印警报。 阅读完所有文本后,计时器将在最后一个上一行结束时重启。 在计数(cpt)出错并中断后,它会工作3或4次,然后读取整个日志文件。 它以错误的索引(cpt)重新启动

对不起,我的英语不好

  function TimeOut(str1){
    console.log('Wait 60s');
    setInterval(function () {
         processFile('myLog.log',str1);
            }, 60000);
}

function processFile(inputFile,nbline) {
    console.log('Start process');
    var fs = require('fs'),
        readline = require('readline'),
        instream = fs.createReadStream(inputFile),
        rl = readline.createInterface({
            input: instream,
            crlfDelay: Infinity
        });
        cpt = 0;

    rl.on('line', function (line) {
        if(cpt++>=nbline){
        var mySearch = line.search(/ConanSandbox: Purge Started at/i);
        if(mySearch !== -1) {
            console.log(cpt + " " +line);
            }
        }
    });

    rl.on('close', function (line) {
        console.log("End Line: " + cpt);
        console.log('End process');
        TimeOut(cpt);
        instream.destroy();
    });


}

processFile('myLog.log',0);

2 个答案:

答案 0 :(得分:0)

这是一个带有+100000行,30mo的文本文件。该文件实时增加。这就是为什么我每60秒进行一次更新并从上一条结束行开始。

结果(错误):

          Start process
        103599 [2018.06.20-15.10.48:088][755]ConanSandbox: Purge Started at V(X=179144.70, Y=109670.14, Z=-18113.97) for Clan 165634, Using Wave Une troupe de grands singes gris
        128083 [2018.06.20-16.23.35:261][815]ConanSandbox: Purge Started at V(X=-281017.59, Y=68414.27, Z=-4400.66) for Clan 7457, Using Wave Une nuée de sauterelles
        152067 [2018.06.20-17.37.42:737][187]ConanSandbox: Purge Started at V(X=228126.78, Y=143240.03, Z=-17717.29) for Clan 138501, Using Wave Une troupe de grands singes gris
        169891 [2018.06.20-18.50.29:828][840]ConanSandbox: Purge Started at V(X=-49786.84, Y=229507.47, Z=-20192.92) for Clan 114258, Using Wave Une horde de hyènes
        Last Line: 198266
        End process
        Wait 60s
//good result
        Start process
        Last Line: 198637
        End process
        Wait 60s
//good result
        Start process
        198721 [2018.06.20-20.13.40:770][800]ConanSandbox: Purge Started at V(X=76036.11, Y=166601.59, Z=-17867.28) for Clan 4672, Using Wave Une horde de hyènes
        Last Line: 199016
        End process
        Wait 60s
//good result
    Start process
    Last Line: 4851 // <= why 4851 before is 199016 for list line
    End process
    Wait 60s
//Wrong result
// After result is not correct :/

myLog.log的某些行:

[2018.06.13-15.11.43:016][ 29]BattlEyeLogging: BattlEyeServer: Player 5 is now In Play
[2018.06.13-15.11.44:248][ 66]Network:Warning: Data: FunCombat_PlayerController_C_5 (server) sent 1320.0 bytes per second > 1024 (sample window size: 2.00 seconds)
[2018.06.13-15.11.50:239][244]Network:Warning: Data: BasePlayerChar_C_86 (server) sent 1055.0 bytes per second > 1024 (sample window size: 2.00 seconds)
[2018.06.13-15.11.52:237][303]Network:Warning: Data: BasePlayerChar_C_84 (server) sent 1588.0 bytes per second > 1024 (sample window size: 2.00 seconds)
[2018.06.13-15.11.52:240][303]Network:Warning: Data: BasePlayerChar_C_84 (server) sent 46.0 parts per second > 40 (sample window size: 2.00 seconds)
[2018.06.13-15.11.52:240][303]Network:Warning: Data: BasePlayerChar_C_84 (server) received 2400.0 bytes per second > 1024 (sample window size: 2.00 seconds)
[2018.06.13-15.11.55:079][388]LogNet: Server connection received: ActorChannelFailure
[2018.06.13-15.12.02:205][594]Combat:Display: [BasePlayerChar_C_73] Start: 1710.579956 End-1963.349365 Fall Distance: 3673.929199
[2018.06.13-15.12.02:205][594]Combat:Display: [BasePlayerChar_C_73]  Falling Velocity:-2697.634521 Percent of deadly fall velocity: 1.212596
[2018.06.13-15.12.02:208][594]Combat:Display: [BasePlayerChar_C_73] Final fall damage:218.0
[2018.06.13-15.12.02:241][595]Network:Warning: Data: BasePlayerChar_C_86 (server) sent 1057.5 bytes per second > 1024 (sample window size: 2.00 seconds)
[2018.06.13-15.12.03:298][626]ConanSandbox: Purge Started at V(X=76636.86, Y=238970.73, Z=-20249.11) for Clan 147412, Using Wave Une horde de hyènes
[2018.06.13-15.12.03:533][633]LogUObjectBase:Warning: NULL object
[2018.06.13-15.12.03:536][633]LogUObjectBase:Warning: NULL object

答案 1 :(得分:0)

我发现了:) 我替换:

 function TimeOut(str1){
    console.log('Wait 60s');
    setInterval(function () {
         processFile('myLog.log',str1);
            }, 60000);
}

作者:

function TimeOut(str1){
    console.log('Wait 60s');
    setTimeout(function() {
         processFile('myLog.log',str1);
    }, 60000);
}

因为“ setInterval”重复了太多次 “ seTimeout”仅启动一次