summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/log.jsm
blob: 5c2829017cda0f72371aebc2f806ad276b752535 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * 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 https://mozilla.org/MPL/2.0/.
 */
"use strict";

const EXPORTED_SYMBOLS = ["EnigmailLog"];

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

var EnigmailLog = {
  level: 3,
  directory: null,
  fileStream: null,

  setLogLevel(newLogLevel) {
    EnigmailLog.level = newLogLevel;
  },

  getLogLevel() {
    return EnigmailLog.level;
  },

  setLogDirectory(newLogDirectory) {
    EnigmailLog.directory =
      newLogDirectory + (AppConstants.platform == "win" ? "\\" : "/");
    EnigmailLog.createLogFiles();
  },

  createLogFiles() {
    if (
      EnigmailLog.directory &&
      !EnigmailLog.fileStream &&
      EnigmailLog.level >= 5
    ) {
      let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
      file.initWithPath(EnigmailLog.directory + "enigdbug.txt");
      let ofStream = Cc[
        "@mozilla.org/network/file-output-stream;1"
      ].createInstance(Ci.nsIFileOutputStream);
      ofStream.init(file, -1, -1, 0);

      EnigmailLog.fileStream = ofStream;
    }
  },

  onShutdown() {
    if (EnigmailLog.fileStream) {
      EnigmailLog.fileStream.close();
    }
    EnigmailLog.fileStream = null;
  },

  WRITE(str) {
    function withZeroes(val, digits) {
      return ("0000" + val.toString()).substr(-digits);
    }

    var d = new Date();
    var datStr =
      d.getFullYear() +
      "-" +
      withZeroes(d.getMonth() + 1, 2) +
      "-" +
      withZeroes(d.getDate(), 2) +
      " " +
      withZeroes(d.getHours(), 2) +
      ":" +
      withZeroes(d.getMinutes(), 2) +
      ":" +
      withZeroes(d.getSeconds(), 2) +
      "." +
      withZeroes(d.getMilliseconds(), 3) +
      " ";
    if (EnigmailLog.level >= 4) {
      dump(datStr + str);
    }

    if (EnigmailLog.fileStream) {
      EnigmailLog.fileStream.write(datStr, datStr.length);
      EnigmailLog.fileStream.write(str, str.length);
    }
  },

  DEBUG(str) {
    try {
      EnigmailLog.WRITE("[DEBUG] " + str);
    } catch (ex) {}
  },

  WARNING(str) {
    EnigmailLog.WRITE("[WARN] " + str);
  },

  ERROR(str) {
    try {
      var consoleSvc = Services.console;
      var scriptError = Cc["@mozilla.org/scripterror;1"].createInstance(
        Ci.nsIScriptError
      );
      scriptError.init(
        str,
        null,
        null,
        0,
        0,
        scriptError.errorFlag,
        "Enigmail"
      );
      consoleSvc.logMessage(scriptError);
    } catch (ex) {}

    EnigmailLog.WRITE("[ERROR] " + str);
  },

  CONSOLE(str) {
    if (EnigmailLog.level >= 3) {
      EnigmailLog.WRITE("[CONSOLE] " + str);
    }
  },

  /**
   *  Log an exception including the stack trace
   *
   *  referenceInfo: String - arbitrary text to write before the exception is logged
   *  ex:            exception object
   */
  writeException(referenceInfo, ex) {
    EnigmailLog.ERROR(
      referenceInfo +
        ": caught exception: " +
        ex.name +
        "\n" +
        "Message: '" +
        ex.message +
        "'\n" +
        "File:    " +
        ex.fileName +
        "\n" +
        "Line:    " +
        ex.lineNumber +
        "\n" +
        "Stack:   " +
        ex.stack +
        "\n"
    );
  },
};