ConsoleWriter.ts
49 lines
| 1.7 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r49 | import { IObservable, IDestroyable, ICancellation } from "../../interfaces"; | ||
| import { TraceEvent, LogLevel, WarnLevel, DebugLevel } from "../TraceSource"; | ||||
| import { Cancellation } from "../../Cancellation"; | ||||
| import { destroy } from "../../safe"; | ||||
|
|
r59 | function hasConsole() { | ||
| try { | ||||
| // tslint:disable-next-line:no-console | ||||
| return (typeof console !== "undefined" && typeof console.log === "function"); | ||||
| } catch { | ||||
| return false; | ||||
| } | ||||
| } | ||||
|
|
r49 | export class ConsoleWriter implements IDestroyable { | ||
| readonly _subscriptions = new Array<IDestroyable>(); | ||||
| writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | ||||
| const subscription = source.on(this.writeEvent.bind(this)); | ||||
| if (ct.isSupported()) { | ||||
| ct.register(subscription.destroy.bind(subscription)); | ||||
| } | ||||
| this._subscriptions.push(subscription); | ||||
| } | ||||
| writeEvent(next: TraceEvent) { | ||||
|
|
r59 | // IE will create console only when devepoler tools are activated | ||
| if (!hasConsole()) | ||||
| return; | ||||
|
|
r49 | if (next.level >= DebugLevel) { | ||
|
|
r59 | // tslint:disable-next-line:no-console | ||
|
|
r49 | console.debug(next.source.id.toString(), next.arg); | ||
| } else if (next.level >= LogLevel) { | ||||
|
|
r59 | // tslint:disable-next-line:no-console | ||
|
|
r49 | console.log(next.source.id.toString(), next.arg); | ||
| } else if (next.level >= WarnLevel) { | ||||
|
|
r59 | // tslint:disable-next-line:no-console | ||
|
|
r49 | console.warn(next.source.id.toString(), next.arg); | ||
| } else { | ||||
|
|
r59 | // tslint:disable-next-line:no-console | ||
|
|
r49 | console.error(next.source.id.toString(), next.arg); | ||
| } | ||||
| } | ||||
| destroy() { | ||||
| this._subscriptions.forEach(destroy); | ||||
| } | ||||
| } | ||||
