-
Notifications
You must be signed in to change notification settings - Fork 19
/
Debug.ts
66 lines (49 loc) · 1.58 KB
/
Debug.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
import { IObject } from '@leafer/interface'
export class Debug {
static enable: boolean
static filterList: string[] = []
static excludeList: string[] = []
// other
static __: IObject = {}
@debugAttr()
static showRepaint: boolean
public name: string
constructor(name: string) {
this.name = name
}
static get(name: string): Debug {
return new Debug(name)
}
static set filter(name: string | string[]) {
if (!name) name = []
else if (typeof name === 'string') name = [name]
this.filterList = name
}
static set exclude(name: string | string[]) {
if (!name) name = []
else if (typeof name === 'string') name = [name]
this.excludeList = name
}
log(...messages: unknown[]): void {
if (D.enable) {
if (D.filterList.length && D.filterList.every(name => name !== this.name)) return
if (D.excludeList.length && D.excludeList.some(name => name === this.name)) return
console.log('%c' + this.name, 'color:#21ae62', ...messages)
}
}
warn(...messages: unknown[]): void {
console.warn(this.name, ...messages)
}
error(...messages: unknown[]): void {
console.error(this.name, ...messages)
}
}
const D = Debug
function debugAttr(defaultValue?: unknown) {
return (target: IObject, key: string) => {
Object.defineProperty(target, key, {
get() { return target.enable ? target.__[key] : defaultValue },
set(value: unknown) { this.__[key] = value }
})
}
}