forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.ts
120 lines (108 loc) · 3.01 KB
/
logger.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
export interface ILogFunction {
(message?: any, ...optionalParams: any[]): void;
}
export interface ILogger {
trace: ILogFunction;
debug: ILogFunction;
log: ILogFunction;
warn: ILogFunction;
info: ILogFunction;
error: ILogFunction;
}
export class Logger implements ILogger {
trace: ILogFunction;
debug: ILogFunction;
log: ILogFunction;
warn: ILogFunction;
info: ILogFunction;
error: ILogFunction;
constructor(label: string, logger: ILogger) {
const lb = `[${label}]:`;
this.trace = noop;
this.debug = logger.debug.bind(null, lb);
this.log = logger.log.bind(null, lb);
this.warn = logger.warn.bind(null, lb);
this.info = logger.info.bind(null, lb);
this.error = logger.error.bind(null, lb);
}
}
const noop: ILogFunction = function () {};
const fakeLogger: ILogger = {
trace: noop,
debug: noop,
log: noop,
warn: noop,
info: noop,
error: noop,
};
function createLogger() {
return Object.assign({}, fakeLogger);
}
// let lastCallTime;
// function formatMsgWithTimeInfo(type, msg) {
// const now = Date.now();
// const diff = lastCallTime ? '+' + (now - lastCallTime) : '0';
// lastCallTime = now;
// msg = (new Date(now)).toISOString() + ' | [' + type + '] > ' + msg + ' ( ' + diff + ' ms )';
// return msg;
// }
function consolePrintFn(type: string, id: string | undefined): ILogFunction {
const func: ILogFunction = self.console[type];
return func
? func.bind(self.console, `${id ? '[' + id + '] ' : ''}[${type}] >`)
: noop;
}
function getLoggerFn(
key: string,
debugConfig: boolean | Partial<ILogger>,
id?: string,
): ILogFunction {
return debugConfig[key]
? debugConfig[key].bind(debugConfig)
: consolePrintFn(key, id);
}
const exportedLogger: ILogger = createLogger();
export function enableLogs(
debugConfig: boolean | ILogger,
context: string,
id?: string | undefined,
): ILogger {
// check that console is available
const newLogger = createLogger();
if (
(typeof console === 'object' && debugConfig === true) ||
typeof debugConfig === 'object'
) {
const keys: (keyof ILogger)[] = [
// Remove out from list here to hard-disable a log-level
// 'trace',
'debug',
'log',
'info',
'warn',
'error',
];
keys.forEach((key) => {
newLogger[key] = getLoggerFn(key, debugConfig, id);
});
// Some browsers don't allow to use bind on console object anyway
// fallback to default if needed
try {
newLogger.log(
`Debug logs enabled for "${context}" in hls.js version ${__VERSION__}`,
);
} catch (e) {
/* log fn threw an exception. All logger methods are no-ops. */
return createLogger();
}
// global exported logger uses the same functions as new logger without `id`
keys.forEach((key) => {
exportedLogger[key] = getLoggerFn(key, debugConfig);
});
} else {
// Reset global exported logger
Object.assign(exportedLogger, newLogger);
}
return newLogger;
}
export const logger: ILogger = exportedLogger;