| @@ -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,7 +1,6 | |||
|
|
1 | 1 | import * as format from "../text/format"; |
|
|
2 | import { argumentNotNull } from "../safe"; | |
|
|
3 | 2 | import { Observable } from "../Observable"; |
|
|
4 |
import { |
|
|
|
3 | import { Registry } from "./Registry"; | |
|
|
5 | 4 | |
|
|
6 | 5 | export const DebugLevel = 400; |
|
|
7 | 6 | |
| @@ -13,91 +12,33 export const ErrorLevel = 100; | |||
|
|
13 | 12 | |
|
|
14 | 13 | export const SilentLevel = 0; |
|
|
15 | 14 | |
|
|
16 |
export |
|
|
|
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( |
|
|
|
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( |
|
|
|
41 | this._notifyNext({ source: this, level, arg}); | |
|
|
101 | 42 | } |
|
|
102 | 43 | |
|
|
103 | 44 | isDebugEnabled() { |
| @@ -136,7 +77,7 export class TraceSource { | |||
|
|
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 | */ |
| @@ -148,7 +89,7 export class TraceSource { | |||
|
|
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) { |
| @@ -157,7 +98,7 export class TraceSource { | |||
|
|
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 | */ |
| @@ -169,7 +110,7 export class TraceSource { | |||
|
|
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) { |
| @@ -178,7 +119,7 export class TraceSource { | |||
|
|
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) { |
General Comments 0
You need to be logged in to leave comments.
Login now
