Created
September 27, 2012 21:55
Node debugging functions
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
global.debug = ( ) -> | |
tabs = ( n ) -> ( "\t" for i in [0...n] ).join( '' ) | |
for i in [0...arguments.length] | |
arg = arguments[i] | |
type = get_type arg | |
# last arg is boolean? sleep if true | |
if ( i + 1 ) is arguments.length and type is 'Boolean' | |
if arg | |
sleep = require 'sleep' | |
sleep.sleep 5 | |
continue | |
if arg and i is 0 | |
arg = "\n" + arg | |
if type in ['Object', 'Array'] | |
for key, value of arg when arg.hasOwnProperty key | |
if key is '_id' | |
value = value.toString() | |
try | |
json = JSON.stringify value, null, ' ' | |
catch e | |
try | |
json = JSON.stringify_circular value | |
catch e | |
throw e | |
console.log tabs( i ), key, ':', json | |
else | |
console.log tabs( i ), arg | |
true | |
JSON.stringify_circular = (object) -> | |
custom_json = (key, value) -> | |
if typeof value is 'object' and value isnt null | |
if value in custom_json.cache | |
return 'CIRCULAR' | |
custom_json.cache.push value | |
return value | |
custom_json.cache = [] | |
ret = JSON.stringify object, custom_json, ' ' | |
custom_json.cache = null | |
return ret | |
global.debug_wrapped = ( ) -> | |
console.log '\n\n\n##########################################################\n\n' | |
debug.apply this, arguments | |
console.log '\n\n\n##########################################################\n\n' | |
global.show_stack = ( ) -> | |
try | |
everything_you_hold_dear.doesnt.exist += 1 | |
catch e | |
debug_wrapped e.stack |
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
debug 'What this is debugging/logging', thing: thing, count: thing.length | |
debug_wrapped 'I want to know about this', very_important: thing | |
# How did I get here? | |
show_stack() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment