forked from LearnBoost/console-trace
-
Notifications
You must be signed in to change notification settings - Fork 5
/
debug-trace.js
167 lines (148 loc) · 5.02 KB
/
debug-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
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
/**
* Module dependencies.
*/
var callsite = require('callsite')
, tty = require('tty')
, isatty = Boolean(tty.isatty(2) && process.stdout.getWindowSize)
, defaultColors = { log: '90', error: '91', warn: '93', info: '96', trace: '90' }
/**
* Store custom options
*
* @param {Object} options
* @api public
*/
module.exports = function debugTrace(options) {
options = options || {};
console.traceOptions = console.traceOptions || {};
console.traceOptions.cwd = options.cwd || process.cwd() + '/';
console.traceOptions.colors = typeof options.colors !== 'undefined' ? options.colors : true;
console.traceOptions.always = typeof options.always !== 'undefined' ? options.always : true;
console.traceOptions.right = typeof options.right !== 'undefined' ? options.right : false;
if (typeof options.overwriteDebugLog === 'undefined' || options.overwriteDebugLog) overwriteDebugLog(options.overwriteDebugLog)
if (typeof options.patchOutput === 'undefined' || options.patchOutput) patchOutput()
}
function overwriteDebugLog(debugLog) {
debugLog = typeof debugLog === 'function' ? debugLog : console.log;
try {
var Debug = require('debug');
Debug.log = function log() {
return debugLog.apply(debugLog, arguments);
};
} catch (error) {
console.log('debug module is not installed');
}
}
function patchOutput() {
var stdout = process.stdout.write;
var stderr = process.stderr.write;
process.stdout.write = function write() {
var stack = callsite();
var trace = stack[1];
if (trace.getFunctionName() === 'log') {
var args = [].slice.call(arguments);
if (typeof args[args.length - 1] === 'string') args[args.length - 1] = args[args.length - 1].replace(/\n$/g, '');
return console.log.apply(console.log, args);
}
return stdout.apply(process.stdout, arguments)
}
process.stderr.write = function write() {
var stack = callsite();
var trace = stack[1];
if (trace.getFunctionName() === 'log') {
var args = [].slice.call(arguments);
if (typeof args[args.length - 1] === 'string') args[args.length - 1] = args[args.length - 1].replace(/\n$/g, '');
return console.log.apply(console.log, args);
}
return stderr.apply(process.stderr, arguments)
}
}
/**
* Overrides the console methods.
*/
;['error', 'log', 'info', 'warn', 'trace'].forEach(function (name) {
var fn = console[name];
console[name] = function () {
var args = arguments;
if (console._trace || console.traceOptions.always) {
if (args.length === 1) args = ['', args[0]];
if (Buffer.isBuffer(args[0])) {
args[0] = args[0].inspect()
} else if (typeof args[0] === 'object') {
args[0] = JSON.stringify(args[0], null, ' ');
}
var pad = (args[0] && !console.traceOptions.right || !isatty ? ' ' : '');
// when using the debug module: dig one level deeper in the stack
var stack = callsite();
var trace = stack[1];
if (stack.length > 2 && trace.getFunctionName() === 'log') {
trace = stack[3];
trace.debug = true;
}
if (stack.length > 3 && trace.getFunctionName() === 'write') {
trace = stack[4];
trace.debug = true;
}
trace.debug = trace.debug || false;
args[0] = console.traceFormat(trace, name) + pad + args[0];
}
console._trace = false;
return fn.apply(this, args);
}
});
/**
* Overridable formatting function.
*
* @param {CallSite}
* @param {String} calling method
* @api public
*/
console.traceFormat = function (call, method) {
call.filename = call.getFileName().replace(console.traceOptions.cwd, '');
call.method = method;
call.functionName = call.getFunctionName() || 'anonymous'
call.getDate = function getDate() {
return new Date().toISOString().replace('T', ' ').replace('Z', '');
}
var str = console.format(call);
var 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';
}
}
/**
* Overridable string formatting function.
*
* @param {CallSite} CallSite Object pimped with additional properties.
* @api public
*/
console.format = function (c) {
return c.getDate() + ": [" + c.filename + ":" + c.getLineNumber() + "] " + c.functionName;
};
/**
* Adds trace getter to the `console` object.
*
* @api public
*/
function getter() {
this._trace = true;
return this;
}
Object.defineProperty(console, 't', { get: getter });
Object.defineProperty(console, 'traced', { get: getter });