UNPKG

8.59 kBJavaScriptView Raw
1let enabled = false;
2let _categories = {};
3const _writers = [];
4const _eventListeners = [];
5let _errorHandler;
6export var Trace;
7(function (Trace) {
8 /**
9 * Enables the trace module.
10 */
11 function enable() {
12 enabled = true;
13 }
14 Trace.enable = enable;
15 /**
16 * Disables the trace module.
17 */
18 function disable() {
19 enabled = false;
20 }
21 Trace.disable = disable;
22 /**
23 * A function that returns whether the tracer is enabled and there is a point in writing messages.
24 * Check this to avoid writing complex string templates.
25 * Send error messages even if tracing is disabled.
26 */
27 function isEnabled() {
28 return enabled;
29 }
30 Trace.isEnabled = isEnabled;
31 /**
32 * Adds a TraceWriter instance to the trace module.
33 * @param writer The TraceWriter instance to add.
34 */
35 function addWriter(writer) {
36 _writers.push(writer);
37 }
38 Trace.addWriter = addWriter;
39 /**
40 * Removes a TraceWriter instance from the trace module.
41 * @param writer The TraceWriter instance to remove.
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 * Clears all the writers from the trace module.
52 */
53 function clearWriters() {
54 if (_writers.length > 0) {
55 _writers.splice(0, _writers.length);
56 }
57 }
58 Trace.clearWriters = clearWriters;
59 /**
60 * Sets the categories the module will trace.
61 * @param categories The comma-separated list of categories. If not specified all messages from all categories will be traced.
62 */
63 function setCategories(categories) {
64 _categories = {};
65 addCategories(categories);
66 }
67 Trace.setCategories = setCategories;
68 /**
69 * Adds categories to existing categories the module will trace.
70 * @param categories The comma-separated list of categories. If not specified all messages from all categories will be traced.
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 * Check if category is already set in trace module.
81 * @param category The category to check.
82 */
83 function isCategorySet(category) {
84 return category in _categories;
85 }
86 Trace.isCategorySet = isCategorySet;
87 /**
88 * Writes a message using the available writers.
89 * @param message The message to be written.
90 * @param category The category of the message.
91 * @param type Optional, the type of the message - info, warning, error.
92 */
93 function write(message, category, type) {
94 // print error no matter what
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 * Notifies all the attached listeners for an event that has occurred in the sender object.
115 * @param object The Object instance that raised the event.
116 * @param name The name of the raised event.
117 * @param data An optional parameter that passes the data associated with the event.
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 * all predefined categories.
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 // register a ConsoleWriter by default
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 * Passes an error to the registered ErrorHandler
242 * @param error The error to be handled.
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//# sourceMappingURL=index.js.map
\No newline at end of file