-
Notifications
You must be signed in to change notification settings - Fork 4
/
console.js
154 lines (138 loc) · 4.67 KB
/
console.js
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
define('mo/console', [
'./lang/es5',
'./lang/type',
'./browsers',
'./template/string',
'./domready'
], function(_0, _, browsers, tpl){
var console = this.console = this.console || {},
origin_console = {
log: console.log,
info: console.info,
warn: console.warn,
error: console.error
},
RE_CODE = /^function[^(]*\([^)]*\)[^{]*\{([.\s\S]*)\}$/;
console._ccBuffer = [];
console.config = function(opt){
if (opt.output) {
this._ccOutput = opt.output;
this._ccOutput.innerHTML = this._ccBuffer.join('');
}
if (opt.record !== undefined) {
this._recording = opt.record;
if (!opt.record) {
console.cc();
}
}
return this;
};
console.enable = function(){
for (var i in origin_console) {
console[i] = console_api(i);
}
console.run = run;
return this;
};
console.disable = function(){
for (var i in origin_console) {
console[i] = origin_console[i];
}
console.run = console.log;
return this;
};
console.cc = function(newlog){
if (newlog === undefined) {
return this._ccBuffer.join('');
} else {
this._ccBuffer.push(newlog);
var result = this._ccBuffer.join('');
if (!this._recording) {
//if (!this._ccOutput) {
//this._ccOutput = default_output();
//}
if (this._ccOutput) {
this._ccOutput.innerHTML = result;
}
}
return result;
}
};
function run(fn, opt){
opt = opt || {};
var code = fn.toString().trim()
.match(RE_CODE)[1]
.trim()
.replace(/return\s+/, '')
.replace(/;$/, '');
console.info('run `' + code + '`');
try {
console.info(fn());
} catch(ex) {
console.error(opt.showStack ? ex : ex.message);
}
}
function console_api(method){
return function(){
if (_.isFunction(origin_console[method])) {
origin_console[method].apply(console, arguments);
}
console.cc('<p>'
+ '<span class="type type-' + method + '"></span>'
+ '<span class="log">'
+ Array.prototype.slice.call(arguments)
.map(escape_log, method).join('</span><span class="log">')
+ '</span></p>');
};
}
//function default_output(){
//var output = document.createElement('DIV');
//output.setAttribute('id', 'console');
//document.body.insertBefore(output, document.body.firstChild);
//return output;
//}
function escape_log(text){
var method = this;
if (text instanceof Error) {
text = text.stack ? text.stack.split(/at\s/) : [text.message];
return text.map(function(msg){
return escape_log('at ' + msg, method);
}).join('<br>');
} else if (method.toString() !== 'log'
&& text
&& typeof text === 'object'
&& (!browsers.aosp || text.nodeType !== 1)) {
text = [
'<span class="obj-start">' + tpl.escapeHTML('{') + '</span>',
Object.keys(text).map(function(key){
var v;
try {
v = this[key];
} catch(ex) {
v = ex.message;
}
if (typeof v === 'string') {
v = '"' + v + '"';
} else {
v = String(v);
}
return '<span class="obj-item">'
+ '<span class="obj-k">'
+ escape_log(key, method)
+ ': </span><span class="obj-v">'
+ (typeof v === 'string' ? tpl.escapeHTML(v) : v)
+ '</span>,</span>';
}, text).join(''),
'<span class="obj-end">' + tpl.escapeHTML('}') + '</span>'
].join('');
return '<span class="obj-wrap"><span class="obj-overview">'
+ text
+ '</span><span class="obj-end">...}</span><span class="obj-detail">'
+ text
+ '</span></span></span>';
}
text = String(text);
return typeof text === 'string' ? tpl.escapeHTML(text) : text;
}
return console;
});