##// END OF EJS Templates
minor refactoring of TraceSource, code linting
minor refactoring of TraceSource, code linting

File last commit:

r53:5fd4258909e8 di-typescript
r53:5fd4258909e8 di-typescript
Show More
TraceSource.ts
128 lines | 3.0 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / log / TraceSource.ts
cin
changed the project structure
r49 import * as format from "../text/format";
import { Observable } from "../Observable";
cin
minor refactoring of TraceSource, code linting
r53 import { Registry } from "./Registry";
cin
changed the project structure
r49
export const DebugLevel = 400;
export const LogLevel = 300;
export const WarnLevel = 200;
export const ErrorLevel = 100;
export const SilentLevel = 0;
cin
minor refactoring of TraceSource, code linting
r53 export interface TraceEvent {
cin
changed the project structure
r49 readonly source: TraceSource;
readonly level: number;
readonly arg: any;
}
export class TraceSource {
cin
minor refactoring of TraceSource, code linting
r53 readonly id: any;
cin
changed the project structure
r49
cin
minor refactoring of TraceSource, code linting
r53 level: number;
cin
changed the project structure
r49
cin
minor refactoring of TraceSource, code linting
r53 readonly events: Observable<TraceEvent>;
cin
changed the project structure
r49
cin
minor refactoring of TraceSource, code linting
r53 _notifyNext: (arg: TraceEvent) => void;
cin
changed the project structure
r49
constructor(id: any) {
this.id = id || new Object();
cin
minor refactoring of TraceSource, code linting
r53 this.events = new Observable(next => {
cin
changed the project structure
r49 this._notifyNext = next;
cin
minor refactoring of TraceSource, code linting
r53 });
cin
changed the project structure
r49 }
protected emit(level: number, arg: any) {
cin
minor refactoring of TraceSource, code linting
r53 this._notifyNext({ source: this, level, arg});
cin
changed the project structure
r49 }
isDebugEnabled() {
return this.level >= DebugLevel;
}
debug(msg: string, ...args: any[]) {
if (this.isEnabled(DebugLevel))
this.emit(DebugLevel, format.apply(null, arguments));
}
isLogEnabled() {
return this.level >= LogLevel;
}
log(msg: string, ...args: any[]) {
if (this.isEnabled(LogLevel))
this.emit(LogLevel, format.apply(null, arguments));
}
isWarnEnabled() {
return this.level >= WarnLevel;
}
warn(msg: string, ...args: any[]) {
if (this.isEnabled(WarnLevel))
this.emit(WarnLevel, format.apply(null, arguments));
}
/**
* returns true if errors will be recorded.
*/
isErrorEnabled() {
return this.level >= ErrorLevel;
}
/**
* Traces a error.
cin
minor refactoring of TraceSource, code linting
r53 *
cin
changed the project structure
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.
cin
minor refactoring of TraceSource, code linting
r53 *
cin
changed the project structure
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
cin
minor refactoring of TraceSource, code linting
r53 *
cin
changed the project structure
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.
cin
minor refactoring of TraceSource, code linting
r53 *
cin
changed the project structure
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.
cin
minor refactoring of TraceSource, code linting
r53 *
cin
changed the project structure
r49 * @param id the id for the trace source
*/
static get(id: any) {
return Registry.instance.get(id);
}
}