-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_function_info.js
More file actions
167 lines (139 loc) · 4.57 KB
/
send_function_info.js
File metadata and controls
167 lines (139 loc) · 4.57 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint-disable */
/**
* 被插桩的代码用 npm run 启动的时候才需要这句,否则需要屏蔽掉这句
*/
const axios = require("axios");
const callLogs = [];
let loggingEnabled = true; // 添加一个标志变量来控制日志记录是否启用
function sendLogs() {
// 打印日志,说明正在发送日志
console.log("正在发送日志");
if (callLogs.length !== 0) {
axios
.post("http://localhost:8087/log", callLogs, {
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
callLogs.length = 0;
console.log(response);
})
.catch((error) => {
console.log(error);
});
}
return true;
}
// 设置全局变量
global.sendLogs = sendLogs;
// 如果想定时发送而不是在chrome的控制台调用sendLogs发送,就打开这个函数
// setInterval(sendLogs, 3000);
// 定时查看日志数量 -- 测试
setInterval(getLogLength, 3000);
/**
* 最大日志数,超过这个数就发送日志,卡死了就改大一点
*/
const maxLogs = 5000000;
function addLog(log, log_, args, targetString) {
// 如果日志记录未启用,则直接返回
if (!loggingEnabled) {
return;
}
// 判断目标字符串是否是空字符串
if (targetString !== "") {
// 保存原始的堆栈跟踪限制
let originalStackTraceLimit = Error.stackTraceLimit;
// 设置无穷大的堆栈跟踪限制
Error.stackTraceLimit = Infinity;
for (let i = 0; i < args.length; i++) {
if (typeof args[i] === "string") {
// 判断参数是否时目标字符串
if (args[i].includes(targetString)) {
// 获取调用栈
var stack = new Error().stack;
// 删除调用栈前两行,第一行是error标识,第二行是addLog
stack = stack.split("\n").slice(2).join("\n");
// 打印调用栈
console.log(
"stack : \n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" +
stack +
"\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
);
}
}
}
// 恢复原始的堆栈跟踪限制
Error.stackTraceLimit = originalStackTraceLimit;
}
// // 打印原始日志 --测试
// console.log(log_);
// log_analysis后台有去重的操作,所以这里不需要去重,前端少做点事
callLogs.push(log);
if (callLogs.length > maxLogs) {
sendLogs();
}
}
// 查看日志数量
function getLogLength() {
// 打印当前日志数量 -- 测试
console.log("当前日志数量:" + callLogs.length);
return callLogs.length;
}
// 设置全局变量
global.getLogLength = getLogLength;
// 清空日志
function clearLog() {
callLogs.length = 0;
console.log("日志已清空。");
}
// 设置全局变量
global.clearLog = clearLog;
// 定义一个变量来存储定时器的ID
let logIntervalID;
// 开启定时器的函数;离开devtool进入界面时就会触发一些函数,这些函数并不是期望中要记录的函数,但是又没办法回到devtool中调用clearLog清空,这时候
// 可以启动定时器,每隔一段时间就清空一次日志,这样就可以避免记录一些不必要的函数;当真正需要发送日志的时候,可以调用stopInterval停止定时器
// 还有一种更简单的方法,对于不期望的函数就不要插桩,但是这样就会漏掉一些函数,所以还是用定时器的方法比较好,除非是非常明确的知道哪些函数不需要插桩
function startInterval() {
// 防止多个定时器同时运行
if (logIntervalID == null) {
console.log("定时发送启动,每10秒执行一次clearLog函数。");
logIntervalID = setInterval(clearLog, 10000);
}
}
// 停止定时器的函数
function stopInterval() {
if (logIntervalID != null) {
console.log("定时发送停止。");
clearInterval(logIntervalID);
logIntervalID = null;
}
}
// 把控制函数暴露给全局对象
global.startInterval = startInterval;
global.stopInterval = stopInterval;
// 添加stopLog函数
function stopLog() {
loggingEnabled = false;
console.log("日志记录已暂停。");
}
// 设置全局变量
global.stopLog = stopLog;
// 添加startLog函数
function startLog() {
loggingEnabled = true;
console.log("日志记录已启动。");
}
// 设置全局变量
global.startLog = startLog;
// 添加refreshPage函数
function refreshPage() {
console.log("正在刷新当前页面...");
window.location.reload();
}
// 设置全局变量
global.refreshPage = refreshPage;
// 导出addLog函数
module.exports = {
addLog,
};