Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Created September 27, 2012 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richthegeek/3796687 to your computer and use it in GitHub Desktop.
Save richthegeek/3796687 to your computer and use it in GitHub Desktop.
Node debugging functions
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
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