Path: blob/trunk/third_party/closure/goog/debug/console.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Simple logger that logs to the window console if available.16*17* Has an autoInstall option which can be put into initialization code, which18* will start logging if "Debug=true" is in document.location.href19*20*/2122goog.provide('goog.debug.Console');2324goog.require('goog.debug.LogManager');25goog.require('goog.debug.Logger');26goog.require('goog.debug.TextFormatter');27282930/**31* Create and install a log handler that logs to window.console if available32* @constructor33*/34goog.debug.Console = function() {35this.publishHandler_ = goog.bind(this.addLogRecord, this);3637/**38* Formatter for formatted output.39* @type {!goog.debug.TextFormatter}40* @private41*/42this.formatter_ = new goog.debug.TextFormatter();43this.formatter_.showAbsoluteTime = false;44this.formatter_.showExceptionText = false;45// The console logging methods automatically append a newline.46this.formatter_.appendNewline = false;4748this.isCapturing_ = false;49this.logBuffer_ = '';5051/**52* Loggers that we shouldn't output.53* @type {!Object<boolean>}54* @private55*/56this.filteredLoggers_ = {};57};585960/**61* Returns the text formatter used by this console62* @return {!goog.debug.TextFormatter} The text formatter.63*/64goog.debug.Console.prototype.getFormatter = function() {65return this.formatter_;66};676869/**70* Sets whether we are currently capturing logger output.71* @param {boolean} capturing Whether to capture logger output.72*/73goog.debug.Console.prototype.setCapturing = function(capturing) {74if (capturing == this.isCapturing_) {75return;76}7778// attach or detach handler from the root logger79var rootLogger = goog.debug.LogManager.getRoot();80if (capturing) {81rootLogger.addHandler(this.publishHandler_);82} else {83rootLogger.removeHandler(this.publishHandler_);84this.logBuffer = '';85}86this.isCapturing_ = capturing;87};888990/**91* Adds a log record.92* @param {goog.debug.LogRecord} logRecord The log entry.93*/94goog.debug.Console.prototype.addLogRecord = function(logRecord) {9596// Check to see if the log record is filtered or not.97if (this.filteredLoggers_[logRecord.getLoggerName()]) {98return;99}100101var record = this.formatter_.formatRecord(logRecord);102var console = goog.debug.Console.console_;103if (console) {104switch (logRecord.getLevel()) {105case goog.debug.Logger.Level.SHOUT:106goog.debug.Console.logToConsole_(console, 'info', record);107break;108case goog.debug.Logger.Level.SEVERE:109goog.debug.Console.logToConsole_(console, 'error', record);110break;111case goog.debug.Logger.Level.WARNING:112goog.debug.Console.logToConsole_(console, 'warn', record);113break;114default:115goog.debug.Console.logToConsole_(console, 'debug', record);116break;117}118} else {119this.logBuffer_ += record;120}121};122123124/**125* Adds a logger name to be filtered.126* @param {string} loggerName the logger name to add.127*/128goog.debug.Console.prototype.addFilter = function(loggerName) {129this.filteredLoggers_[loggerName] = true;130};131132133/**134* Removes a logger name to be filtered.135* @param {string} loggerName the logger name to remove.136*/137goog.debug.Console.prototype.removeFilter = function(loggerName) {138delete this.filteredLoggers_[loggerName];139};140141142/**143* Global console logger instance144* @type {goog.debug.Console}145*/146goog.debug.Console.instance = null;147148149/**150* The console to which to log. This is a property so it can be mocked out in151* this unit test for goog.debug.Console. Using goog.global, as console might be152* used in window-less contexts.153* @type {!{log:!Function}}154* @private155*/156goog.debug.Console.console_ = goog.global['console'];157158159/**160* Sets the console to which to log.161* @param {!Object} console The console to which to log.162*/163goog.debug.Console.setConsole = function(console) {164goog.debug.Console.console_ = /** @type {!{log:!Function}} */ (console);165};166167168/**169* Install the console and start capturing if "Debug=true" is in the page URL170*/171goog.debug.Console.autoInstall = function() {172if (!goog.debug.Console.instance) {173goog.debug.Console.instance = new goog.debug.Console();174}175176if (goog.global.location &&177goog.global.location.href.indexOf('Debug=true') != -1) {178goog.debug.Console.instance.setCapturing(true);179}180};181182183/**184* Show an alert with all of the captured debug information.185* Information is only captured if console is not available186*/187goog.debug.Console.show = function() {188alert(goog.debug.Console.instance.logBuffer_);189};190191192/**193* Logs the record to the console using the given function. If the function is194* not available on the console object, the log function is used instead.195* @param {!{log:!Function}} console The console object.196* @param {string} fnName The name of the function to use.197* @param {string} record The record to log.198* @private199*/200goog.debug.Console.logToConsole_ = function(console, fnName, record) {201if (console[fnName]) {202console[fnName](record);203} else {204console.log(record);205}206};207208209