I implemented very similar functions in Java and C, passing the prefix line to the logging function and adding a space to the prefix line at each recursion level. This is probably a little more efficient than looping to build a prefix line every time you want to print something.
So you might be lucky with something like:
// Log with depth function dlog() { var depth = arguments[arguments.length - 1]; var real_args = []; real_args.push(prefix); for(var i = 0; i < arguments.length - 1; i++) real_args.push(arguments[i]); console.log.apply(console, real_args); } // Just walk through the node tree, logging as we go. function walk_node(node, prefix) { if (! prefix) { prefix = ''; } dlog('walking node: ', node, prefix); console.log('walking node: ', node); var child = node.children('.node').first(); if(child.length > 0) walk_node(child, prefix + ' '); }
aroth source share