-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.ts
124 lines (121 loc) · 4.38 KB
/
functions.ts
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
import fs from "fs";
const regexp = /^(([^\(]|\s)+)\((.*)\)$/;
interface logObject {
server: string,
category: string,
value: string[],
timestamp: number,
title: string
}
function initLog(path: string) {
let logJson: logObject[] = [];
fs.readFile(path, (err, data) => {
if (err) console.log(err);
else {
let jsonData = JSON.parse(data.toString() || "null");
if (Array.isArray(jsonData)) {
logJson = logJson.concat(jsonData);
}
else if (typeof jsonData === 'object' && jsonData != null) {
logJson.push(jsonData);
}
}
});
return logJson;
}
function initFileSize(path: string) {
let logFileSize = 0;
fs.readFile(path, (err, data) => {
if (err) console.log(err);
else {
logFileSize = data.toString().length;
}
});
return logFileSize;
}
function updateLog(path: string, jsonPath: string, size: number, time: number, logJson: logObject[], isError: boolean, server: string) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) console.log(err);
else {
let fileChange = data.toString().slice(size);
fileChange = fileChange.replace(/\n/g, '<br>');
if(!isError)
{
let logs: string[] = fileChange.split('\`');
logs.forEach((element: string) => {
console.log(element, 1);
if(element.startsWith("<br>"))
{
element = element.slice(4);
}
let match = element.match(regexp);
console.log(match, 2);
if (match) {
let valueArray = [];
if(match[3].length < 8000)
{
valueArray.push(match[3]);
}
else
{
let temp = match[3];
while(temp.length > 8000)
{
valueArray.push(temp.slice(0, 8000));
temp = temp.slice(8000);
}
valueArray.push(temp);
}
const logInstance: logObject =
{
server: server,
category: 'info',
value: valueArray,
timestamp: time,
title: match[1]
};
logJson.push(logInstance);
}
})
fs.writeFile(jsonPath, JSON.stringify(logJson), (err) => {
if (err) console.log(err);
});
}
else
{
let valueArray = [];
if(fileChange.length < 8000)
{
valueArray.push(fileChange);
}
else
{
let temp = fileChange;
while(temp.length > 8000)
{
valueArray.push(temp.slice(0, 8000));
temp = temp.slice(8000);
}
valueArray.push(temp);
}
const logInstance: logObject =
{
server: server,
category: 'error',
value: valueArray,
timestamp: time,
title: 'error'
};
logJson.push(logInstance);
fs.writeFile(jsonPath, JSON.stringify(logJson), (err) => {
if (err) console.log(err);
});
}
}
size = data.toString().length;
resolve(size);
});
})
}
module.exports = { initLog, initFileSize, updateLog };