FloatLogBus.js
4.78 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { LogLevel } from './constants'
import { serializeIfObject } from '../utils'
import FloatLogConfig from './FloatLogConfig.js'
const MAX_LINES = 400
const PIN_TAG = '[StoragePathMerge]'
const MAX_PINNED_LINES = 60
function trimLines() {
if (lines.length <= MAX_LINES) return
const pinned = lines.filter(l => l.text.includes(PIN_TAG))
const normal = lines.filter(l => !l.text.includes(PIN_TAG))
const pinnedKeep = pinned.slice(-MAX_PINNED_LINES)
const normalBudget = Math.max(0, MAX_LINES - pinnedKeep.length)
const normalKeep = normal.slice(-normalBudget)
lines = [...normalKeep, ...pinnedKeep]
lines.sort((a, b) => {
const ta = parseInt(String(a.id).split('_')[0], 10) || 0
const tb = parseInt(String(b.id).split('_')[0], 10) || 0
return ta - tb
})
}
let lines = []
/** 路径合并等启动期日志,单独保留,避免被后续刷屏挤掉 */
let mergeLines = []
let listeners = new Set()
let installed = false
let originalConsole = null
function pad(n) {
return n < 10 ? '0' + n : String(n)
}
function nowTime() {
const d = new Date()
const ms = String(d.getMilliseconds())
const ms3 = ms.length >= 3 ? ms : ('00' + ms).slice(-3)
return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}.${ms3}`
}
function normalizeLevel(level) {
const l = String(level || 'info').toLowerCase()
if (l === LogLevel.DEBUG || l === 'debug') return 'debug'
if (l === LogLevel.WARN || l === 'warn') return 'warn'
if (l === LogLevel.ERROR || l === 'error') return 'error'
return 'info'
}
function formatPayload(payload) {
if (payload == null) return ''
if (typeof payload === 'string') return payload
if (payload instanceof Error) {
return payload.stack || payload.message || String(payload)
}
if (typeof payload === 'object') {
if (payload.msg != null) {
const extra = payload.extra != null ? ` ${serializeIfObject(payload.extra)}` : ''
return String(payload.msg) + extra
}
try {
return serializeIfObject(payload)
} catch (e) {
return String(payload)
}
}
return String(payload)
}
function joinArgs(args) {
return args.map(a => formatPayload(a)).filter(Boolean).join(' ')
}
function pushLine(level, text) {
const msg = formatPayload(text)
if (!msg) return
const line = {
id: `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
time: nowTime(),
level: normalizeLevel(level),
text: msg
}
lines.push(line)
if (msg.includes(PIN_TAG)) {
mergeLines.push(line)
if (mergeLines.length > MAX_PINNED_LINES) {
mergeLines = mergeLines.slice(mergeLines.length - MAX_PINNED_LINES)
}
}
trimLines()
listeners.forEach(fn => {
try {
fn(line)
} catch (e) {
/* ignore */
}
})
}
function patchConsole() {
if (originalConsole) return
originalConsole = {
log: console.log.bind(console),
info: console.info.bind(console),
warn: console.warn.bind(console),
error: console.error.bind(console),
debug: console.debug.bind(console)
}
console.log = (...args) => {
originalConsole.log(...args)
pushLine('info', joinArgs(args))
}
console.info = (...args) => {
originalConsole.info(...args)
pushLine('info', joinArgs(args))
}
console.warn = (...args) => {
originalConsole.warn(...args)
pushLine('warn', joinArgs(args))
}
console.error = (...args) => {
originalConsole.error(...args)
pushLine('error', joinArgs(args))
}
console.debug = (...args) => {
originalConsole.debug(...args)
pushLine('debug', joinArgs(args))
}
}
const FloatLogBus = {
isInstalled() {
return installed
},
getOriginalConsole() {
return originalConsole
},
install() {
if (installed) return
installed = true
patchConsole()
if (uni.$logger && typeof uni.$logger.setLogListener === 'function') {
uni.$logger.setLogListener((level, info) => {
pushLine(level, info)
})
}
pushLine('info', '[FloatLogBus] 日志悬浮窗已就绪')
},
isFloatLogEnabled() {
return FloatLogConfig.isLogBallVisible()
},
push(level, text) {
pushLine(level, text)
},
subscribe(fn) {
listeners.add(fn)
return () => listeners.delete(fn)
},
getLineCount() {
return lines.length
},
getLines() {
return lines.slice()
},
/** 仅路径合并相关日志(冷启动后仍可在悬浮窗看到) */
getMergeLines() {
return mergeLines.slice()
},
/** 取尾部日志,避免面板一次性拷贝/渲染过多 */
getRecentLines(limit = 200) {
if (!limit || lines.length <= limit) return lines.slice()
return lines.slice(lines.length - limit)
},
clearLines() {
lines = []
mergeLines = []
listeners.forEach(fn => {
try {
fn(null)
} catch (e) {
/* ignore */
}
})
}
}
export default FloatLogBus