ConsoleWriter.ts
36 lines
| 1.3 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r15 | import { IObservable, IDestroyable, ICancellation } from "../../interfaces"; | ||
|
|
r45 | import { TraceEvent, LogLevel, WarnLevel, DebugLevel } from "../TraceSource"; | ||
|
|
r14 | import { Cancellation } from "../../Cancellation"; | ||
|
|
r45 | import { destroy } from "../../safe"; | ||
|
|
r14 | |||
|
|
r22 | export class ConsoleWriter implements IDestroyable { | ||
|
|
r15 | readonly _subscriptions = new Array<IDestroyable>(); | ||
| writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | ||||
|
|
r45 | const subscription = source.on(this.writeEvent.bind(this)); | ||
|
|
r15 | if (ct.isSupported()) { | ||
| ct.register(subscription.destroy.bind(subscription)); | ||||
| } | ||||
| this._subscriptions.push(subscription); | ||||
| } | ||||
| writeEvent(next: TraceEvent) { | ||||
|
|
r45 | if (next.level >= DebugLevel) { | ||
| // tslint:disable-next-line | ||||
| console.debug(next.source.id.toString(), next.arg); | ||||
| } else if (next.level >= LogLevel) { | ||||
| // tslint:disable-next-line | ||||
|
|
r15 | console.log(next.source.id.toString(), next.arg); | ||
|
|
r45 | } else if (next.level >= WarnLevel) { | ||
| // tslint:disable-next-line | ||||
|
|
r15 | console.warn(next.source.id.toString(), next.arg); | ||
| } else { | ||||
|
|
r45 | // tslint:disable-next-line | ||
|
|
r15 | console.error(next.source.id.toString(), next.arg); | ||
|
|
r14 | } | ||
| } | ||||
|
|
r15 | destroy() { | ||
|
|
r45 | this._subscriptions.forEach(destroy); | ||
|
|
r14 | } | ||
|
|
r45 | } | ||
