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