TraceSource.ts
188 lines
| 4.4 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r9 | import * as format from '../text/format' | ||
|
|
r10 | import { argumentNotNull } from '../safe'; | ||
|
|
r23 | import { Observable } from '../Observable' | ||
|
|
r14 | import { IDestroyable } from '../interfaces'; | ||
|
|
r22 | |||
| export const DebugLevel = 400; | ||||
| export const LogLevel = 300; | ||||
| export const WarnLevel = 200; | ||||
| export const ErrorLevel = 100; | ||||
| export const SilentLevel = 0; | ||||
| export class TraceEvent { | ||||
| readonly source: TraceSource; | ||||
| readonly level: Number; | ||||
| readonly arg: any; | ||||
| constructor(source: TraceSource, level: Number, arg: any) { | ||||
| this.source = source; | ||||
| this.level = level; | ||||
| this.arg = arg; | ||||
| } | ||||
| } | ||||
|
|
r9 | |||
|
|
r10 | class Registry { | ||
| static readonly instance = new Registry(); | ||||
| private _registry: object = new Object(); | ||||
| private _listeners: object = new Object(); | ||||
| private _nextCookie: number = 1; | ||||
|
|
r9 | |||
|
|
r10 | get(id: any): TraceSource { | ||
| argumentNotNull(id, "id"); | ||||
| if (this._registry[id]) | ||||
| return this._registry[id]; | ||||
|
|
r9 | |||
|
|
r10 | var source = new TraceSource(id); | ||
| this._registry[id] = source; | ||||
| this._onNewSource(source); | ||||
| return source; | ||||
|
|
r9 | } | ||
|
|
r10 | add(id: any, source: TraceSource) { | ||
| argumentNotNull(id, "id"); | ||||
| argumentNotNull(source, "source"); | ||||
| this._registry[id] = source; | ||||
| this._onNewSource(source); | ||||
| } | ||||
| _onNewSource(source: TraceSource) { | ||||
| for (let i in this._listeners) | ||||
| this._listeners[i].call(null, source); | ||||
| } | ||||
|
|
r14 | on(handler: (source: TraceSource) => void): IDestroyable { | ||
|
|
r10 | argumentNotNull(handler, "handler"); | ||
| var me = this; | ||||
| var cookie = this._nextCookie++; | ||||
| this._listeners[cookie] = handler; | ||||
| for (let i in this._registry) | ||||
| handler(this._registry[i]); | ||||
| return { | ||||
| destroy() { | ||||
| delete me._listeners[cookie]; | ||||
| } | ||||
| }; | ||||
|
|
r9 | } | ||
| } | ||||
|
|
r22 | export class TraceSource { | ||
|
|
r9 | readonly id: any | ||
| level: number | ||||
|
|
r22 | readonly events: Observable<TraceEvent> | ||
| _notifyNext: (arg: TraceEvent) => void | ||||
|
|
r9 | constructor(id: any) { | ||
|
|
r22 | |||
|
|
r9 | this.id = id || new Object(); | ||
|
|
r22 | this.events = new Observable((next) => { | ||
| this._notifyNext = next; | ||||
| }) | ||||
|
|
r9 | } | ||
|
|
r10 | protected emit(level: number, arg: any) { | ||
|
|
r15 | this._notifyNext(new TraceEvent(this, level, arg)); | ||
|
|
r9 | } | ||
| isDebugEnabled() { | ||||
|
|
r22 | return this.level >= DebugLevel; | ||
|
|
r9 | } | ||
|
|
r10 | debug(msg: string, ...args: any[]) { | ||
|
|
r22 | if (this.isEnabled(DebugLevel)) | ||
|
|
r35 | this.emit(DebugLevel, format.apply(null, arguments)); | ||
|
|
r9 | } | ||
| isLogEnabled() { | ||||
|
|
r22 | return this.level >= LogLevel; | ||
|
|
r9 | } | ||
|
|
r10 | log(msg: string, ...args: any[]) { | ||
|
|
r22 | if (this.isEnabled(LogLevel)) | ||
|
|
r35 | this.emit(LogLevel, format.apply(null, arguments)); | ||
|
|
r9 | } | ||
| isWarnEnabled() { | ||||
|
|
r22 | return this.level >= WarnLevel; | ||
|
|
r9 | } | ||
|
|
r10 | warn(msg: string, ...args: any[]) { | ||
|
|
r22 | if (this.isEnabled(WarnLevel)) | ||
|
|
r35 | this.emit(WarnLevel, format.apply(null, arguments)); | ||
|
|
r9 | } | ||
|
|
r12 | /** | ||
| * returns true if errors will be recorded. | ||||
| */ | ||||
|
|
r9 | isErrorEnabled() { | ||
|
|
r22 | return this.level >= ErrorLevel; | ||
|
|
r9 | } | ||
|
|
r12 | /** | ||
| * Traces a error. | ||||
| * | ||||
| * @param msg the message. | ||||
| * @param args parameters which will be substituted in the message. | ||||
| */ | ||||
|
|
r10 | error(msg: string, ...args: any[]) { | ||
|
|
r22 | if (this.isEnabled(ErrorLevel)) | ||
|
|
r35 | this.emit(ErrorLevel, format.apply(null, arguments)); | ||
|
|
r10 | } | ||
|
|
r12 | /** | ||
| * Checks whether the specified level is enabled for this | ||||
| * trace source. | ||||
| * | ||||
| * @param level the trace level which should be checked. | ||||
| */ | ||||
|
|
r10 | isEnabled(level: number) { | ||
| return (this.level >= level); | ||||
| } | ||||
|
|
r12 | /** | ||
| * Traces a raw event, passing data as it is to the underlying listeners | ||||
| * | ||||
| * @param level the level of the event | ||||
| * @param arg the data of the event, can be a simple string or any object. | ||||
| */ | ||||
|
|
r10 | traceEvent(level: number, arg: any) { | ||
| if (this.isEnabled(level)) | ||||
| this.emit(level, arg); | ||||
| } | ||||
|
|
r12 | /** | ||
| * Register the specified handler to be called for every new and already | ||||
| * created trace source. | ||||
| * | ||||
| * @param handler the handler which will be called for each trace source | ||||
| */ | ||||
|
|
r14 | static on(handler: (source: TraceSource) => void) { | ||
|
|
r11 | return Registry.instance.on(handler); | ||
|
|
r10 | } | ||
|
|
r12 | /** | ||
| * Creates or returns already created trace source for the specified id. | ||||
| * | ||||
| * @param id the id for the trace source | ||||
| */ | ||||
|
|
r10 | static get(id: any) { | ||
| return Registry.instance.get(id); | ||||
|
|
r9 | } | ||
| } | ||||
