TraceSource.ts
141 lines
| 3.4 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r49 | import { Observable } from "../Observable"; | ||
|
|
r53 | import { Registry } from "./Registry"; | ||
|
|
r82 | import { TraceEventData } from "./TraceEventData"; | ||
|
|
r49 | |||
| export const DebugLevel = 400; | ||||
| export const LogLevel = 300; | ||||
| export const WarnLevel = 200; | ||||
| export const ErrorLevel = 100; | ||||
| export const SilentLevel = 0; | ||||
|
|
r53 | export interface TraceEvent { | ||
|
|
r49 | readonly source: TraceSource; | ||
| readonly level: number; | ||||
|
|
r82 | readonly message: any; | ||
|
|
r49 | |||
|
|
r82 | readonly args?: any[]; | ||
|
|
r65 | } | ||
|
|
r49 | export class TraceSource { | ||
|
|
r53 | readonly id: any; | ||
|
|
r49 | |||
|
|
r53 | level: number; | ||
|
|
r49 | |||
|
|
r53 | readonly events: Observable<TraceEvent>; | ||
|
|
r49 | |||
|
|
r53 | _notifyNext: (arg: TraceEvent) => void; | ||
|
|
r49 | |||
| constructor(id: any) { | ||||
| this.id = id || new Object(); | ||||
|
|
r53 | this.events = new Observable(next => { | ||
|
|
r49 | this._notifyNext = next; | ||
|
|
r53 | }); | ||
|
|
r49 | } | ||
|
|
r82 | protected emit(level: number, message: any, args?: any[]) { | ||
| this._notifyNext(new TraceEventData(this, level, message, args)); | ||||
|
|
r49 | } | ||
| isDebugEnabled() { | ||||
| return this.level >= DebugLevel; | ||||
| } | ||||
|
|
r82 | debug(data: any): void; | ||
| debug(msg: string, ...args: any[]): void; | ||||
|
|
r49 | debug(msg: string, ...args: any[]) { | ||
| if (this.isEnabled(DebugLevel)) | ||||
|
|
r82 | this.emit(DebugLevel, msg, args); | ||
|
|
r49 | } | ||
| isLogEnabled() { | ||||
| return this.level >= LogLevel; | ||||
| } | ||||
|
|
r82 | log(data: any): void; | ||
| log(msg: string, ...args: any[]): void; | ||||
|
|
r49 | log(msg: string, ...args: any[]) { | ||
| if (this.isEnabled(LogLevel)) | ||||
|
|
r82 | this.emit(LogLevel, msg, args); | ||
|
|
r49 | } | ||
| isWarnEnabled() { | ||||
| return this.level >= WarnLevel; | ||||
| } | ||||
|
|
r82 | warn(data: any): void; | ||
| warn(msg: string, ...args: any[]): void; | ||||
|
|
r49 | warn(msg: string, ...args: any[]) { | ||
| if (this.isEnabled(WarnLevel)) | ||||
|
|
r82 | this.emit(WarnLevel, msg, args); | ||
|
|
r49 | } | ||
| /** | ||||
| * returns true if errors will be recorded. | ||||
| */ | ||||
| isErrorEnabled() { | ||||
| return this.level >= ErrorLevel; | ||||
| } | ||||
|
|
r82 | /** Traces a error | ||
| * @param data The object which will be passed to the underlying listeners | ||||
| */ | ||||
| error(data: any): void; | ||||
|
|
r49 | /** | ||
| * Traces a error. | ||||
|
|
r53 | * | ||
|
|
r49 | * @param msg the message. | ||
| * @param args parameters which will be substituted in the message. | ||||
| */ | ||||
|
|
r82 | error(msg: string, ...args: any[]): void; | ||
|
|
r49 | error(msg: string, ...args: any[]) { | ||
| if (this.isEnabled(ErrorLevel)) | ||||
|
|
r82 | this.emit(ErrorLevel, msg, args); | ||
|
|
r49 | } | ||
| /** | ||||
| * Checks whether the specified level is enabled for this | ||||
| * trace source. | ||||
|
|
r53 | * | ||
|
|
r49 | * @param level the trace level which should be checked. | ||
| */ | ||||
| isEnabled(level: number) { | ||||
| return (this.level >= level); | ||||
| } | ||||
| /** | ||||
| * Traces a raw event, passing data as it is to the underlying listeners | ||||
|
|
r53 | * | ||
|
|
r49 | * @param level the level of the event | ||
|
|
r82 | * @param msg the data of the event, can be a simple string or any object. | ||
|
|
r49 | */ | ||
|
|
r82 | traceEvent(level: number, msg: any, ...args: any[]) { | ||
|
|
r49 | if (this.isEnabled(level)) | ||
|
|
r82 | this.emit(level, msg, args); | ||
|
|
r49 | } | ||
| /** | ||||
| * Register the specified handler to be called for every new and already | ||||
| * created trace source. | ||||
|
|
r53 | * | ||
|
|
r49 | * @param handler the handler which will be called for each trace source | ||
| */ | ||||
| static on(handler: (source: TraceSource) => void) { | ||||
| return Registry.instance.on(handler); | ||||
| } | ||||
| /** | ||||
| * Creates or returns already created trace source for the specified id. | ||||
|
|
r53 | * | ||
|
|
r49 | * @param id the id for the trace source | ||
| */ | ||||
| static get(id: any) { | ||||
| return Registry.instance.get(id); | ||||
| } | ||||
| } | ||||
