Created
September 17, 2014 19:45
-
-
Save bunnybones1/a0e446f32e50fb629e29 to your computer and use it in GitHub Desktop.
helper to debug when you don't know who or what is changing a value. Can log sets and gets, or even provide stacktraces to the offenders. Meant as a last resort debugging tool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var defaultMax = 1000; | |
function GetterSetterValue(initVal, logGet, logSet, traceSet, traceGet, max) { | |
max = max === undefined ? defaultMax : max; | |
this.originalMax = max; | |
this._value = initVal; | |
this.__logGet = logGet !== false; | |
this.__logSet = logSet !== false; | |
this.__traceGet = traceGet !== false; | |
this.__traceSet = traceSet !== false; | |
this.__countLogGet = max; | |
this.__countLogSet = max; | |
this.__countTraceGet = max; | |
this.__countTraceSet = max; | |
} | |
GetterSetterValue.prototype = { | |
_value: 0, | |
__count: defaultMax, | |
get value() { | |
if(this.__logGet && this.__countLogGet >= 0) { | |
console.log("getting", this._value); | |
if(this.__countLogGet == 0) { | |
console.log("max number of get logs reached (", this.originalMax, ")") | |
} | |
this.__countLogGet--; | |
} | |
if(this.__traceGet && this.__countTraceGet >= 0) { | |
try { | |
throw Error("Who read this value?"); | |
} catch(err) { | |
console.log(err.message); | |
console.log(err.stack); | |
} | |
if(this.__countTraceGet == 0) { | |
console.log("max number of get traces reached (", this.originalMax, ")") | |
} | |
this.__countTraceGet--; | |
} | |
return this._value; | |
}, | |
set value(val) { | |
if(this.__logSet && this.__countLogSet >= 0) { | |
console.log("setting", val); | |
if(this.__countLogSet == 0) { | |
console.log("max number of set logs reached (", this.originalMax, ")") | |
} | |
this.__countLogSet--; | |
} | |
if(this.__traceSet && this.__countTraceSet >= 0) { | |
try { | |
throw Error("Who wrote this value?"); | |
} catch(err) { | |
console.log(err.message); | |
console.log(err.stack); | |
} | |
if(this.__countTraceSet == 0) { | |
console.log("max number of set traces reached (", this.originalMax, ")") | |
} | |
this.__countTraceSet--; | |
} | |
this._value = val; | |
} | |
} | |
LibHelpers = { | |
GetterSetterValue: GetterSetterValue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is mostly meant to debug pesky in-place vars defined and manipulated in a function. It was born of a need to figure out what was wrong with geometry I was trying to parse with Three.js. It's a bit hacky because you do need to replace all instances of offendingVar with offendingVar.value, but in large libraries where its almost impossible to keep track of everything that manipulates a particular value, this can be a lifesaver.
For example:
To use this helper, include it in the html:
and modify the suspicious code:
And you should get a flood of logs pertaining to anything that reads or writes to offendingVar.value