【问题标题】:Node.js ReadLine Line by Line LoopNode.js ReadLine 逐行循环
【发布时间】:2018-11-29 22:18:09
【问题描述】:

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

我想逐行读取文件文本并在找到某些单词时打印警报。 阅读完所有文本后,计时器会在最后一个结束行重新启动。 它工作 3 或 4 次,在计数 (cpt) 出错并在读取整个日志文件之前中断。 它以错误的索引(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);

【问题讨论】:

  • 不清楚你在问什么。您能否提供一些来自myLog.log 文件的示例输入在错误之前和之后以及您看到的结果?

标签: node.js readline


【解决方案1】:

这是一个文本文件,有 +100000 行,30 个月。该文件实时递增。这就是为什么我每 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

【讨论】:

    【解决方案2】:

    我找到了:) 我替换:

     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" 只启动一次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-02
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多