Skip to content

Instantly share code, notes, and snippets.

@afaur
Last active June 11, 2017 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afaur/968e1a6b5c3581fef89ef8819fdfb264 to your computer and use it in GitHub Desktop.
Save afaur/968e1a6b5c3581fef89ef8819fdfb264 to your computer and use it in GitHub Desktop.
Make a replacement log function that doesn't require the parentheses.
// Get stdout off process as outPipe
const { stdout: outPipe } = process
// Import log and trace off of console
const { log, trace } = console
// Make a logRaw function
const logRaw = (...args) => outPipe.write(...args)
// Easy log fn for node
function echo() {
const maybeArray = Array.prototype.slice.call(arguments)[0]
const logString = (typeof(maybeArray) === "string") ? maybeArray : maybeArray[0]
const templateVarToValue = (match, string, offset) => eval(Array.from(match).splice(1).toString())
log(logString.replace(/(\$[^\W]+)/g, templateVarToValue))
}
// Give back an array of the stacktrace items
const traceIt = () => {
var orig = Error.prepareStackTrace
Error.prepareStackTrace = function(_, stack) {
return stack
}
var err = new Error
Error.captureStackTrace(err, arguments.callee)
var stack = err.stack
Error.prepareStackTrace = orig
return stack
}
// Takes a string of variable names comma seperated
// In: `foo, bar` => Out: `foo: fooVal, bar: barVal`
function varDump(/*str, stackTrace=false*/) {
// Parse the arguments
const maybeArray = Array.prototype.slice.call(arguments)[0]
// Parse first argument
const varString = (typeof(maybeArray) === "string") ? maybeArray : maybeArray[0]
// Take all arguments, variable names
var varArray = varString.split(',')
// Take the last argument
const stackTrace = (varArray.pop().trim().toLowerCase() === 'true')
// If we need to create a stack trace
stackTrace && traceIt().forEach((site) => {
log(' \033[36m%s\033[90m in %s:%d\033[0m',
site.getFunctionName() || 'anonymous',
site.getFileName(), site.getLineNumber()
)
})
// For each to variable names log out its current value
varArray
.map((__) => __.trim())
.forEach((__) => logRaw(`${__}: ${eval(__)} `))
// Add a newline at the end
logRaw("\n")
}
const a = "foo"
const b = "bar"
const c = "baz"
//echo`The value of: a=$a, b=$b, and c=$c`
//varDump`a, b, c, false`
module.exports = { varDump, echo }
// First argument will be an `array` when called as: log`something`
// First argument will be a `string` when called as: log(`something`)
function log(/* arguments */) {
var maybeArray = Array.prototype.slice.call(arguments)[0]
var resultString = (typeof(maybeArray) === "string") ? maybeArray : maybeArray[0]
console.log(resultString)
}
// Can call function without `()`
log`something`
// Can call function with `()`
log(`something`)
@afaur
Copy link
Author

afaur commented Jun 9, 2017

LogUtil.js has methods that can help with debugging, but if used while inside of a function block (and not in the global scope) then you will not be able to get the values of the variables.

@afaur
Copy link
Author

afaur commented Jun 10, 2017

TODO: Refactor approach to use something similar to this after refactoring to include some observed limitations listed below..

echo  = function() { /* noop */ }
debug = {}

Object.defineProperty(debug, 'fn', {
  get: function() {
    const toLog = (full, matchedTemplateStr, offset) => {
      const wrapWithConsoleLog  = (line)      => ("console.log('" + line + "')")
      const wrapAsVariableInStr = (variable)  => ("'+" + variable + "+'")
      const normalizeDollarVar  = (dollarVar) => (Array.from(dollarVar).splice(1).toString())
      const replaceDollarVar = (full, matchedDollarVar, offset) => (
        wrapAsVariableInStr( normalizeDollarVar(matchedDollarVar) )
      )
      line = matchedTemplateStr.replace(/(\$[^\W]+)/g, replaceDollarVar)
      line = wrapWithConsoleLog(line)
      return line
    }
    let fn = arguments.callee.caller.toString()
    fn = fn.replace(/return debug\.fn/g, '')
    fn = fn.replace(/echo`(.*)`/g, toLog)
    eval('var ___fn = '+fn)
    return ___fn()
  }
})

var b = 20

function foo () {
  return debug.fn

  var a = 10

  echo`hello $a+$b=30`

  return 5
}

// Show that we can get a result back from the function still
var result = foo()

// Log the return value from the function call
console.log(result)

Use return debug.fn at the beginning of any function you would like to debug inside.
Wherever you use echo to inspect inside that function should work now because we replace it.

This works by using arguments.callee.caller to get all the code of the function, that the current function was called inside of.
This program alters the original functions code and runs the altered version of the function respecting return values.

Some problems I can see are:

  • Make the regex better to include $this.something for matching.
  • Make it work to carry this over to the newly executing function.
  • Regex replacements should take into account possible ; at end of code lines.
  • In strict mode arguments.callee.caller is not available so I have written an alternative...
echo  = function() { /* noop */ }

debug = {
  fn: function(prevFn) {
      const toLog = (full, matchedTemplateStr, offset) => {
      const wrapWithConsoleLog  = (line)      => ("console.log('" + line + "')")
      const wrapAsVariableInStr = (variable)  => ("'+" + variable + "+'")
      const normalizeDollarVar  = (dollarVar) => (Array.from(dollarVar).splice(1).toString())
      const replaceDollarVar = (full, matchedDollarVar, offset) => (
        wrapAsVariableInStr( normalizeDollarVar(matchedDollarVar) )
      )
      line = matchedTemplateStr.replace(/(\$[^\W]+)/g, replaceDollarVar)
      line = wrapWithConsoleLog(line)
      return line
    }
    let fn = prevFn.toString()
    fn = fn.replace(/return debug\.fn\.bind\(this, foo\)\(\)/g, '')
    fn = fn.replace(/echo`(.*)`/g, toLog)
    eval(`var ___fn = ${fn}.bind(this)`)
    return ___fn()
  }
}

var b = 20

function foo () {
  return debug.fn.bind(this, foo)()
  var a = 10

  echo`hello $a+$b=30`
  return 5
}

var result = foo()

console.log(result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment