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