forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.ts
179 lines (151 loc) · 4.63 KB
/
trace.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import definition = require("trace");
import * as types from "utils/types";
export var enabled = false;
var _categories = {};
var _writers: Array<definition.TraceWriter> = [];
var _eventListeners: Array<definition.EventListener> = [];
export function enable() {
enabled = true;
}
export function disable() {
enabled = false;
}
export function isCategorySet(category: string): boolean {
return category in _categories;
}
export function addWriter(writer: definition.TraceWriter) {
_writers.push(writer);
}
export function removeWriter(writer: definition.TraceWriter) {
var index = _writers.indexOf(writer);
if (index >= 0) {
_writers.splice(index, 1);
}
}
export function clearWriters() {
if (_writers.length > 0) {
_writers.splice(0, _writers.length);
}
}
export function setCategories(categories: string) {
_categories = {};
addCategories(categories);
}
export function addCategories(categories: string) {
var split = categories.split(",");
for (let i = 0; i < split.length; i++) {
_categories[split[i].trim()] = true;
}
}
export function write(message: any, category: string, type?: number) {
// print error no matter what
var i;
if (type === messageType.error) {
for (i = 0; i < _writers.length; i++) {
_writers[i].write(message, category, type);
}
}
if (!enabled) {
return;
}
if (!(category in _categories)) {
return;
}
for (i = 0; i < _writers.length; i++) {
_writers[i].write(message, category, type);
}
}
export function notifyEvent(object: Object, name: string, data?: any) {
if (!enabled) {
return;
}
var i,
listener: definition.EventListener,
filters: Array<string>;
for (i = 0; i < _eventListeners.length; i++) {
listener = _eventListeners[i];
if (listener.filter) {
filters = listener.filter.split(",");
filters.forEach((value: string) => {
if (value.trim() === name) {
listener.on(object, name, data);
}
});
} else {
listener.on(object, name, data);
}
}
}
export function addEventListener(listener: definition.EventListener) {
_eventListeners.push(listener);
}
export function removeEventListener(listener: definition.EventListener) {
var index = _eventListeners.indexOf(listener);
if (index >= 0) {
_eventListeners.splice(index, 1);
}
}
export module messageType {
export var log = 0;
export var info = 1;
export var warn = 2;
export var error = 3;
}
export module categories {
export var VisualTreeEvents = "VisualTreeEvents";
export var Layout = "Layout";
export var Style = "Style";
export var ViewHierarchy = "ViewHierarchy";
export var NativeLifecycle = "NativeLifecycle";
export var Debug = "Debug";
export var Navigation = "Navigation";
export var Test = "Test";
export var Binding = "Binding";
export var BindingError = "BindingError";
export var Error = "Error";
export var Animation = "Animation";
export var Transition = "Transition";
export var All = VisualTreeEvents + "," + Layout + "," + Style + "," + ViewHierarchy + "," + NativeLifecycle + "," + Debug + "," + Navigation + "," + Test + "," + Binding + "," + Error + "," + Animation + "," + Transition;
export var separator = ",";
export function concat(): string {
var i;
var result: string;
for (i = 0; i < arguments.length; i++) {
if (!result) {
result = arguments[i];
continue;
}
result = result.concat(separator, arguments[i]);
}
return result;
}
}
class ConsoleWriter implements definition.TraceWriter {
public write(message: any, category: string, type?: number) {
if (!console) {
return;
}
var msgType;
if (types.isUndefined(type)) {
msgType = messageType.log;
} else {
msgType = type;
}
switch (msgType) {
case messageType.log:
console.log(category + ": " + message);
break;
case messageType.info:
console.info(category + ": " + message);
break;
case messageType.warn:
console.warn(category + ": " + message);
break;
case messageType.error:
console.error(category + ": " + message);
break;
}
}
}
// register a ConsoleWriter by default
addWriter(new ConsoleWriter());