-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·109 lines (93 loc) · 2.86 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"use strict";
var fs = require('fs'),
optimist = require('optimist'),
options,
config,
analyze, filterRaw, transform, filterCooked, map, reduce,
Source = require('./lib/ReadStreamLineEmitter.js'), source,
summary;
// parse command line arguments and options
options = optimist
.usage('Usage: $0 [options]')
.demand(0)
.options('encoding', {
'default': 'utf8',
alias: 'e',
describe: 'character-set of input'
})
.options('input', {
'default': null,
alias: 'i',
describe: 'path/name of the input-file (stdin if not set)'
})
.options('output', {
'default': null,
alias: 'o',
describe: 'path/name of the output-file (stdout if not set)'
})
.options('config', {
'default': null,
describe: 'path/name of configuration file (js)'
})
.options('header', {
'default': null,
describe: 'header text, e.g. site-name, server-name, ...'
})
.options('help', {
alias: 'h',
describe: 'show help and exit'
})
.argv;
// fast exit when demanding help ...
if (options.help) {
optimist.showHelp();
process.exit(1);
}
if (options.debug) {
console.log(options);
}
config = options.config ? require(options.config) : {};
analyze = config.analyze || require('./lib/LogLineAnalyzerApache.js');
filterRaw = config.filterRaw || function () { return true; };
transform = config.transform || require('./lib/DefaultTransformImpl.js');
filterCooked = config.filterCooked || function () { return true; };
map = config.map || require('./lib/DefaultMapImpl.js');
reduce = config.reduce || require('./lib/DefaultReduceImpl.js');
summary = {};
if (options.header) {
summary.header = options.header;
}
if (options.input) {
source = new Source(fs.createReadStream(options.input, {encoding: options.encoding}), options);
} else {
source = new Source(process.stdin, options);
}
analyzeToSummary(source, summary);
function analyzeToSummary(source, summary, callback) {
source.on('line', function (line) {
var hit = analyze(line),
cooked;
if (hit && filterRaw(hit)) {
cooked = transform(hit);
if (cooked && filterCooked(cooked)) {
map(cooked, function emit(input) {
reduce([input], summary);
});
}
}
});
source.on('end', function () {
if (options.output) {
fs.writeFileSync(options.output, JSON.stringify(summary, null, '\t'));
} else {
console.log(JSON.stringify(summary, null, '\t'));
}
if (callback) {
callback(null, summary);
}
});
if (!options.input) {
// stdin is paused by default, so let's start!
process.stdin.resume();
}
}