trace.js
944 Bytes
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
(function(factory){
if(typeof define != "undefined"){
define([], factory);
}else if(typeof module != "undefined"){
module.exports = factory();
}else{
dclAdvicesTrace = factory();
}
})(function(){
"use strict";
var lvl = 0;
function rep(ch, n){
if(n < 1){ return ""; }
if(n == 1){ return ch; }
var h = rep(ch, Math.floor(n / 2));
return h + h + ((n & 1) ? ch : "");
}
function pad(value, width, ch){
var v = value.toString();
return v + rep(ch || " ", width - v.length);
}
return function(name, level){
return {
before: function(){
++lvl;
console.log((level ? pad(lvl, 2 * lvl) : "") + this + " => " +
name + "(" + Array.prototype.join.call(arguments, ", ") + ")");
},
after: function(args, result){
console.log((level ? pad(lvl, 2 * lvl) : "") + this + " => " +
name + (result && result instanceof Error ? " throws" : " returns") +
" " + result);
--lvl;
}
};
};
});