|
1 /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ |
|
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * The contents of this file were copied almost entirely from |
|
9 * toolkit/identity/LogUtils.jsm. Until we've got a more generalized logging |
|
10 * mechanism for toolkit, I think this is going to be how we roll. |
|
11 */ |
|
12 |
|
13 "use strict"; |
|
14 |
|
15 this.EXPORTED_SYMBOLS = ["DownloadsLogger"]; |
|
16 const PREF_DEBUG = "browser.download.debug"; |
|
17 |
|
18 const Cu = Components.utils; |
|
19 const Ci = Components.interfaces; |
|
20 const Cc = Components.classes; |
|
21 const Cr = Components.results; |
|
22 |
|
23 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
24 Cu.import("resource://gre/modules/Services.jsm"); |
|
25 |
|
26 this.DownloadsLogger = { |
|
27 _generateLogMessage: function _generateLogMessage(args) { |
|
28 // create a string representation of a list of arbitrary things |
|
29 let strings = []; |
|
30 |
|
31 for (let arg of args) { |
|
32 if (typeof arg === 'string') { |
|
33 strings.push(arg); |
|
34 } else if (arg === undefined) { |
|
35 strings.push('undefined'); |
|
36 } else if (arg === null) { |
|
37 strings.push('null'); |
|
38 } else { |
|
39 try { |
|
40 strings.push(JSON.stringify(arg, null, 2)); |
|
41 } catch(err) { |
|
42 strings.push("<<something>>"); |
|
43 } |
|
44 } |
|
45 }; |
|
46 return 'Downloads: ' + strings.join(' '); |
|
47 }, |
|
48 |
|
49 /** |
|
50 * log() - utility function to print a list of arbitrary things |
|
51 * |
|
52 * Enable with about:config pref browser.download.debug |
|
53 */ |
|
54 log: function DL_log(...args) { |
|
55 let output = this._generateLogMessage(args); |
|
56 dump(output + "\n"); |
|
57 |
|
58 // Additionally, make the output visible in the Error Console |
|
59 Services.console.logStringMessage(output); |
|
60 }, |
|
61 |
|
62 /** |
|
63 * reportError() - report an error through component utils as well as |
|
64 * our log function |
|
65 */ |
|
66 reportError: function DL_reportError(...aArgs) { |
|
67 // Report the error in the browser |
|
68 let output = this._generateLogMessage(aArgs); |
|
69 Cu.reportError(output); |
|
70 dump("ERROR:" + output + "\n"); |
|
71 for (let frame = Components.stack.caller; frame; frame = frame.caller) { |
|
72 dump("\t" + frame + "\n"); |
|
73 } |
|
74 } |
|
75 |
|
76 }; |