##// END OF EJS Templates
Слияние с default
Слияние с default

File last commit:

r35:9b7927c5bafc v1.1.1 default
r37:4325b0f90ccc merge di-typescript
Show More
TraceSource.ts
188 lines | 4.4 KiB | video/mp2t | TypeScriptLexer
/ src / ts / log / TraceSource.ts
cin
Async operation cancellation proposal...
r9 import * as format from '../text/format'
cin
rewritten TraceSource
r10 import { argumentNotNull } from '../safe';
cin
documenting...
r23 import { Observable } from '../Observable'
cin
working on Observable
r14 import { IDestroyable } from '../interfaces';
cin
Code cleanup,...
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;
}
}
cin
Async operation cancellation proposal...
r9
cin
rewritten TraceSource
r10 class Registry {
static readonly instance = new Registry();
private _registry: object = new Object();
private _listeners: object = new Object();
private _nextCookie: number = 1;
cin
Async operation cancellation proposal...
r9
cin
rewritten TraceSource
r10 get(id: any): TraceSource {
argumentNotNull(id, "id");
if (this._registry[id])
return this._registry[id];
cin
Async operation cancellation proposal...
r9
cin
rewritten TraceSource
r10 var source = new TraceSource(id);
this._registry[id] = source;
this._onNewSource(source);
return source;
cin
Async operation cancellation proposal...
r9 }
cin
rewritten TraceSource
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);
}
cin
working on Observable
r14 on(handler: (source: TraceSource) => void): IDestroyable {
cin
rewritten TraceSource
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];
}
};
cin
Async operation cancellation proposal...
r9 }
}
cin
Code cleanup,...
r22 export class TraceSource {
cin
Async operation cancellation proposal...
r9 readonly id: any
level: number
cin
Code cleanup,...
r22 readonly events: Observable<TraceEvent>
_notifyNext: (arg: TraceEvent) => void
cin
Async operation cancellation proposal...
r9 constructor(id: any) {
cin
Code cleanup,...
r22
cin
Async operation cancellation proposal...
r9 this.id = id || new Object();
cin
Code cleanup,...
r22 this.events = new Observable((next) => {
this._notifyNext = next;
})
cin
Async operation cancellation proposal...
r9 }
cin
rewritten TraceSource
r10 protected emit(level: number, arg: any) {
cin
fixed format-compile bug while formatting strings with new line symbols....
r15 this._notifyNext(new TraceEvent(this, level, arg));
cin
Async operation cancellation proposal...
r9 }
isDebugEnabled() {
cin
Code cleanup,...
r22 return this.level >= DebugLevel;
cin
Async operation cancellation proposal...
r9 }
cin
rewritten TraceSource
r10 debug(msg: string, ...args: any[]) {
cin
Code cleanup,...
r22 if (this.isEnabled(DebugLevel))
cin
fixed trace messages formatting
r35 this.emit(DebugLevel, format.apply(null, arguments));
cin
Async operation cancellation proposal...
r9 }
isLogEnabled() {
cin
Code cleanup,...
r22 return this.level >= LogLevel;
cin
Async operation cancellation proposal...
r9 }
cin
rewritten TraceSource
r10 log(msg: string, ...args: any[]) {
cin
Code cleanup,...
r22 if (this.isEnabled(LogLevel))
cin
fixed trace messages formatting
r35 this.emit(LogLevel, format.apply(null, arguments));
cin
Async operation cancellation proposal...
r9 }
isWarnEnabled() {
cin
Code cleanup,...
r22 return this.level >= WarnLevel;
cin
Async operation cancellation proposal...
r9 }
cin
rewritten TraceSource
r10 warn(msg: string, ...args: any[]) {
cin
Code cleanup,...
r22 if (this.isEnabled(WarnLevel))
cin
fixed trace messages formatting
r35 this.emit(WarnLevel, format.apply(null, arguments));
cin
Async operation cancellation proposal...
r9 }
cin
removed obsolete code
r12 /**
* returns true if errors will be recorded.
*/
cin
Async operation cancellation proposal...
r9 isErrorEnabled() {
cin
Code cleanup,...
r22 return this.level >= ErrorLevel;
cin
Async operation cancellation proposal...
r9 }
cin
removed obsolete code
r12 /**
* Traces a error.
*
* @param msg the message.
* @param args parameters which will be substituted in the message.
*/
cin
rewritten TraceSource
r10 error(msg: string, ...args: any[]) {
cin
Code cleanup,...
r22 if (this.isEnabled(ErrorLevel))
cin
fixed trace messages formatting
r35 this.emit(ErrorLevel, format.apply(null, arguments));
cin
rewritten TraceSource
r10 }
cin
removed obsolete code
r12 /**
* Checks whether the specified level is enabled for this
* trace source.
*
* @param level the trace level which should be checked.
*/
cin
rewritten TraceSource
r10 isEnabled(level: number) {
return (this.level >= level);
}
cin
removed obsolete code
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.
*/
cin
rewritten TraceSource
r10 traceEvent(level: number, arg: any) {
if (this.isEnabled(level))
this.emit(level, arg);
}
cin
removed obsolete code
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
*/
cin
working on Observable
r14 static on(handler: (source: TraceSource) => void) {
cin
added TraceSource tests
r11 return Registry.instance.on(handler);
cin
rewritten TraceSource
r10 }
cin
removed obsolete code
r12 /**
* Creates or returns already created trace source for the specified id.
*
* @param id the id for the trace source
*/
cin
rewritten TraceSource
r10 static get(id: any) {
return Registry.instance.get(id);
cin
Async operation cancellation proposal...
r9 }
}