This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathstorjshare-logs.js
executable file
·142 lines (118 loc) · 3.49 KB
/
storjshare-logs.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
'use strict';
const config = require('../lib/config/daemon');
const utils = require('../lib/utils');
const {Tail} = require('tail');
const colors = require('colors/safe');
const storjshare_logs = require('commander');
const fs = require('fs');
const path = require('path');
const FsLogger = require('fslogger');
storjshare_logs
.description('tails the logs for the given share id')
.option('-i, --nodeid <nodeid>', 'id of the running share')
.option('-l, --lines <num>', 'lines back to print')
.option('-r, --remote <hostname:port>',
'hostname and optional port of the daemon')
.parse(process.argv);
if (!storjshare_logs.nodeid) {
console.error('\n missing node id, try --help');
process.exit(1);
}
function getLastLines(filename, lines, callback) {
let chunk = '';
let size = Math.max(0, fs.statSync(filename).size - (lines * 200));
fs.createReadStream(filename, { start: size })
.on('data', function(data) {
chunk += data.toString();
})
.on('end', function() {
chunk = chunk.split('\n').slice(-(lines + 1));
chunk.pop();
callback(chunk);
});
}
function prettyLog(line) {
var output = ' ';
try {
line = JSON.parse(line);
} catch (err) {
return;
}
switch (line.level) {
case 'debug':
output += colors.magenta('[ debug ] ');
break;
case 'info':
output += colors.cyan('[ info ] ');
break;
case 'warn':
output += colors.yellow('[ warn ] ');
break;
case 'error':
output += colors.red('[ error ] ');
break;
default:
// noop
}
output += colors.gray( `[ ${line.timestamp} ]\n [ `);
output += `${line.message}`;
console.log(output);
}
let port = config.daemonRpcPort;
let address = null;
if (storjshare_logs.remote) {
address = storjshare_logs.remote.split(':')[0];
if (storjshare_logs.remote.split(':').length > 1) {
port = parseInt(storjshare_logs.remote.split(':')[1], 10);
}
}
utils.connectToDaemon(port, function(rpc, sock) {
process.on('exit', () => {
sock.end();
process.exit(0);
});
rpc.status((err, shares) => {
if (err) {
console.error(`\n cannot get status, reason: ${err.message}`);
return sock.end();
}
let logFileDir = null;
for (let i = 0; i < shares.length; i++) {
if (shares[i].id === storjshare_logs.nodeid) {
logFileDir = shares[i].config.loggerOutputFile;
break;
}
}
try {
if (!fs.statSync(logFileDir).isDirectory()) {
logFileDir = path.dirname(logFileDir);
}
} catch (err) {
logFileDir = path.dirname(logFileDir);
}
const fslogger = new FsLogger(logFileDir, storjshare_logs.nodeid);
let currentFile = null;
let logTail = null;
setInterval(function() {
if (currentFile !== fslogger._todaysFile()) {
if (logTail instanceof Tail) {
logTail.unwatch();
}
currentFile = fslogger._todaysFile();
if (!utils.existsSync(fslogger._todaysFile())) {
console.error(`\n no logs to show for ${storjshare_logs.nodeid}`);
return sock.end();
}
logTail = new Tail(fslogger._todaysFile());
let numLines = storjshare_logs.lines
? parseInt(storjshare_logs.lines)
: 20;
getLastLines(fslogger._todaysFile(), numLines, (lines) => {
lines.forEach((line) => prettyLog(line));
logTail.on('line', (line) => prettyLog(line));
});
}
}, 1000);
});
}, address);