TraceSource.ts
128 lines
| 3.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r49 | import { Observable } from "../Observable"; | ||
|
|
r53 | import { Registry } from "./Registry"; | ||
|
|
r54 | import { format } from "../text/FormatString"; | ||
|
|
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; | ||||
| readonly arg: any; | ||||
| } | ||||
| 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 | } | ||
| protected emit(level: number, arg: any) { | ||||
|
|
r53 | this._notifyNext({ source: this, level, arg}); | ||
|
|
r49 | } | ||
| isDebugEnabled() { | ||||
| return this.level >= DebugLevel; | ||||
| } | ||||
| debug(msg: string, ...args: any[]) { | ||||
| if (this.isEnabled(DebugLevel)) | ||||
|
|
r54 | this.emit(DebugLevel, format(msg, args)); | ||
|
|
r49 | } | ||
| isLogEnabled() { | ||||
| return this.level >= LogLevel; | ||||
| } | ||||
| log(msg: string, ...args: any[]) { | ||||
| if (this.isEnabled(LogLevel)) | ||||
|
|
r54 | this.emit(LogLevel, format(msg, args)); | ||
|
|
r49 | } | ||
| isWarnEnabled() { | ||||
| return this.level >= WarnLevel; | ||||
| } | ||||
| warn(msg: string, ...args: any[]) { | ||||
| if (this.isEnabled(WarnLevel)) | ||||
|
|
r54 | this.emit(WarnLevel, format(msg, args)); | ||
|
|
r49 | } | ||
| /** | ||||
| * returns true if errors will be recorded. | ||||
| */ | ||||
| isErrorEnabled() { | ||||
| return this.level >= ErrorLevel; | ||||
| } | ||||
| /** | ||||
| * Traces a error. | ||||
|
|
r53 | * | ||
|
|
r49 | * @param msg the message. | ||
| * @param args parameters which will be substituted in the message. | ||||
| */ | ||||
| error(msg: string, ...args: any[]) { | ||||
| if (this.isEnabled(ErrorLevel)) | ||||
| this.emit(ErrorLevel, format.apply(null, arguments)); | ||||
| } | ||||
| /** | ||||
| * 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 | ||
| * @param arg the data of the event, can be a simple string or any object. | ||||
| */ | ||||
| traceEvent(level: number, arg: any) { | ||||
| if (this.isEnabled(level)) | ||||
| this.emit(level, arg); | ||||
| } | ||||
| /** | ||||
| * 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); | ||||
| } | ||||
| } | ||||
