Last active
March 31, 2021 09:40
-
-
Save kosiew/171e3ab1b874c436a39c9483016ce3fd to your computer and use it in GitHub Desktop.
Aspects for debugging
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
// version 1.00 | |
let indent = 0, | |
LEVEL_0_COLOR = '#060dd3', | |
LEVEL_X_COLOR = '#D33106', | |
DLOG_DEBUG = true; | |
const START = '<', | |
END = '>'; | |
function dlog(message, level=0){ | |
if (DLOG_DEBUG) { | |
const styles = [ | |
'border: 1px solid #3E0E02' | |
, 'color: white' | |
, 'display: block' | |
, 'text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3)' | |
, 'box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4) inset, 0 5px 3px -5px rgba(0, 0, 0, 0.5), 0 -13px 5px -10px rgba(255, 255, 255, 0.4) inset' | |
, 'line-height: 20px' | |
, 'text-align: center' | |
, 'font-weight: bold' | |
]; | |
if (level==0) { | |
styles.push(`background: linear-gradient(${LEVEL_0_COLOR}, #040647)`); | |
} else { | |
styles.push(`background: linear-gradient(${LEVEL_X_COLOR}, #571402)`); | |
} | |
const _styles = styles.join(';'); | |
console.log(`%c ${message}`, _styles); | |
} | |
} | |
function dl(message, _indent=' ') { | |
if (_indent == START) { | |
indent += 1; | |
} | |
const prefix = ' '.repeat(indent*2); | |
dlog(`${prefix} ${message}`); | |
if (_indent == END) { | |
indent -= 1; | |
} | |
} | |
function wrap_object(target) { | |
// eg wrap_object(bl) | |
dl('wrap_object'); | |
/** Helping function used to get all methods of an object */ | |
const getMethods = (obj) => { | |
let props = Object.getOwnPropertyNames(obj) | |
if (props.length == 0) { | |
props = Object.getOwnPropertyNames(Object.getPrototypeOf(obj)); | |
} | |
return props.filter(item => typeof obj[item] === 'function'); | |
} | |
/** Replace the original method with a custom function that will call our aspect when the advice dictates */ | |
function replaceMethod(target, methodName) { | |
dl(`replaceMethod for object ${target}`); | |
const originalCode = target[methodName] | |
target[methodName] = (...args) => { | |
dl(`${target.constructor.name}.${methodName} ( ${args} )`, START); | |
const returnedValue = originalCode.apply(target, args) | |
dl(`${target.constructor.name}.${methodName} return ${returnedValue} (type: ${typeof(returnedValue)})`, END); | |
return returnedValue; | |
} | |
} | |
const methods = getMethods(target) | |
dl(`methods = ${methods}`); | |
methods.forEach( m => { | |
replaceMethod(target, m) | |
}) | |
} | |
function wrap(f) { | |
dl(`wrap ${f}`) | |
// eg test = wrap(test) | |
const old_f = f; | |
f = (...args) => { | |
dl(`${old_f.name} ( ${args} )`, START); | |
const result = old_f(args); | |
dl(`${old_f.name} return ${result} (type: ${typeof result})`, END); | |
return result; | |
} | |
return f | |
} | |
// version 1.00 | |
// . initial release |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment