Created
July 24, 2015 16:48
-
-
Save zeusdeux/2a5a534f650b0a99e61a to your computer and use it in GitHub Desktop.
Pretty indented stack traces
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
/* | |
# | |
# Example final output: | |
# | |
# 1000 main | |
# 1000 workLoop | |
# 900 read | |
# 900 __sys_read | |
# 100 parse | |
# 100 strcmp | |
# | |
*/ | |
function getStacktraces() { | |
return [ | |
["main","parse_args", "parse_entry"], | |
["main", "workloop", "select"], | |
["main", "workloop", "parse_data", "parse_entry"], | |
["main", "workloop", "select"] | |
] | |
} | |
function simplifyTraces(stacktraces) { | |
var reducer = function(p, c) { | |
var fnName = c; | |
p[fnName] = p[fnName] || {}; | |
p[fnName].count = undefined === p[fnName].count? 1 : p[fnName].count + 1; | |
p[fnName].next = p[fnName].next || {}; | |
return p[fnName].next; // to enable chaining | |
} | |
return stacktraces.reduce(function(p, stacktrace){ | |
stacktrace.reduce(reducer, p); | |
return p; // since p = {} and will be passed by ref, everything side effects it | |
}, {}); | |
} | |
function pp(obj, initLevel) { | |
Object.keys(obj).forEach(function(key) { | |
function walk(obj, key, level) { | |
if (undefined !== obj) { | |
console.log('\t'.repeat(level), obj.count, key); | |
pp(obj.next, ++level); // mutual recursion ftw | |
} | |
} | |
walk(obj[key], key, initLevel || 0); | |
}); | |
} | |
pp(simplifyTraces(getStacktraces()), 0); | |
/* voila! */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment