loggerclient.js 4.2 KB

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