Last active
May 15, 2018 17:38
-
-
Save Bjvanminnen/ace7914a8db4d6447f684bd3e4acaf5d to your computer and use it in GitHub Desktop.
Log all WebGL calls
This file contains 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
/** | |
* Usage: | |
* // start to log the name/arguments for all function calls on context | |
* const logger = new WebGLLogger(webglContext); | |
* | |
* // stop logging, restoring the originals | |
* logger.disable(); | |
*/ | |
class WebGLLogger { | |
constructor(context) { | |
this.context = context; | |
this.enable(); | |
} | |
enable() { | |
const context = this.context; | |
let numCalls = 0; | |
this.originals = {}; | |
this.constants = { | |
}; | |
for (let name in context) { | |
if (typeof(context[name]) === 'function') { | |
this.originals[name] = context[name].bind(context); | |
/* eslint-disable no-loop-func */ | |
context[name] = (...args) => { | |
numCalls++; | |
const mappedArgs = args.map(val => { | |
if (typeof(val) === 'number' && this.constants[val]) { | |
return `${this.constants[val]}(${val})`; | |
} | |
return val; | |
}); | |
if (args.some(val => typeof(val) === 'string' && val.length > 100)) { | |
console.groupCollapsed(`[${numCalls}]`, name, '...'); | |
console.log(...mappedArgs); | |
console.groupEnd(); | |
} else { | |
console.log(`[${numCalls}]`, name, ...mappedArgs); | |
} | |
const result = this.originals[name](...args); | |
if (result) { | |
console.log(result); | |
} | |
return result; | |
} | |
} else if (typeof(context[name]) === 'number') { | |
this.constants[context[name]] = name; | |
} | |
} | |
} | |
disable() { | |
Object.keys(this.originals).forEach(name => { | |
this.context[name] = this.originals[name]; | |
}); | |
this.originals = {}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment