debug.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2007 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>Debug</title>
  10. <script type="text/javascript" src="../base.js"></script>
  11. <script type="text/javascript">
  12. goog.require('goog.debug');
  13. goog.require('goog.debug.FancyWindow');
  14. goog.require('goog.log');
  15. /**
  16. * Person - a sample person object.
  17. * @param {string} name The name
  18. * @param {number} age The age
  19. */
  20. var Person = function(name, age) {
  21. this.name_ = name;
  22. this.age_ = age;
  23. this.address_ = null;
  24. this.kids_ = [];
  25. /**
  26. * Set the address.
  27. * @param {string} address The address to set
  28. */
  29. this.setAddress = function(address) {
  30. this.address_ = address;
  31. }
  32. /**
  33. * Add a child.
  34. * @param {Object} child The child to add
  35. */
  36. this.addChild = function(child) {
  37. this.kids_.push(child);
  38. }
  39. /**
  40. * Create a string representation of the object.
  41. * @return {string} The object as a string
  42. */
  43. this.toString = function() {
  44. return 'Person name: ' + this.name_ + ' Age: ' + this.age_;
  45. }
  46. }
  47. /**
  48. * Demonstrate the debug options.
  49. */
  50. var demoDebug = function() {
  51. // Create the debug window.
  52. var debugWindow = new goog.debug.FancyWindow('main');
  53. debugWindow.setEnabled(true);
  54. debugWindow.init();
  55. // Create a logger.
  56. var theLogger = goog.log.getLogger('demo');
  57. goog.log.info(theLogger, 'Logging examples');
  58. // Create a simple object.
  59. var someone = {'name': 'joe',
  60. 'age': 33,
  61. 'gender': 'm',
  62. 'kids': ['jen', 'sam', 'oliver'],
  63. 'address': '233 Great Road, Axtonhammer, MD'};
  64. // Show the object, note that it will output '[object Object]'.
  65. goog.log.info(theLogger, someone);
  66. // Use expose to walk through the object and show all data.
  67. goog.log.info(theLogger, 'Person: '+ goog.debug.expose(someone));
  68. // Now create a Person object to demonstrate expose w/functions.
  69. var pObj = new Person('fred', 2);
  70. // Add a child, and an address.
  71. pObj.addChild(someone);
  72. pObj.setAddress('1 broadway, ny, ny');
  73. // The toString will be called.
  74. goog.log.info(theLogger, 'toString: '+ pObj);
  75. // Does not show the functions by default.
  76. goog.log.info(theLogger, 'expose (no functions): ' + goog.debug.expose(pObj));
  77. // You can specify false if you really want to.
  78. goog.log.info(theLogger, 'expose (no functions): ' + goog.debug.expose(pObj, false));
  79. // Shows the functions as well.
  80. goog.log.info(theLogger, 'expose (w/functions): ' + goog.debug.expose(pObj, true));
  81. // Show deepExpose, which walks recursively through data.
  82. goog.log.info(theLogger, 'deepExpose (no functions): '+ goog.debug.deepExpose(pObj));
  83. // You can specify false if you really want to.
  84. goog.log.info(theLogger, 'deepExpose (no functions): '+
  85. goog.debug.deepExpose(pObj, false));
  86. goog.log.info(theLogger, 'deepExpose (w/functions): '+
  87. goog.debug.deepExpose(pObj, true));
  88. goog.log.log(theLogger, goog.log.Level.SHOUT, 'shout');
  89. goog.log.error(theLogger, 'severe');
  90. goog.log.warning(theLogger, 'warning');
  91. goog.log.info(theLogger, 'info');
  92. goog.log.fine(theLogger, 'fine');
  93. }
  94. </script>
  95. <body>
  96. Look in the log window for debugging examples.
  97. </body>
  98. <script>
  99. // Call the demo method.
  100. demoDebug();
  101. </script>
  102. </html>