Path: blob/trunk/third_party/closure/goog/messaging/loggerserver.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 listens on a message channel for logger commands and16* logs them on the local page. This is useful when dealing with message17* channels to contexts that don't have access to their own logging facilities.18*19*/2021goog.provide('goog.messaging.LoggerServer');2223goog.require('goog.Disposable');24goog.require('goog.log');25goog.require('goog.log.Level');26272829/**30* Creates a logger server that logs messages on behalf of the remote end of a31* message channel. The remote end of the channel should use a32* {goog.messaging.LoggerClient} with the same service name.33*34* @param {!goog.messaging.MessageChannel} channel The channel that is sending35* the log messages.36* @param {string} serviceName The name of the logging service to listen for.37* @param {string=} opt_channelName The name of this channel. Used to help38* distinguish this client's messages.39* @constructor40* @extends {goog.Disposable}41* @final42*/43goog.messaging.LoggerServer = function(channel, serviceName, opt_channelName) {44goog.messaging.LoggerServer.base(this, 'constructor');4546/**47* The channel that is sending the log messages.48* @type {!goog.messaging.MessageChannel}49* @private50*/51this.channel_ = channel;5253/**54* The name of the logging service to listen for.55* @type {string}56* @private57*/58this.serviceName_ = serviceName;5960/**61* The name of the channel.62* @type {string}63* @private64*/65this.channelName_ = opt_channelName || 'remote logger';6667this.channel_.registerService(68this.serviceName_, goog.bind(this.log_, this), true /* opt_json */);69};70goog.inherits(goog.messaging.LoggerServer, goog.Disposable);717273/**74* Handles logging messages from the client.75* @param {!Object|string} message76* The logging information from the client.77* @private78*/79goog.messaging.LoggerServer.prototype.log_ = function(message) {80var args =81/**82* @type {{level: number, message: string,83* name: string, exception: Object}}84*/ (message);85var level = goog.log.Level.getPredefinedLevelByValue(args['level']);86if (level) {87var msg = '[' + this.channelName_ + '] ' + args['message'];88goog.log.getLogger(args['name']).log(level, msg, args['exception']);89}90};919293/** @override */94goog.messaging.LoggerServer.prototype.disposeInternal = function() {95goog.messaging.LoggerServer.base(this, 'disposeInternal');96this.channel_.registerService(this.serviceName_, goog.nullFunction, true);97delete this.channel_;98};99100101