| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Simple logger that logs to the window console if available. |
| 17 |
* |
| 18 |
* Has an autoInstall option which can be put into initialization code, which |
| 19 |
* will start logging if "Debug=true" is in document.location.href |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.debug.Console'); |
| 24 |
|
| 25 |
goog.require('goog.debug.LogManager'); |
| 26 |
goog.require('goog.debug.Logger'); |
| 27 |
goog.require('goog.debug.TextFormatter'); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Create and install a log handler that logs to window.console if available |
| 33 |
* @constructor |
| 34 |
*/ |
| 35 |
goog.debug.Console = function() { |
| 36 |
this.publishHandler_ = goog.bind(this.addLogRecord, this); |
| 37 |
|
| 38 |
/** |
| 39 |
* Formatter for formatted output. |
| 40 |
* @type {!goog.debug.TextFormatter} |
| 41 |
* @private |
| 42 |
*/ |
| 43 |
this.formatter_ = new goog.debug.TextFormatter(); |
| 44 |
this.formatter_.showAbsoluteTime = false; |
| 45 |
this.formatter_.showExceptionText = false; |
| 46 |
// The console logging methods automatically append a newline. |
| 47 |
this.formatter_.appendNewline = false; |
| 48 |
|
| 49 |
this.isCapturing_ = false; |
| 50 |
this.logBuffer_ = ''; |
| 51 |
|
| 52 |
/** |
| 53 |
* Loggers that we shouldn't output. |
| 54 |
* @type {!Object<boolean>} |
| 55 |
* @private |
| 56 |
*/ |
| 57 |
this.filteredLoggers_ = {}; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the text formatter used by this console |
| 63 |
* @return {!goog.debug.TextFormatter} The text formatter. |
| 64 |
*/ |
| 65 |
goog.debug.Console.prototype.getFormatter = function() { |
| 66 |
return this.formatter_; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Sets whether we are currently capturing logger output. |
| 72 |
* @param {boolean} capturing Whether to capture logger output. |
| 73 |
*/ |
| 74 |
goog.debug.Console.prototype.setCapturing = function(capturing) { |
| 75 |
if (capturing == this.isCapturing_) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// attach or detach handler from the root logger |
| 80 |
var rootLogger = goog.debug.LogManager.getRoot(); |
| 81 |
if (capturing) { |
| 82 |
rootLogger.addHandler(this.publishHandler_); |
| 83 |
} else { |
| 84 |
rootLogger.removeHandler(this.publishHandler_); |
| 85 |
this.logBuffer = ''; |
| 86 |
} |
| 87 |
this.isCapturing_ = capturing; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Adds a log record. |
| 93 |
* @param {goog.debug.LogRecord} logRecord The log entry. |
| 94 |
*/ |
| 95 |
goog.debug.Console.prototype.addLogRecord = function(logRecord) { |
| 96 |
|
| 97 |
// Check to see if the log record is filtered or not. |
| 98 |
if (this.filteredLoggers_[logRecord.getLoggerName()]) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
var record = this.formatter_.formatRecord(logRecord); |
| 103 |
var console = goog.debug.Console.console_; |
| 104 |
if (console) { |
| 105 |
switch (logRecord.getLevel()) { |
| 106 |
case goog.debug.Logger.Level.SHOUT: |
| 107 |
goog.debug.Console.logToConsole_(console, 'info', record); |
| 108 |
break; |
| 109 |
case goog.debug.Logger.Level.SEVERE: |
| 110 |
goog.debug.Console.logToConsole_(console, 'error', record); |
| 111 |
break; |
| 112 |
case goog.debug.Logger.Level.WARNING: |
| 113 |
goog.debug.Console.logToConsole_(console, 'warn', record); |
| 114 |
break; |
| 115 |
default: |
| 116 |
goog.debug.Console.logToConsole_(console, 'log', record); |
| 117 |
break; |
| 118 |
} |
| 119 |
} else { |
| 120 |
this.logBuffer_ += record; |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Adds a logger name to be filtered. |
| 127 |
* @param {string} loggerName the logger name to add. |
| 128 |
*/ |
| 129 |
goog.debug.Console.prototype.addFilter = function(loggerName) { |
| 130 |
this.filteredLoggers_[loggerName] = true; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Removes a logger name to be filtered. |
| 136 |
* @param {string} loggerName the logger name to remove. |
| 137 |
*/ |
| 138 |
goog.debug.Console.prototype.removeFilter = function(loggerName) { |
| 139 |
delete this.filteredLoggers_[loggerName]; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Global console logger instance |
| 145 |
* @type {goog.debug.Console} |
| 146 |
*/ |
| 147 |
goog.debug.Console.instance = null; |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* The console to which to log. This is a property so it can be mocked out in |
| 152 |
* this unit test for goog.debug.Console. Using goog.global, as console might be |
| 153 |
* used in window-less contexts. |
| 154 |
* @type {!{log:!Function}} |
| 155 |
* @private |
| 156 |
*/ |
| 157 |
goog.debug.Console.console_ = goog.global['console']; |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Sets the console to which to log. |
| 162 |
* @param {!Object} console The console to which to log. |
| 163 |
*/ |
| 164 |
goog.debug.Console.setConsole = function(console) { |
| 165 |
goog.debug.Console.console_ = /** @type {!{log:!Function}} */ (console); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Install the console and start capturing if "Debug=true" is in the page URL |
| 171 |
*/ |
| 172 |
goog.debug.Console.autoInstall = function() { |
| 173 |
if (!goog.debug.Console.instance) { |
| 174 |
goog.debug.Console.instance = new goog.debug.Console(); |
| 175 |
} |
| 176 |
|
| 177 |
if (goog.global.location && |
| 178 |
goog.global.location.href.indexOf('Debug=true') != -1) { |
| 179 |
goog.debug.Console.instance.setCapturing(true); |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Show an alert with all of the captured debug information. |
| 186 |
* Information is only captured if console is not available |
| 187 |
*/ |
| 188 |
goog.debug.Console.show = function() { |
| 189 |
alert(goog.debug.Console.instance.logBuffer_); |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Logs the record to the console using the given function. If the function is |
| 195 |
* not available on the console object, the log function is used instead. |
| 196 |
* @param {!{log:!Function}} console The console object. |
| 197 |
* @param {string} fnName The name of the function to use. |
| 198 |
* @param {string} record The record to log. |
| 199 |
* @private |
| 200 |
*/ |
| 201 |
goog.debug.Console.logToConsole_ = function(console, fnName, record) { |
| 202 |
if (console[fnName]) { |
| 203 |
console[fnName](record); |
| 204 |
} else { |
| 205 |
console.log(record); |
| 206 |
} |
| 207 |
}; |