##// END OF EJS Templates
Fixed circular dependency TraceSource<->Registry
cin -
r148:cd81c631015f v1.4.0-rc6 default
parent child
Show More
@@ -1,55 +1,55
1 1 import { TraceSource } from "./TraceSource";
2 2 import { argumentNotNull } from "../safe";
3 3 import { IDestroyable, MapOf } from "../interfaces";
4 4
5 5 export class Registry {
6 6 static readonly instance = new Registry();
7 7
8 8 private _registry: MapOf<TraceSource> = {};
9 9 private _listeners: MapOf<(source: TraceSource) => void> = {};
10 10 private _nextCookie: number = 1;
11 11
12 12 get(id: string): TraceSource {
13 13 argumentNotNull(id, "id");
14 14
15 15 if (this._registry[id])
16 16 return this._registry[id];
17 else
18 throw new Error("The specified trace source doesn't exists");
19 }
17 20
18 const source = new TraceSource(id);
19 this._registry[id] = source;
20 this._onNewSource(source);
21
22 return source;
21 has(id: string) {
22 return !!this._registry[id];
23 23 }
24 24
25 25 add(id: any, source: TraceSource) {
26 26 argumentNotNull(id, "id");
27 27 argumentNotNull(source, "source");
28 28
29 29 this._registry[id] = source;
30 30 this._onNewSource(source);
31 31 }
32 32
33 33 _onNewSource(source: TraceSource) {
34 34 for (const i in this._listeners)
35 35 this._listeners[i].call(null, source);
36 36 }
37 37
38 38 on(handler: (source: TraceSource) => void): IDestroyable {
39 39 argumentNotNull(handler, "handler");
40 40 const me = this;
41 41
42 42 const cookie = this._nextCookie++;
43 43
44 44 this._listeners[cookie] = handler;
45 45
46 46 for (const i in this._registry)
47 47 handler(this._registry[i]);
48 48
49 49 return {
50 50 destroy() {
51 51 delete me._listeners[cookie];
52 52 }
53 53 };
54 54 }
55 55 }
@@ -1,141 +1,147
1 1 import { Observable } from "../Observable";
2 2 import { Registry } from "./Registry";
3 3 import { TraceEventData } from "./TraceEventData";
4 4 import { EventProvider } from "../EventProvider";
5 5
6 6 export const DebugLevel = 400;
7 7
8 8 export const LogLevel = 300;
9 9
10 10 export const WarnLevel = 200;
11 11
12 12 export const ErrorLevel = 100;
13 13
14 14 export const SilentLevel = 0;
15 15
16 16 export interface TraceEvent {
17 17 readonly source: TraceSource;
18 18
19 19 readonly level: number;
20 20
21 21 readonly message: any;
22 22
23 23 readonly args?: any[];
24 24 }
25 25
26 26 export class TraceSource {
27 27 readonly id: any;
28 28
29 29 level = 0;
30 30
31 31 readonly events: Observable<TraceEvent>;
32 32
33 33 private readonly _provider: EventProvider<TraceEvent>;
34 34
35 35 constructor(id?: any) {
36 36
37 37 this.id = id || new Object();
38 38 this._provider = new EventProvider();
39 39 this.events = this._provider.getObservable();
40 40 }
41 41
42 42 protected emit(level: number, message: any, args: any[]) {
43 43 this._provider.post(new TraceEventData(this, level, message, args));
44 44 }
45 45
46 46 isDebugEnabled() {
47 47 return this.level >= DebugLevel;
48 48 }
49 49
50 50 debug(data: any): void;
51 51 debug(msg: string, ...args: any[]): void;
52 52 debug(msg: string, ...args: any[]) {
53 53 if (this.isEnabled(DebugLevel))
54 54 this.emit(DebugLevel, msg, args);
55 55 }
56 56
57 57 isLogEnabled() {
58 58 return this.level >= LogLevel;
59 59 }
60 60
61 61 log(data: any): void;
62 62 log(msg: string, ...args: any[]): void;
63 63 log(msg: string, ...args: any[]) {
64 64 if (this.isEnabled(LogLevel))
65 65 this.emit(LogLevel, msg, args);
66 66 }
67 67
68 68 isWarnEnabled() {
69 69 return this.level >= WarnLevel;
70 70 }
71 71
72 72 warn(data: any): void;
73 73 warn(msg: string, ...args: any[]): void;
74 74 warn(msg: string, ...args: any[]) {
75 75 if (this.isEnabled(WarnLevel))
76 76 this.emit(WarnLevel, msg, args);
77 77 }
78 78
79 79 /**
80 80 * returns true if errors will be recorded.
81 81 */
82 82 isErrorEnabled() {
83 83 return this.level >= ErrorLevel;
84 84 }
85 85
86 86 /** Traces a error
87 87 * @param data The object which will be passed to the underlying listeners
88 88 */
89 89 error(data: any): void;
90 90 /**
91 91 * Traces a error.
92 92 *
93 93 * @param msg the message.
94 94 * @param args parameters which will be substituted in the message.
95 95 */
96 96 error(msg: string, ...args: any[]): void;
97 97 error(msg: string, ...args: any[]) {
98 98 if (this.isEnabled(ErrorLevel))
99 99 this.emit(ErrorLevel, msg, args);
100 100 }
101 101
102 102 /**
103 103 * Checks whether the specified level is enabled for this
104 104 * trace source.
105 105 *
106 106 * @param level the trace level which should be checked.
107 107 */
108 108 isEnabled(level: number) {
109 109 return (this.level >= level);
110 110 }
111 111
112 112 /**
113 113 * Traces a raw event, passing data as it is to the underlying listeners
114 114 *
115 115 * @param level the level of the event
116 116 * @param msg the data of the event, can be a simple string or any object.
117 117 */
118 118 traceEvent(level: number, msg: any, ...args: any[]) {
119 119 if (this.isEnabled(level))
120 120 this.emit(level, msg, args);
121 121 }
122 122
123 123 /**
124 124 * Register the specified handler to be called for every new and already
125 125 * created trace source.
126 126 *
127 127 * @param handler the handler which will be called for each trace source
128 128 */
129 129 static on(handler: (source: TraceSource) => void) {
130 130 return Registry.instance.on(handler);
131 131 }
132 132
133 133 /**
134 134 * Creates or returns already created trace source for the specified id.
135 135 *
136 136 * @param id the id for the trace source
137 137 */
138 138 static get(id: any) {
139 return Registry.instance.get(id);
139 if (!Registry.instance.has(id)) {
140 const trace = new TraceSource(id);
141 Registry.instance.add(id, trace);
142 return trace;
143 } else {
144 return Registry.instance.get(id);
145 }
140 146 }
141 147 }
General Comments 0
You need to be logged in to leave comments. Login now