Skip to content

Commit 8008015

Browse files
committed
initial commit
0 parents  commit 8008015

File tree

9 files changed

+591
-0
lines changed

9 files changed

+591
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
开发日志阅读器(DLR)
2+
=================
3+
4+
本工具适合阅读开发环境的日志。
5+
6+
传统的开发日志在终端输出,很多时候对日志进行跟踪、筛选、查找、格式化等工作会浪费较大的精力。
7+
8+
DLR (Development Log Reader) 把本地开发进程的输出内容通过 WebSocket 重定向到浏览器,支持实时查看、筛选、查找以及 JSON 自动识别和格式化工作,可以大幅提升日志阅读的效率。
9+
10+
![DLR 截图](./screenshot/1.png)
11+
12+
## 安装
13+
14+
```
15+
npm install dev-log-reader --global
16+
```
17+
18+
## 使用
19+
20+
```
21+
dlr -p 1234 -t QC_MAIN
22+
```

bin/reader.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const http = require('http');
6+
const child_process = require('child_process');
7+
const WebSocket = require('ws');
8+
9+
/** @type {WebSocket[]} */
10+
const clients = [];
11+
const logs = [];
12+
13+
let lastLineTial = {}, lastLineSendTimer = {};
14+
const produce = (data, source) => {
15+
clearTimeout(lastLineSendTimer[source]);
16+
const chunk = (lastLineTial[source] || '') + data;
17+
const lines = chunk.split(/\r?\n/g);
18+
lastLineTial[source] = lines.pop();
19+
for (let line of lines) {
20+
console.log(line);
21+
logs.push(line);
22+
}
23+
consume();
24+
lastLineSendTimer[source] = setTimeout(() => {
25+
if (lastLineTial[source]) {
26+
logs.push(lastLineTial[source]);
27+
lastLineTial[source] = '';
28+
consume();
29+
}
30+
}, 100);
31+
}
32+
33+
const consume = () => {
34+
if (clients.length) {
35+
for (let client of clients) {
36+
while (logs.length) {
37+
client.send(logs.shift());
38+
}
39+
}
40+
}
41+
};
42+
43+
const usage = () => {
44+
console.log('Usage: dlr [-p,--port=number] [-t,--title=title] -- command');
45+
process.exit(-1);
46+
};
47+
const args = process.argv.slice(2);
48+
const splitIndex = args.indexOf('--');
49+
50+
if (splitIndex === -1) usage();
51+
52+
const bootArgs = args.slice(0, splitIndex);
53+
const [taskCommand, ...taskArgs] = args.slice(splitIndex + 1);
54+
55+
let port = 1234, title = 'LogReader';
56+
57+
while (arg = bootArgs.shift()) {
58+
let value;
59+
if (arg === '-p' && (value = bootArgs.shift()) || /--port=(\d+)/.test(arg)) {
60+
port = parseInt(value || RegExp.$1, 10);
61+
if (isNaN(port)) {
62+
usage();
63+
break;
64+
}
65+
continue;
66+
}
67+
if (arg === '-t' && (value = bootArgs.shift()) || /--title=(.+)/.test(arg)) {
68+
title = value || RegExp.$1;
69+
if (!title) {
70+
usage();
71+
break;
72+
}
73+
continue;
74+
}
75+
}
76+
77+
const task = child_process.spawn(taskCommand, taskArgs);
78+
79+
task.stdout.on('data', data => produce(data.toString('utf8'), 'stdout'));
80+
task.stderr.on('data', data => produce(data.toString('utf8'), 'stderr'));
81+
82+
const server = http.createServer((req, res) => {
83+
res.writeHead(200, {
84+
'Content-Type': 'text/html',
85+
});
86+
res.write(fs.readFileSync(path.join(__dirname, '../static/index.html'), { encoding: 'utf8' }));
87+
res.end();
88+
});
89+
90+
const ws = new WebSocket.Server({ server });
91+
92+
ws.on('connection', client => {
93+
clients.push(client);
94+
client.on('close', () => {
95+
const index = clients.indexOf(client);
96+
if (index > -1) {
97+
clients.splice(index, 1);
98+
}
99+
});
100+
consume();
101+
});
102+
103+
server.listen(port);
104+
console.log(`Read logs in http://127.0.0.1:${port}?title=${encodeURIComponent(title)}`);

jsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"es2016"
5+
]
6+
},
7+
"typeAcquisition": {
8+
"enable": true
9+
}
10+
}

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "dev-log-reader",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"bin": {
10+
"dlr": "./bin/reader.js"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@types/node": "^8.0.33",
16+
"ws": "^3.2.0"
17+
}
18+
}

screenshot/1.png

383 KB
Loading

0 commit comments

Comments
 (0)