forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.ts
More file actions
52 lines (45 loc) · 1.57 KB
/
Copy pathlog.ts
File metadata and controls
52 lines (45 loc) · 1.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
import {DebugMessageTypeCStoBG} from '../../utils/message';
import type {DebugMessageCStoBG} from '../../definitions';
declare const __DEBUG__: boolean;
declare const __TEST__: boolean;
declare const __WATCH__: boolean;
declare const __LOG__: 'info' | 'warn';
function sendLogToBG(level: 'info' | 'warn' | 'assert', ...args: any[]) {
if (__WATCH__ && __LOG__ && (__LOG__ === 'info' || level === 'warn')) {
// No need to generate contextId since we do not expect a response
chrome.runtime.sendMessage<DebugMessageCStoBG>({type: DebugMessageTypeCStoBG.LOG, data: {level, log: args}});
}
}
export function logInfo(...args: any[]): void {
if (__DEBUG__) {
console.info(...args);
sendLogToBG('info', ...args);
}
}
export function logWarn(...args: any[]): void {
if (__DEBUG__) {
console.warn(...args);
sendLogToBG('warn', ...args);
}
}
export function logInfoCollapsed(title: string, ...args: any[]): void {
if (__DEBUG__) {
console.groupCollapsed(title);
console.log(...args);
console.groupEnd();
}
}
function logAssert(...args: any[]): void {
if ((__TEST__ || __DEBUG__)) {
console.assert(...args);
sendLogToBG('assert', ...args);
}
}
export function ASSERT(description: string, condition: (() => boolean) | any): void {
if ((__TEST__ || __DEBUG__) && (typeof condition === 'function' && !condition()) || !Boolean(condition)) {
logAssert(description);
if (__TEST__) {
throw new Error(`Assertion failed: ${description}`);
}
}
}