File size: 1,025 Bytes
19605ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';

var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var optionParser = require('./optionParser');

function ThresholdTrigger(options) {
    var base = new EventEmitter();

    var threshold = 10485760; // 10 MB

    var bytesWritten = 0;

    function newFile(data) {
        bytesWritten = data.stats.size;
    }

    function logWrite(data) {
        if ((bytesWritten > 0) && (bytesWritten + data.logSize > threshold)) {

            var date = null;
            try {
                var log = JSON.parse(data.logstr);
                date = log.time;
            } catch (e) {}

            base.emit('rotate', {date: date});
        } else {
            bytesWritten += data.logSize;
        }
    }

    function shutdown() {

    }

    threshold = optionParser.parseSize(options.threshold);

    return _.extend({}, {
        newFile: newFile,
        logWrite: logWrite,
        shutdown: shutdown
    }, base);
}

module.exports = ThresholdTrigger;