logging.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. this.EXPORTED_SYMBOLS = ["logging"];
  6. this.logging = {};
  7. /** Simple logger that is used in Simple Test harness tests. */
  8. logging.ContentLogger = class {
  9. constructor() {
  10. this.logs_ = [];
  11. }
  12. /**
  13. * Append a log entry.
  14. *
  15. * @param {string} message
  16. * Log entry message.
  17. * @param {string=} level
  18. * Severity of entry. Defaults to "INFO".
  19. */
  20. log(message, level = "INFO") {
  21. let now = (new Date()).toString();
  22. this.logs_.push([level, message, now]);
  23. }
  24. /**
  25. * Append array of log entries.
  26. *
  27. * @param {Array.<Array<string, string, string>>} messages
  28. * List of log entries, that are of the form severity, message,
  29. * and date.
  30. */
  31. addAll(messages) {
  32. for (let message of messages) {
  33. this.logs_.push(message);
  34. }
  35. }
  36. /**
  37. * Gets current log entries and clears the cache.
  38. *
  39. * @return {Array.<Array<string, string, string>>}
  40. * Log entries of the form severity, message, and date.
  41. */
  42. get() {
  43. let logs = this.logs_;
  44. this.logs_ = [];
  45. return logs;
  46. }
  47. };
  48. /**
  49. * Adapts an instance of ContentLogger for use in a sandbox. Is consumed
  50. * by sandbox.augment.
  51. */
  52. logging.Adapter = class {
  53. constructor(logger = null) {
  54. this.logger = logger;
  55. }
  56. get exports() {
  57. return new Map([["log", this.log.bind(this)]]);
  58. }
  59. log(message, level = "INFO") {
  60. dump(`MARIONETTE LOG: ${level}: ${message}\n`);
  61. if (this.logger) {
  62. this.logger.log(message, level);
  63. }
  64. }
  65. };