loggerserver.js 3.1 KB

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