1 | let enabled = false;
|
2 | let _categories = {};
|
3 | const _writers = [];
|
4 | const _eventListeners = [];
|
5 | let _errorHandler;
|
6 | export var Trace;
|
7 | (function (Trace) {
|
8 | |
9 |
|
10 |
|
11 | function enable() {
|
12 | enabled = true;
|
13 | }
|
14 | Trace.enable = enable;
|
15 | |
16 |
|
17 |
|
18 | function disable() {
|
19 | enabled = false;
|
20 | }
|
21 | Trace.disable = disable;
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 | function isEnabled() {
|
28 | return enabled;
|
29 | }
|
30 | Trace.isEnabled = isEnabled;
|
31 | |
32 |
|
33 |
|
34 |
|
35 | function addWriter(writer) {
|
36 | _writers.push(writer);
|
37 | }
|
38 | Trace.addWriter = addWriter;
|
39 | |
40 |
|
41 |
|
42 |
|
43 | function removeWriter(writer) {
|
44 | const index = _writers.indexOf(writer);
|
45 | if (index >= 0) {
|
46 | _writers.splice(index, 1);
|
47 | }
|
48 | }
|
49 | Trace.removeWriter = removeWriter;
|
50 | |
51 |
|
52 |
|
53 | function clearWriters() {
|
54 | if (_writers.length > 0) {
|
55 | _writers.splice(0, _writers.length);
|
56 | }
|
57 | }
|
58 | Trace.clearWriters = clearWriters;
|
59 | |
60 |
|
61 |
|
62 |
|
63 | function setCategories(categories) {
|
64 | _categories = {};
|
65 | addCategories(categories);
|
66 | }
|
67 | Trace.setCategories = setCategories;
|
68 | |
69 |
|
70 |
|
71 |
|
72 | function addCategories(categories) {
|
73 | const split = categories.split(',');
|
74 | for (let i = 0; i < split.length; i++) {
|
75 | _categories[split[i].trim()] = true;
|
76 | }
|
77 | }
|
78 | Trace.addCategories = addCategories;
|
79 | |
80 |
|
81 |
|
82 |
|
83 | function isCategorySet(category) {
|
84 | return category in _categories;
|
85 | }
|
86 | Trace.isCategorySet = isCategorySet;
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function write(message, category, type) {
|
94 |
|
95 | let i;
|
96 | if (type === messageType.error) {
|
97 | for (i = 0; i < _writers.length; i++) {
|
98 | _writers[i].write(message, category, type);
|
99 | }
|
100 | return;
|
101 | }
|
102 | if (!enabled) {
|
103 | return;
|
104 | }
|
105 | if (!(category in _categories)) {
|
106 | return;
|
107 | }
|
108 | for (i = 0; i < _writers.length; i++) {
|
109 | _writers[i].write(message, category, type);
|
110 | }
|
111 | }
|
112 | Trace.write = write;
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function notifyEvent(object, name, data) {
|
120 | if (!enabled) {
|
121 | return;
|
122 | }
|
123 | let i, listener, filters;
|
124 | for (i = 0; i < _eventListeners.length; i++) {
|
125 | listener = _eventListeners[i];
|
126 | if (listener.filter) {
|
127 | filters = listener.filter.split(',');
|
128 | filters.forEach((value) => {
|
129 | if (value.trim() === name) {
|
130 | listener.on(object, name, data);
|
131 | }
|
132 | });
|
133 | }
|
134 | else {
|
135 | listener.on(object, name, data);
|
136 | }
|
137 | }
|
138 | }
|
139 | Trace.notifyEvent = notifyEvent;
|
140 | function addEventListener(listener) {
|
141 | _eventListeners.push(listener);
|
142 | }
|
143 | Trace.addEventListener = addEventListener;
|
144 | function removeEventListener(listener) {
|
145 | const index = _eventListeners.indexOf(listener);
|
146 | if (index >= 0) {
|
147 | _eventListeners.splice(index, 1);
|
148 | }
|
149 | }
|
150 | Trace.removeEventListener = removeEventListener;
|
151 | let messageType;
|
152 | (function (messageType) {
|
153 | messageType.log = 0;
|
154 | messageType.info = 1;
|
155 | messageType.warn = 2;
|
156 | messageType.error = 3;
|
157 | })(messageType = Trace.messageType || (Trace.messageType = {}));
|
158 | |
159 |
|
160 |
|
161 | let categories;
|
162 | (function (categories) {
|
163 | categories.Accessibility = 'Accessibility';
|
164 | categories.VisualTreeEvents = 'VisualTreeEvents';
|
165 | categories.Layout = 'Layout';
|
166 | categories.Style = 'Style';
|
167 | categories.ViewHierarchy = 'ViewHierarchy';
|
168 | categories.NativeLifecycle = 'NativeLifecycle';
|
169 | categories.Debug = 'Debug';
|
170 | categories.Navigation = 'Navigation';
|
171 | categories.Test = 'Test';
|
172 | categories.Binding = 'Binding';
|
173 | categories.BindingError = 'BindingError';
|
174 | categories.Error = 'Error';
|
175 | categories.Animation = 'Animation';
|
176 | categories.Transition = 'Transition';
|
177 | categories.Livesync = 'Livesync';
|
178 | categories.ModuleNameResolver = 'ModuleNameResolver';
|
179 | categories.MediaQuery = 'MediaQuery';
|
180 | categories.separator = ',';
|
181 | categories.All = [categories.VisualTreeEvents, categories.Layout, categories.Style, categories.ViewHierarchy, categories.NativeLifecycle, categories.Debug, categories.Navigation, categories.Test, categories.Binding, categories.Error, categories.Animation, categories.Transition, categories.Livesync, categories.ModuleNameResolver, categories.MediaQuery].join(categories.separator);
|
182 | function concat(...args) {
|
183 | let result;
|
184 | for (let i = 0; i < args.length; i++) {
|
185 | if (!result) {
|
186 | result = args[i];
|
187 | continue;
|
188 | }
|
189 | result = result.concat(categories.separator, args[i]);
|
190 | }
|
191 | return result;
|
192 | }
|
193 | categories.concat = concat;
|
194 | })(categories = Trace.categories || (Trace.categories = {}));
|
195 | class ConsoleWriter {
|
196 | write(message, category, type) {
|
197 | if (!console) {
|
198 | return;
|
199 | }
|
200 | let msgType;
|
201 | if (type === undefined) {
|
202 | msgType = messageType.log;
|
203 | }
|
204 | else {
|
205 | msgType = type;
|
206 | }
|
207 | switch (msgType) {
|
208 | case messageType.log:
|
209 | console.log(category + ': ' + message);
|
210 | break;
|
211 | case messageType.info:
|
212 | console.info(category + ': ' + message);
|
213 | break;
|
214 | case messageType.warn:
|
215 | console.warn(category + ': ' + message);
|
216 | break;
|
217 | case messageType.error:
|
218 | console.error(category + ': ' + message);
|
219 | break;
|
220 | }
|
221 | }
|
222 | }
|
223 |
|
224 | addWriter(new ConsoleWriter());
|
225 | class DefaultErrorHandler {
|
226 | handlerError(error) {
|
227 | throw error;
|
228 | }
|
229 | }
|
230 | Trace.DefaultErrorHandler = DefaultErrorHandler;
|
231 | setErrorHandler(new DefaultErrorHandler());
|
232 | function getErrorHandler() {
|
233 | return _errorHandler;
|
234 | }
|
235 | Trace.getErrorHandler = getErrorHandler;
|
236 | function setErrorHandler(handler) {
|
237 | _errorHandler = handler;
|
238 | }
|
239 | Trace.setErrorHandler = setErrorHandler;
|
240 | |
241 |
|
242 |
|
243 |
|
244 | function error(error) {
|
245 | if (!_errorHandler) {
|
246 | return;
|
247 | }
|
248 | if (typeof error === 'string') {
|
249 | error = new Error(error);
|
250 | }
|
251 | _errorHandler.handlerError(error);
|
252 | }
|
253 | Trace.error = error;
|
254 | })(Trace || (Trace = {}));
|
255 |
|
\ | No newline at end of file |