Path: blob/trunk/third_party/closure/goog/messaging/loggerclient.js
2868 views
// Copyright 2010 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 This class sends logging messages over a message channel to a16* server on the main page that prints them using standard logging mechanisms.17*18*/1920goog.provide('goog.messaging.LoggerClient');2122goog.require('goog.Disposable');23goog.require('goog.debug');24goog.require('goog.debug.LogManager');25goog.require('goog.debug.Logger');26272829/**30* Creates a logger client that sends messages along a message channel for the31* remote end to log. The remote end of the channel should use a32* {goog.messaging.LoggerServer} with the same service name.33*34* @param {!goog.messaging.MessageChannel} channel The channel that on which to35* send the log messages.36* @param {string} serviceName The name of the logging service to use.37* @constructor38* @extends {goog.Disposable}39* @final40*/41goog.messaging.LoggerClient = function(channel, serviceName) {42if (goog.messaging.LoggerClient.instance_) {43return goog.messaging.LoggerClient.instance_;44}4546goog.messaging.LoggerClient.base(this, 'constructor');4748/**49* The channel on which to send the log messages.50* @type {!goog.messaging.MessageChannel}51* @private52*/53this.channel_ = channel;5455/**56* The name of the logging service to use.57* @type {string}58* @private59*/60this.serviceName_ = serviceName;6162/**63* The bound handler function for handling log messages. This is kept in a64* variable so that it can be deregistered when the logger client is disposed.65* @type {Function}66* @private67*/68this.publishHandler_ = goog.bind(this.sendLog_, this);69goog.debug.LogManager.getRoot().addHandler(this.publishHandler_);7071goog.messaging.LoggerClient.instance_ = this;72};73goog.inherits(goog.messaging.LoggerClient, goog.Disposable);747576/**77* The singleton instance, if any.78* @type {goog.messaging.LoggerClient}79* @private80*/81goog.messaging.LoggerClient.instance_ = null;828384/**85* Sends a log message through the channel.86* @param {!goog.debug.LogRecord} logRecord The log message.87* @private88*/89goog.messaging.LoggerClient.prototype.sendLog_ = function(logRecord) {90var name = logRecord.getLoggerName();91var level = logRecord.getLevel();92var msg = logRecord.getMessage();93var originalException = logRecord.getException();9495var exception;96if (originalException) {97var normalizedException =98goog.debug.normalizeErrorObject(originalException);99exception = {100'name': normalizedException.name,101'message': normalizedException.message,102'lineNumber': normalizedException.lineNumber,103'fileName': normalizedException.fileName,104// Normalized exceptions without a stack have 'stack' set to 'Not105// available', so we check for the existence of 'stack' on the original106// exception instead.107'stack': originalException.stack ||108goog.debug.getStacktrace(goog.debug.Logger.prototype.log)109};110111if (goog.isObject(originalException)) {112// Add messageN to the exception in case it was added using113// goog.debug.enhanceError.114for (var i = 0; 'message' + i in originalException; i++) {115exception['message' + i] = String(originalException['message' + i]);116}117}118}119this.channel_.send(this.serviceName_, {120'name': name,121'level': level.value,122'message': msg,123'exception': exception124});125};126127128/** @override */129goog.messaging.LoggerClient.prototype.disposeInternal = function() {130goog.messaging.LoggerClient.base(this, 'disposeInternal');131goog.debug.LogManager.getRoot().removeHandler(this.publishHandler_);132delete this.channel_;133goog.messaging.LoggerClient.instance_ = null;134};135136137