Debian Codegraph @

closure-library/closure/goog/messaging/loggerserver.js

raw
1 // Copyright 2010 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 This class listens on a message channel for logger commands and
17 * logs them on the local page. This is useful when dealing with message
18 * channels to contexts that don't have access to their own logging facilities.
19 *
20 */
21
22 goog.provide('goog.messaging.LoggerServer');
23
24 goog.require('goog.Disposable');
25 goog.require('goog.log');
26 goog.require('goog.log.Level');
27
28
29
30 /**
31 * Creates a logger server that logs messages on behalf of the remote end of a
32 * message channel. The remote end of the channel should use a
33 * {goog.messaging.LoggerClient} with the same service name.
34 *
35 * @param {!goog.messaging.MessageChannel} channel The channel that is sending
36 * the log messages.
37 * @param {string} serviceName The name of the logging service to listen for.
38 * @param {string=} opt_channelName The name of this channel. Used to help
39 * distinguish this client's messages.
40 * @constructor
41 * @extends {goog.Disposable}
42 * @final
43 */
44 goog.messaging.LoggerServer = function(channel, serviceName, opt_channelName) {
45 goog.messaging.LoggerServer.base(this, 'constructor');
46
47 /**
48 * The channel that is sending the log messages.
49 * @type {!goog.messaging.MessageChannel}
50 * @private
51 */
52 this.channel_ = channel;
53
54 /**
55 * The name of the logging service to listen for.
56 * @type {string}
57 * @private
58 */
59 this.serviceName_ = serviceName;
60
61 /**
62 * The name of the channel.
63 * @type {string}
64 * @private
65 */
66 this.channelName_ = opt_channelName || 'remote logger';
67
68 this.channel_.registerService(
69 this.serviceName_, goog.bind(this.log_, this), true /* opt_json */);
70 };
71 goog.inherits(goog.messaging.LoggerServer, goog.Disposable);
72
73
74 /**
75 * Handles logging messages from the client.
76 * @param {!Object|string} message
77 * The logging information from the client.
78 * @private
79 */
80 goog.messaging.LoggerServer.prototype.log_ = function(message) {
81 var args =
82 /**
83 * @type {{level: number, message: string,
84 * name: string, exception: Object}}
85 */ (message);
86 var level = goog.log.Level.getPredefinedLevelByValue(args['level']);
87 if (level) {
88 var msg = '[' + this.channelName_ + '] ' + args['message'];
89 goog.log.getLogger(args['name']).log(level, msg, args['exception']);
90 }
91 };
92
93
94 /** @override */
95 goog.messaging.LoggerServer.prototype.disposeInternal = function() {
96 goog.messaging.LoggerServer.base(this, 'disposeInternal');
97 this.channel_.registerService(this.serviceName_, goog.nullFunction, true);
98 delete this.channel_;
99 };