Mercurial > pub > ImplabJs
annotate src/implab/log/trace.js @ 28:d796bbbe558c
added gradle
author | cin |
---|---|
date | Sat, 23 Jun 2018 21:50:11 +0300 |
parents | f750c89976d3 |
children |
rev | line source |
---|---|
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
1 define(["../text/format"], function (format) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
2 'use strict'; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
3 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
4 var listeners = []; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
5 var channels = {}; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
6 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
7 var Trace = function (name) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
8 this.name = name; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
9 this._subscribers = []; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
10 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
11 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
12 Trace.prototype.debug = function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
13 if (Trace.level >= 4) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
14 this.notify("debug", format.apply(null, arguments)); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
15 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
16 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
17 Trace.prototype.log = function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
18 if (Trace.level >= 3) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
19 this.notify("log", format.apply(null, arguments)); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
20 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
21 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
22 Trace.prototype.warn = function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
23 if (Trace.level >= 2) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
24 this.notify("warn", format.apply(null, arguments)); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
25 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
26 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
27 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
28 Trace.prototype.error = function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
29 if (Trace.level >= 1) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
30 this.notify("error", format.apply(null, arguments)); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
31 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
32 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
33 Trace.prototype.notify = function (name, msg) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
34 var me = this; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
35 me._subscribers.forEach(function (cb) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
36 cb(me, name, msg); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
37 }); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
38 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
39 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
40 Trace.prototype.subscribe = function (cb) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
41 this._subscribers.push(cb); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
42 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
43 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
44 Trace.prototype.toString = function () { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
45 return this.name; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
46 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
47 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
48 Trace.createChannel = function (type, name, cb) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
49 var chId = name; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
50 if (channels[chId]) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
51 return channels[chId]; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
52 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
53 var channel = new type(chId); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
54 channels[chId] = channel; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
55 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
56 Trace._onNewChannel(chId, channel); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
57 cb(channel); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
58 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
59 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
60 Trace._onNewChannel = function (chId, ch) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
61 listeners.forEach(function (listener) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
62 listener(chId, ch); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
63 }); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
64 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
65 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
66 Trace.on = function (filter, cb) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
67 if (arguments.length == 1) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
68 cb = filter; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
69 filter = undefined; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
70 } |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
71 var d, test; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
72 if (filter instanceof RegExp) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
73 test = function (chId) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
74 return filter.test(chId); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
75 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
76 } else if (filter instanceof Function) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
77 test = filter; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
78 } else if (filter) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
79 test = function (chId) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
80 return chId == filter; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
81 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
82 } |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
83 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
84 if (test) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
85 d = function(chId, ch) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
86 if(test(chId)) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
87 ch.subscribe(cb); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
88 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
89 } else { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
90 d = function(chId, ch) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
91 ch.subscribe(cb); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
92 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
93 } |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
94 listeners.push(d); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
95 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
96 for(var chId in channels) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
97 d(chId,channels[chId]); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
98 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
99 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
100 Trace.load = function (id, require, cb) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
101 if (id) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
102 Trace.createChannel(Trace, id, cb); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
103 else if (require.module && require.module.mid) |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
104 Trace.createChannel(Trace, require.module.mid, cb); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
105 else |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
106 require(['module'], function (module) { |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
107 Trace.createChannel(Trace, module && module.id, cb); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
108 }); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
109 }; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
110 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
111 Trace.dynamic = true; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
112 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
113 Trace.level = 4; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
114 |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
115 return Trace; |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
diff
changeset
|
116 }); |