summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/pageloader/chrome/MozillaFileLogger.js
blob: 3da728ef677fd526e1f20c265074a4fb8c12ee03 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 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/. */

/**
 * MozillaFileLogger, a log listener that can write to a local file.
 */

// double logging to account for normal mode and ipc mode (mobile_profile only)
// Ideally we would remove the dump() and just do ipc logging
function dumpLog(msg) {
  dump(msg);
  MozillaFileLogger.log(msg);
}

const FOSTREAM_CID = "@mozilla.org/network/file-output-stream;1";
const LF_CID = "@mozilla.org/file/local;1";

// File status flags. It is a bitwise OR of the following bit flags.
// Only one of the first three flags below may be used.
const PR_READ_ONLY = 0x01; // Open for reading only.
const PR_WRITE_ONLY = 0x02; // Open for writing only.
const PR_READ_WRITE = 0x04; // Open for reading and writing.

// If the file does not exist, the file is created.
// If the file exists, this flag has no effect.
const PR_CREATE_FILE = 0x08;

// The file pointer is set to the end of the file prior to each write.
const PR_APPEND = 0x10;

// If the file exists, its length is truncated to 0.
const PR_TRUNCATE = 0x20;

// If set, each write will wait for both the file data
// and file status to be physically updated.
const PR_SYNC = 0x40;

// If the file does not exist, the file is created. If the file already
// exists, no action and NULL is returned.
const PR_EXCL = 0x80;

/** Init the file logger with the absolute path to the file.
    It will create and append if the file already exists **/
var MozillaFileLogger = {};

MozillaFileLogger.init = function (path) {
  MozillaFileLogger._file = Cc[LF_CID].createInstance(Ci.nsIFile);
  MozillaFileLogger._file.initWithPath(path);
  MozillaFileLogger._foStream = Cc[FOSTREAM_CID].createInstance(
    Ci.nsIFileOutputStream
  );
  MozillaFileLogger._foStream.init(
    this._file,
    PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND,
    0o664,
    0
  );
};

MozillaFileLogger.getLogCallback = function () {
  return function (msg) {
    var data = msg.num + " " + msg.level + " " + msg.info.join(" ") + "\n";
    if (MozillaFileLogger._foStream) {
      MozillaFileLogger._foStream.write(data, data.length);
    }

    if (data.includes("SimpleTest FINISH")) {
      MozillaFileLogger.close();
    }
  };
};

// This is only used from chrome space by the reftest harness
MozillaFileLogger.log = function (msg) {
  try {
    if (MozillaFileLogger._foStream) {
      MozillaFileLogger._foStream.write(msg, msg.length);
    }
  } catch (ex) {}
};

MozillaFileLogger.close = function () {
  if (MozillaFileLogger._foStream) {
    MozillaFileLogger._foStream.close();
  }

  MozillaFileLogger._foStream = null;
  MozillaFileLogger._file = null;
};

try {
  var filename = Services.prefs.getCharPref("talos.logfile");
  MozillaFileLogger.init(filename);
} catch (ex) {} // pref does not exist, return empty string