forked from LearnBoost/console-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console-trace.js
99 lines (84 loc) · 2.57 KB
/
console-trace.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
/**
* Module dependencies.
*/
var callsite = require('callsite')
, tty = require('tty')
, isatty = Boolean(tty.isatty() && process.stdout.getWindowSize)
, defaultColors = { log: '90', error: '91', warn: '93', info: '96', trace: '90' }
console.traceOptions = Object.create(null);
console.traceOptions.cwd = process.cwd() + (process.platform === 'win32' ? '\\' : '/');
console.traceOptions.colors = true;
/**
* Store custom options
*
* @param {Object} options
* @api public
*/
module.exports = function (options) {
if (options) {
options.cwd = options.cwd || console.traceOptions.cwd;
console.traceOptions = options;
}
}
/**
* Overrides the console methods.
*/
;['error', 'log', 'info', 'warn', 'trace'].forEach(function (name) {
var fn = console[name];
console[name] = function () {
if (console._trace || console.traceOptions.always) {
if (Buffer.isBuffer(arguments[0])) {
arguments[0] = arguments[0].inspect()
} else if (typeof arguments[0] === 'object') {
arguments[0] = JSON.stringify(arguments[0], null, ' ');
}
var pad = (arguments[0] && !console.traceOptions.right || !isatty ? ' ' : '');
arguments[0] = console.traceFormat(__stack[1], name) + pad + arguments[0];
}
console._trace = false;
return fn.apply(this, arguments);
}
});
/**
* Overridable formatting function.
*
* @param {CallSite}
* @param {String} calling method
* @api public
*/
console.traceFormat = function (call, method) {
var basename = call.getFileName().replace(console.traceOptions.cwd, '')
, str = '[' + basename + ':' + call.getLineNumber() + ']'
, color = '99'
if (!isatty) {
return str;
}
if (console.traceOptions.colors !== false) {
if (console.traceOptions.colors === undefined || console.traceOptions.colors[method] === undefined) {
color = defaultColors[method];
} else {
color = console.traceOptions.colors[method];
}
}
if (console.traceOptions.right) {
var rowWidth = process.stdout.getWindowSize()[0];
return '\033[s' + // save current position
'\033[' + rowWidth + 'D' + // move to the start of the line
'\033[' + (rowWidth - str.length) + 'C' + // align right
'\033[' + color + 'm' + str + '\033[39m' +
'\033[u'; // restore current position
} else {
return '\033[' + color + 'm' + str + '\033[39m';
}
}
/**
* Adds trace getter to the `console` object.
*
* @api public
*/
function getter () {
this._trace = true;
return this;
}
console.__defineGetter__('t', getter);
console.__defineGetter__('traced', getter);