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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// NOTE: The sync test_errorhandler_* tests have quite good coverage for
// other aspects of this.
const { LogManager } = ChromeUtils.importESModule(
"resource://services-common/logmanager.sys.mjs"
);
const { FileUtils } = ChromeUtils.importESModule(
"resource://gre/modules/FileUtils.sys.mjs"
);
// Returns an array of [consoleAppender, dumpAppender, [fileAppenders]] for
// the specified log. Note that fileAppenders will usually have length=1
function getAppenders(log) {
let capps = log.appenders.filter(app => app instanceof Log.ConsoleAppender);
equal(capps.length, 1, "should only have one console appender");
let dapps = log.appenders.filter(app => app instanceof Log.DumpAppender);
equal(dapps.length, 1, "should only have one dump appender");
let fapps = log.appenders.filter(
app => app instanceof LogManager.StorageStreamAppender
);
return [capps[0], dapps[0], fapps];
}
// Test that the correct thing happens when no prefs exist for the log manager.
add_task(async function test_noPrefs() {
// tell the log manager to init with a pref branch that doesn't exist.
let lm = new LogManager("no-such-branch.", ["TestLog"], "test");
let log = Log.repository.getLogger("TestLog");
let [capp, dapp, fapps] = getAppenders(log);
// The console appender gets "Fatal" while the "dump" appender gets "Error" levels
equal(capp.level, Log.Level.Fatal);
equal(dapp.level, Log.Level.Error);
// and the file (stream) appender gets Debug by default
equal(fapps.length, 1, "only 1 file appender");
equal(fapps[0].level, Log.Level.Debug);
lm.finalize();
});
// Test that changes to the prefs used by the log manager are updated dynamically.
add_task(async function test_PrefChanges() {
Services.prefs.setStringPref(
"log-manager.test.log.appender.console",
"Trace"
);
Services.prefs.setStringPref("log-manager.test.log.appender.dump", "Trace");
Services.prefs.setStringPref(
"log-manager.test.log.appender.file.level",
"Trace"
);
let lm = new LogManager("log-manager.test.", ["TestLog2"], "test");
let log = Log.repository.getLogger("TestLog2");
let [capp, dapp, [fapp]] = getAppenders(log);
equal(capp.level, Log.Level.Trace);
equal(dapp.level, Log.Level.Trace);
equal(fapp.level, Log.Level.Trace);
// adjust the prefs and they should magically be reflected in the appenders.
Services.prefs.setStringPref(
"log-manager.test.log.appender.console",
"Debug"
);
Services.prefs.setStringPref("log-manager.test.log.appender.dump", "Debug");
Services.prefs.setStringPref(
"log-manager.test.log.appender.file.level",
"Debug"
);
equal(capp.level, Log.Level.Debug);
equal(dapp.level, Log.Level.Debug);
equal(fapp.level, Log.Level.Debug);
// and invalid values should cause them to fallback to their defaults.
Services.prefs.setStringPref("log-manager.test.log.appender.console", "xxx");
Services.prefs.setStringPref("log-manager.test.log.appender.dump", "xxx");
Services.prefs.setStringPref(
"log-manager.test.log.appender.file.level",
"xxx"
);
equal(capp.level, Log.Level.Fatal);
equal(dapp.level, Log.Level.Error);
equal(fapp.level, Log.Level.Debug);
lm.finalize();
});
// Test that the same log used by multiple log managers does the right thing.
add_task(async function test_SharedLogs() {
// create the prefs for the first instance.
Services.prefs.setStringPref(
"log-manager-1.test.log.appender.console",
"Trace"
);
Services.prefs.setStringPref("log-manager-1.test.log.appender.dump", "Trace");
Services.prefs.setStringPref(
"log-manager-1.test.log.appender.file.level",
"Trace"
);
let lm1 = new LogManager("log-manager-1.test.", ["TestLog3"], "test");
// and the second.
Services.prefs.setStringPref(
"log-manager-2.test.log.appender.console",
"Debug"
);
Services.prefs.setStringPref("log-manager-2.test.log.appender.dump", "Debug");
Services.prefs.setStringPref(
"log-manager-2.test.log.appender.file.level",
"Debug"
);
let lm2 = new LogManager("log-manager-2.test.", ["TestLog3"], "test");
let log = Log.repository.getLogger("TestLog3");
let [capp, dapp] = getAppenders(log);
// console and dump appenders should be "trace" as it is more verbose than
// "debug"
equal(capp.level, Log.Level.Trace);
equal(dapp.level, Log.Level.Trace);
// Set the prefs on the -1 branch to "Error" - it should then end up with
// "Debug" from the -2 branch.
Services.prefs.setStringPref(
"log-manager-1.test.log.appender.console",
"Error"
);
Services.prefs.setStringPref("log-manager-1.test.log.appender.dump", "Error");
Services.prefs.setStringPref(
"log-manager-1.test.log.appender.file.level",
"Error"
);
equal(capp.level, Log.Level.Debug);
equal(dapp.level, Log.Level.Debug);
lm1.finalize();
lm2.finalize();
});
// A little helper to test what log files exist. We expect exactly zero (if
// prefix is null) or exactly one with the specified prefix.
function checkLogFile(prefix) {
let logsdir = FileUtils.getDir("ProfD", ["weave", "logs"]);
let entries = logsdir.directoryEntries;
if (!prefix) {
// expecting no files.
ok(!entries.hasMoreElements());
} else {
// expecting 1 file.
ok(entries.hasMoreElements());
let logfile = entries.getNext().QueryInterface(Ci.nsIFile);
equal(logfile.leafName.slice(-4), ".txt");
ok(logfile.leafName.startsWith(prefix + "-test-"), logfile.leafName);
// and remove it ready for the next check.
logfile.remove(false);
}
}
// Test that we correctly write error logs by default
add_task(async function test_logFileErrorDefault() {
let lm = new LogManager("log-manager.test.", ["TestLog2"], "test");
let log = Log.repository.getLogger("TestLog2");
log.error("an error message");
await lm.resetFileLog(lm.REASON_ERROR);
// One error log file exists.
checkLogFile("error");
lm.finalize();
});
// Test that we correctly write success logs.
add_task(async function test_logFileSuccess() {
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnError",
false
);
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnSuccess",
false
);
let lm = new LogManager("log-manager.test.", ["TestLog2"], "test");
let log = Log.repository.getLogger("TestLog2");
log.info("an info message");
await lm.resetFileLog();
// Zero log files exist.
checkLogFile(null);
// Reset logOnSuccess and do it again - log should appear.
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnSuccess",
true
);
log.info("an info message");
await lm.resetFileLog();
checkLogFile("success");
// Now test with no "reason" specified and no "error" record.
log.info("an info message");
await lm.resetFileLog();
// should get a "success" entry.
checkLogFile("success");
// With no "reason" and an error record - should get no success log.
log.error("an error message");
await lm.resetFileLog();
// should get no entry
checkLogFile(null);
// And finally now with no error, to ensure that the fact we had an error
// previously doesn't persist after the .resetFileLog call.
log.info("an info message");
await lm.resetFileLog();
checkLogFile("success");
lm.finalize();
});
// Test that we correctly write error logs.
add_task(async function test_logFileError() {
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnError",
false
);
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnSuccess",
false
);
let lm = new LogManager("log-manager.test.", ["TestLog2"], "test");
let log = Log.repository.getLogger("TestLog2");
log.info("an info message");
let reason = await lm.resetFileLog();
Assert.equal(reason, null, "null returned when no file created.");
// Zero log files exist.
checkLogFile(null);
// Reset logOnSuccess - success logs should appear if no error records.
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnSuccess",
true
);
log.info("an info message");
reason = await lm.resetFileLog();
Assert.equal(reason, lm.SUCCESS_LOG_WRITTEN);
checkLogFile("success");
// Set logOnError and unset logOnSuccess - error logs should appear.
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnSuccess",
false
);
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnError",
true
);
log.error("an error message");
reason = await lm.resetFileLog();
Assert.equal(reason, lm.ERROR_LOG_WRITTEN);
checkLogFile("error");
// Now test with no "error" record.
log.info("an info message");
reason = await lm.resetFileLog();
// should get no file
Assert.equal(reason, null);
checkLogFile(null);
// With an error record we should get an error log.
log.error("an error message");
reason = await lm.resetFileLog();
// should get en error log
Assert.equal(reason, lm.ERROR_LOG_WRITTEN);
checkLogFile("error");
// And finally now with success, to ensure that the fact we had an error
// previously doesn't persist after the .resetFileLog call.
log.info("an info message");
await lm.resetFileLog();
checkLogFile(null);
lm.finalize();
});
function countLogFiles() {
let logsdir = FileUtils.getDir("ProfD", ["weave", "logs"]);
let count = 0;
for (let entry of logsdir.directoryEntries) {
void entry;
count += 1;
}
return count;
}
// Test that removeAllLogs removes all log files.
add_task(async function test_logFileError() {
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnError",
true
);
Services.prefs.setBoolPref(
"log-manager.test.log.appender.file.logOnSuccess",
true
);
let lm = new LogManager("log-manager.test.", ["TestLog2"], "test");
let log = Log.repository.getLogger("TestLog2");
log.info("an info message");
let reason = await lm.resetFileLog();
Assert.equal(reason, lm.SUCCESS_LOG_WRITTEN, "success log was written.");
log.error("an error message");
reason = await lm.resetFileLog();
Assert.equal(reason, lm.ERROR_LOG_WRITTEN);
Assert.equal(countLogFiles(), 2, "expect 2 log files");
await lm.removeAllLogs();
Assert.equal(
countLogFiles(),
0,
"should be no log files after removing them"
);
lm.finalize();
});
|