debug.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Debugging support for web applications.
  3. *
  4. * @author David I. Lehn <dlehn@digitalbazaar.com>
  5. *
  6. * Copyright 2008-2013 Digital Bazaar, Inc.
  7. */
  8. var forge = require('./forge');
  9. /* DEBUG API */
  10. module.exports = forge.debug = forge.debug || {};
  11. // Private storage for debugging.
  12. // Useful to expose data that is otherwise unviewable behind closures.
  13. // NOTE: remember that this can hold references to data and cause leaks!
  14. // format is "forge._debug.<modulename>.<dataname> = data"
  15. // Example:
  16. // (function() {
  17. // var cat = 'forge.test.Test'; // debugging category
  18. // var sState = {...}; // local state
  19. // forge.debug.set(cat, 'sState', sState);
  20. // })();
  21. forge.debug.storage = {};
  22. /**
  23. * Gets debug data. Omit name for all cat data Omit name and cat for
  24. * all data.
  25. *
  26. * @param cat name of debugging category.
  27. * @param name name of data to get (optional).
  28. * @return object with requested debug data or undefined.
  29. */
  30. forge.debug.get = function(cat, name) {
  31. var rval;
  32. if(typeof(cat) === 'undefined') {
  33. rval = forge.debug.storage;
  34. } else if(cat in forge.debug.storage) {
  35. if(typeof(name) === 'undefined') {
  36. rval = forge.debug.storage[cat];
  37. } else {
  38. rval = forge.debug.storage[cat][name];
  39. }
  40. }
  41. return rval;
  42. };
  43. /**
  44. * Sets debug data.
  45. *
  46. * @param cat name of debugging category.
  47. * @param name name of data to set.
  48. * @param data data to set.
  49. */
  50. forge.debug.set = function(cat, name, data) {
  51. if(!(cat in forge.debug.storage)) {
  52. forge.debug.storage[cat] = {};
  53. }
  54. forge.debug.storage[cat][name] = data;
  55. };
  56. /**
  57. * Clears debug data. Omit name for all cat data. Omit name and cat for
  58. * all data.
  59. *
  60. * @param cat name of debugging category.
  61. * @param name name of data to clear or omit to clear entire category.
  62. */
  63. forge.debug.clear = function(cat, name) {
  64. if(typeof(cat) === 'undefined') {
  65. forge.debug.storage = {};
  66. } else if(cat in forge.debug.storage) {
  67. if(typeof(name) === 'undefined') {
  68. delete forge.debug.storage[cat];
  69. } else {
  70. delete forge.debug.storage[cat][name];
  71. }
  72. }
  73. };