Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. ## logger.coffee
  2. ## console.log for debug prints, filterable with flags in env.DEBUG.
  3. ###?//
  4. To do:
  5. 3. Cache parsed flags internally; since easily mapped to memoized generated functions, can make namespace full blown regexps (strings only, unless RE objects work as hash keys) over flags, so can support grouping, or whatever craziness.
  6. 4. Publish?!
  7. 5. Colors on TTY/logfiles problem?
  8. 6. Unit tests.
  9. 7. Levels: add optional methods for levels, and some syntax for specifying level per namespace. Use "standard" levels, ie Unix syslog.
  10. 8. Option to use different DEBUG delimiter than ','? Why?
  11. #?// Q&D implementation of API I like! Should really parse DEBUG and use
  12. #?// Missing "prefix:*" capability yet! Is grouping that important? Meh. Regexp even better!
  13. ###
  14.  
  15. require 'colors'
  16.  
  17. ## Logger.
  18. d=(f)->
  19. #?//exports.loggers[f]?=if exports.all or f of exports.flags
  20. #?//=(f,m,p...)->if debug.all or f of debug.flags then console.log f.blue+' '+m,p...
  21.  
  22. unless d.loggers[f]?
  23. if (d.all or typeof f is 'string' and f of d.flags or typeof f is 'object' and 'test' of f and d.flags.find (f)->f.test f)
  24. f.replace '%','%%' # Don't parse format specifiers in flag.
  25. d.loggers[f]=(m,p...)->console.log f.blue+' '+m,p...
  26. else
  27. d.loggers[f]=->undefined # No-op.
  28. return d.loggers[f]
  29.  
  30. ###?//
  31. d.loggers[f]?=if (d.all or typeof f is 'string' and f of d.flags or typeof f is 'object' and 'test' of f and d.flags.find (f)->f.test f)
  32. f.replace '%','%%' # Don't parse format specifiers in flag.
  33. (m,p...)->console.log f.blue+' '+m,p...
  34. else
  35. ->undefined # No-op.
  36. ###
  37.  
  38. ## Set flags from string.
  39. d.init=(s,append=no)->
  40. unless append
  41. d.flags={}
  42. d.loggers={}
  43. for v in s.split ',' #?// .map (s)->s.trim(), and then .filter out ''. Or too foolproof?
  44. d.flags[v]=true
  45. d.all='*' of d.flags
  46. # Initialize with env immediately when required.
  47. d.init process.env.DEBUG or ''
  48.  
  49. ###?// Much nicer if on Object's prototype!?
  50. compact=(o)->CSON.stringify(o).split('\n').join ',' #?// Shouldn't this remove indentation whitespace too?
  51. ###
  52.  
  53. ## Export API: logger (anonymous function) and other APIs as properties set on it.
  54. module.exports=d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement