summaryrefslogtreecommitdiffstats
path: root/testing/marionette/logging.js
blob: d77adfb0cbfe446631539e0972947e4e487bbbe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = ["logging"];

this.logging = {};

/** Simple logger that is used in Simple Test harness tests. */
logging.ContentLogger = class {
  constructor() {
    this.logs_ = [];
  }

  /**
   * Append a log entry.
   *
   * @param {string} message
   *     Log entry message.
   * @param {string=} level
   *     Severity of entry.  Defaults to "INFO".
   */
  log(message, level = "INFO") {
    let now = (new Date()).toString();
    this.logs_.push([level, message, now]);
  }

  /**
   * Append array of log entries.
   *
   * @param {Array.<Array<string, string, string>>} messages
   *     List of log entries, that are of the form severity, message,
   *     and date.
   */
  addAll(messages) {
    for (let message of messages) {
      this.logs_.push(message);
    }
  }

  /**
   * Gets current log entries and clears the cache.
   *
   * @return {Array.<Array<string, string, string>>}
   *     Log entries of the form severity, message, and date.
   */
  get() {
    let logs = this.logs_;
    this.logs_ = [];
    return logs;
  }
};

/**
 * Adapts an instance of ContentLogger for use in a sandbox.  Is consumed
 * by sandbox.augment.
 */
logging.Adapter = class {
  constructor(logger = null) {
    this.logger = logger;
  }

  get exports() {
    return new Map([["log", this.log.bind(this)]]);
  }

  log(message, level = "INFO") {
    dump(`MARIONETTE LOG: ${level}: ${message}\n`);
    if (this.logger) {
      this.logger.log(message, level);
    }
  }
};