| 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 sends logging messages over a message channel to a |
| 17 |
* server on the main page that prints them using standard logging mechanisms. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.messaging.LoggerClient'); |
| 22 |
|
| 23 |
goog.require('goog.Disposable'); |
| 24 |
goog.require('goog.debug'); |
| 25 |
goog.require('goog.debug.LogManager'); |
| 26 |
goog.require('goog.debug.Logger'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Creates a logger client that sends messages along a message channel for the |
| 32 |
* remote end to log. The remote end of the channel should use a |
| 33 |
* {goog.messaging.LoggerServer} with the same service name. |
| 34 |
* |
| 35 |
* @param {!goog.messaging.MessageChannel} channel The channel that on which to |
| 36 |
* send the log messages. |
| 37 |
* @param {string} serviceName The name of the logging service to use. |
| 38 |
* @constructor |
| 39 |
* @extends {goog.Disposable} |
| 40 |
* @final |
| 41 |
*/ |
| 42 |
goog.messaging.LoggerClient = function(channel, serviceName) { |
| 43 |
if (goog.messaging.LoggerClient.instance_) { |
| 44 |
return goog.messaging.LoggerClient.instance_; |
| 45 |
} |
| 46 |
|
| 47 |
goog.messaging.LoggerClient.base(this, 'constructor'); |
| 48 |
|
| 49 |
/** |
| 50 |
* The channel on which to send the log messages. |
| 51 |
* @type {!goog.messaging.MessageChannel} |
| 52 |
* @private |
| 53 |
*/ |
| 54 |
this.channel_ = channel; |
| 55 |
|
| 56 |
/** |
| 57 |
* The name of the logging service to use. |
| 58 |
* @type {string} |
| 59 |
* @private |
| 60 |
*/ |
| 61 |
this.serviceName_ = serviceName; |
| 62 |
|
| 63 |
/** |
| 64 |
* The bound handler function for handling log messages. This is kept in a |
| 65 |
* variable so that it can be deregistered when the logger client is disposed. |
| 66 |
* @type {Function} |
| 67 |
* @private |
| 68 |
*/ |
| 69 |
this.publishHandler_ = goog.bind(this.sendLog_, this); |
| 70 |
goog.debug.LogManager.getRoot().addHandler(this.publishHandler_); |
| 71 |
|
| 72 |
goog.messaging.LoggerClient.instance_ = this; |
| 73 |
}; |
| 74 |
goog.inherits(goog.messaging.LoggerClient, goog.Disposable); |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* The singleton instance, if any. |
| 79 |
* @type {goog.messaging.LoggerClient} |
| 80 |
* @private |
| 81 |
*/ |
| 82 |
goog.messaging.LoggerClient.instance_ = null; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Sends a log message through the channel. |
| 87 |
* @param {!goog.debug.LogRecord} logRecord The log message. |
| 88 |
* @private |
| 89 |
*/ |
| 90 |
goog.messaging.LoggerClient.prototype.sendLog_ = function(logRecord) { |
| 91 |
var name = logRecord.getLoggerName(); |
| 92 |
var level = logRecord.getLevel(); |
| 93 |
var msg = logRecord.getMessage(); |
| 94 |
var originalException = logRecord.getException(); |
| 95 |
|
| 96 |
var exception; |
| 97 |
if (originalException) { |
| 98 |
var normalizedException = |
| 99 |
goog.debug.normalizeErrorObject(originalException); |
| 100 |
exception = { |
| 101 |
'name': normalizedException.name, |
| 102 |
'message': normalizedException.message, |
| 103 |
'lineNumber': normalizedException.lineNumber, |
| 104 |
'fileName': normalizedException.fileName, |
| 105 |
// Normalized exceptions without a stack have 'stack' set to 'Not |
| 106 |
// available', so we check for the existence of 'stack' on the original |
| 107 |
// exception instead. |
| 108 |
'stack': originalException.stack || |
| 109 |
goog.debug.getStacktrace(goog.debug.Logger.prototype.log) |
| 110 |
}; |
| 111 |
|
| 112 |
if (goog.isObject(originalException)) { |
| 113 |
// Add messageN to the exception in case it was added using |
| 114 |
// goog.debug.enhanceError. |
| 115 |
for (var i = 0; 'message' + i in originalException; i++) { |
| 116 |
exception['message' + i] = String(originalException['message' + i]); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
this.channel_.send(this.serviceName_, { |
| 121 |
'name': name, |
| 122 |
'level': level.value, |
| 123 |
'message': msg, |
| 124 |
'exception': exception |
| 125 |
}); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/** @override */ |
| 130 |
goog.messaging.LoggerClient.prototype.disposeInternal = function() { |
| 131 |
goog.messaging.LoggerClient.base(this, 'disposeInternal'); |
| 132 |
goog.debug.LogManager.getRoot().removeHandler(this.publishHandler_); |
| 133 |
delete this.channel_; |
| 134 |
goog.messaging.LoggerClient.instance_ = null; |
| 135 |
}; |