-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (108 loc) · 3.62 KB
/
index.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
const colors = require('colors');
// the call place of console.log
const CALL_POINT = 1
// the call stack of console.log
const CALL_STACK = 2
// defaul is the call place of console.log()
var _logType = CALL_POINT
var original_console_log = console.log;
var winObj = {}
Object.defineProperty(winObj, '__AT_STACK__', {
get: function () {
// //atdo way1
// var old = Error.prepareStackTrace;
// Error.prepareStackTrace = function (error, stack) {
// return stack;
// };
// var err = new Error();
// Error.captureStackTrace(err, arguments.callee.caller.caller);
// Error.prepareStackTrace = old;
var err = new Error();
// Error.captureStackTrace(err, arguments.callee);
Error.captureStackTrace(err, arguments.callee.caller.caller);
// original_console_log(err.stack)
// original_console_log('----------')
//from line 1, ignore line 0 of: Error
var from = 1;
var errLines = err.stack.split('\n');
// original_console_log(from, errLines);
var errLineArr = [];
for (var i = from; i < errLines.length; i++) {
/*
Error
at Object.<anonymous> (D:\hack\web\AdminLTE-2.3.5\watJs\log.js:98:1)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
*/
if (errLines[i].indexOf('Module._compile') !== -1
|| errLines[i].indexOf('Generator.next') !== -1) {
break;
}
// original_console_log('====',errLines[i])
// at Query.connection.query (D:\hack\web\AdminLTE-2.3.5\00\mysqlNode\a.js:36:11)
var f = errLines[i].indexOf('(')
// console.log(errLines[i].slice(f + 1, -1))
errLineArr.push(errLines[i].slice(f + 1, -1).trim());
// 83->80
// var b = errLines[i].lastIndexOf(':');
// var b2 = errLines[i].lastIndexOf(':', b - 1);
// var lineNum = parseInt(errLines[i].substring(b2 + 1, b));
// // original_console_log(i, lineNum);
// if (!isNaN(lineNum) && (lineNum !== 1 || (lineNum === 1 && errLineArr.length === 0))) {
// errLineArr.push(lineNum);
// }
}
if (_logType === CALL_POINT) {
return errLineArr.slice(0, 1);
} else {
return errLineArr.reverse();
}
}
});
Object.defineProperty(winObj, '__AT_LINE02__', {
get: function () {
var joinSep = '\n';// \n or ->
return winObj.__AT_STACK__.join(joinSep);
}
});
function getDateTime() {
const d = new Date();
return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${d.getMilliseconds()}`
}
console.log = function () {
//RangeError: Maximum call stack size exceeded
// console.log(winObj.__AT_LINE02__);
let s = winObj.__AT_LINE02__;
if (s.indexOf('at ') === 0) {
original_console_log(`at ${getDateTime()} (${s.substr(3)})`.green);
} else {
original_console_log(`at ${getDateTime()} (${s})`.green);
}
original_console_log.apply(null, arguments);
};
/**
* output call place or call stack of console.log
* @param type
* <pre>
* CALL_POINT: call place
* CALL_STACK: call stack
*/
function logType(type) {
_logType = type
}
//test ========================================
// function d() {
// console.log(3, 1, 2)
// }
//
// d();
module.exports = {
CALL_POINT,
CALL_STACK,
logType,
}