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