##// END OF EJS Templates
minor refactoring of TraceSource, code linting
cin -
r53:5fd4258909e8 di-typescript
parent child
Show More
@@ -0,0 +1,55
1 import { TraceSource } from "./TraceSource";
2 import { argumentNotNull } from "../safe";
3 import { IDestroyable } from "../interfaces";
4
5 export class Registry {
6 static readonly instance = new Registry();
7
8 private _registry: object = new Object();
9 private _listeners: object = new Object();
10 private _nextCookie: number = 1;
11
12 get(id: any): TraceSource {
13 argumentNotNull(id, "id");
14
15 if (this._registry[id])
16 return this._registry[id];
17
18 const source = new TraceSource(id);
19 this._registry[id] = source;
20 this._onNewSource(source);
21
22 return source;
23 }
24
25 add(id: any, source: TraceSource) {
26 argumentNotNull(id, "id");
27 argumentNotNull(source, "source");
28
29 this._registry[id] = source;
30 this._onNewSource(source);
31 }
32
33 _onNewSource(source: TraceSource) {
34 for (const i in this._listeners)
35 this._listeners[i].call(null, source);
36 }
37
38 on(handler: (source: TraceSource) => void): IDestroyable {
39 argumentNotNull(handler, "handler");
40 const me = this;
41
42 const cookie = this._nextCookie++;
43
44 this._listeners[cookie] = handler;
45
46 for (const i in this._registry)
47 handler(this._registry[i]);
48
49 return {
50 destroy() {
51 delete me._listeners[cookie];
52 }
53 };
54 }
55 }
@@ -1,187 +1,128
1 1 import * as format from "../text/format";
2 import { argumentNotNull } from "../safe";
3 2 import { Observable } from "../Observable";
4 import { IDestroyable } from "../interfaces";
3 import { Registry } from "./Registry";
5 4
6 5 export const DebugLevel = 400;
7 6
8 7 export const LogLevel = 300;
9 8
10 9 export const WarnLevel = 200;
11 10
12 11 export const ErrorLevel = 100;
13 12
14 13 export const SilentLevel = 0;
15 14
16 export class TraceEvent {
15 export interface TraceEvent {
17 16 readonly source: TraceSource;
18 17
19 18 readonly level: number;
20 19
21 20 readonly arg: any;
22
23 constructor(source: TraceSource, level: number, arg: any) {
24 this.source = source;
25 this.level = level;
26 this.arg = arg;
27 }
28 }
29
30 class Registry {
31 static readonly instance = new Registry();
32
33 private _registry: object = new Object();
34 private _listeners: object = new Object();
35 private _nextCookie: number = 1;
36
37 get(id: any): TraceSource {
38 argumentNotNull(id, "id");
39
40 if (this._registry[id])
41 return this._registry[id];
42
43 var source = new TraceSource(id);
44 this._registry[id] = source;
45 this._onNewSource(source);
46
47 return source;
48 }
49
50 add(id: any, source: TraceSource) {
51 argumentNotNull(id, "id");
52 argumentNotNull(source, "source");
53
54 this._registry[id] = source;
55 this._onNewSource(source);
56 }
57
58 _onNewSource(source: TraceSource) {
59 for (let i in this._listeners)
60 this._listeners[i].call(null, source);
61 }
62
63 on(handler: (source: TraceSource) => void): IDestroyable {
64 argumentNotNull(handler, "handler");
65 var me = this;
66
67 var cookie = this._nextCookie++;
68
69 this._listeners[cookie] = handler;
70
71 for (let i in this._registry)
72 handler(this._registry[i]);
73
74 return {
75 destroy() {
76 delete me._listeners[cookie];
77 }
78 };
79 }
80 21 }
81 22
82 23 export class TraceSource {
83 readonly id: any
24 readonly id: any;
84 25
85 level: number
26 level: number;
86 27
87 readonly events: Observable<TraceEvent>
28 readonly events: Observable<TraceEvent>;
88 29
89 _notifyNext: (arg: TraceEvent) => void
30 _notifyNext: (arg: TraceEvent) => void;
90 31
91 32 constructor(id: any) {
92 33
93 34 this.id = id || new Object();
94 this.events = new Observable((next) => {
35 this.events = new Observable(next => {
95 36 this._notifyNext = next;
96 })
37 });
97 38 }
98 39
99 40 protected emit(level: number, arg: any) {
100 this._notifyNext(new TraceEvent(this, level, arg));
41 this._notifyNext({ source: this, level, arg});
101 42 }
102 43
103 44 isDebugEnabled() {
104 45 return this.level >= DebugLevel;
105 46 }
106 47
107 48 debug(msg: string, ...args: any[]) {
108 49 if (this.isEnabled(DebugLevel))
109 50 this.emit(DebugLevel, format.apply(null, arguments));
110 51 }
111 52
112 53 isLogEnabled() {
113 54 return this.level >= LogLevel;
114 55 }
115 56
116 57 log(msg: string, ...args: any[]) {
117 58 if (this.isEnabled(LogLevel))
118 59 this.emit(LogLevel, format.apply(null, arguments));
119 60 }
120 61
121 62 isWarnEnabled() {
122 63 return this.level >= WarnLevel;
123 64 }
124 65
125 66 warn(msg: string, ...args: any[]) {
126 67 if (this.isEnabled(WarnLevel))
127 68 this.emit(WarnLevel, format.apply(null, arguments));
128 69 }
129 70
130 71 /**
131 72 * returns true if errors will be recorded.
132 73 */
133 74 isErrorEnabled() {
134 75 return this.level >= ErrorLevel;
135 76 }
136 77
137 78 /**
138 79 * Traces a error.
139 *
80 *
140 81 * @param msg the message.
141 82 * @param args parameters which will be substituted in the message.
142 83 */
143 84 error(msg: string, ...args: any[]) {
144 85 if (this.isEnabled(ErrorLevel))
145 86 this.emit(ErrorLevel, format.apply(null, arguments));
146 87 }
147 88
148 89 /**
149 90 * Checks whether the specified level is enabled for this
150 91 * trace source.
151 *
92 *
152 93 * @param level the trace level which should be checked.
153 94 */
154 95 isEnabled(level: number) {
155 96 return (this.level >= level);
156 97 }
157 98
158 99 /**
159 100 * Traces a raw event, passing data as it is to the underlying listeners
160 *
101 *
161 102 * @param level the level of the event
162 103 * @param arg the data of the event, can be a simple string or any object.
163 104 */
164 105 traceEvent(level: number, arg: any) {
165 106 if (this.isEnabled(level))
166 107 this.emit(level, arg);
167 108 }
168 109
169 110 /**
170 111 * Register the specified handler to be called for every new and already
171 112 * created trace source.
172 *
113 *
173 114 * @param handler the handler which will be called for each trace source
174 115 */
175 116 static on(handler: (source: TraceSource) => void) {
176 117 return Registry.instance.on(handler);
177 118 }
178 119
179 120 /**
180 121 * Creates or returns already created trace source for the specified id.
181 *
122 *
182 123 * @param id the id for the trace source
183 124 */
184 125 static get(id: any) {
185 126 return Registry.instance.get(id);
186 127 }
187 128 }
General Comments 0
You need to be logged in to leave comments. Login now