-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.ts
60 lines (49 loc) · 1.52 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
/**
* Xpra Typescript Client
* @link https://github.com/andersevenrud/xpra-html5-client
* @author Anders Evenrud <andersevenrud@gmail.com>
* @license Mozilla Public License Version 2.0
*/
import TypedEmitter from 'typed-emitter'
import EventEmitter from 'events'
import { XpraLogLevel } from '../types'
// eslint-disable-next-line
export type XpraLoggerArguments = any[]
export type XpraLoggerType = 'log' | 'info' | 'warn' | 'error' | 'debug'
export type XpraLoggerEventEmitters = {
log: (level: XpraLogLevel, args: XpraLoggerArguments) => void
}
const mapped: Record<XpraLoggerType, XpraLogLevel> = {
log: XpraLogLevel.INFO,
info: XpraLogLevel.INFO,
warn: XpraLogLevel.WARNING,
error: XpraLogLevel.ERROR,
debug: XpraLogLevel.DEBUG,
}
/**
* Console logger wrapper that is used for remote logging
* @noInheritDoc
*/
export class XpraLogger extends (EventEmitter as unknown as new () => TypedEmitter<XpraLoggerEventEmitters>) {
log(...args: XpraLoggerArguments) {
this.perform('log', args)
}
info(...args: XpraLoggerArguments) {
this.perform('info', args)
}
warn(...args: XpraLoggerArguments) {
this.perform('warn', args)
}
error(...args: XpraLoggerArguments) {
this.perform('error', args)
}
debug(...args: XpraLoggerArguments) {
this.perform('debug', args)
}
private perform(level: XpraLoggerType, args: XpraLoggerArguments) {
const cons = console[level]
const actual = mapped[level]
cons.apply(cons, ['XpraLogger', ...args])
this.emit('log', actual, args)
}
}