|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /** |
|
6 * |
|
7 * This file contains common code that is shared between marionette-server.js |
|
8 * and marionette-listener.js. |
|
9 * |
|
10 */ |
|
11 |
|
12 /** |
|
13 * Creates an error message for a JavaScript exception thrown during |
|
14 * execute_(async_)script. |
|
15 * |
|
16 * This will generate a [msg, trace] pair like: |
|
17 * |
|
18 * ['ReferenceError: foo is not defined', |
|
19 * 'execute_script @test_foo.py, line 10 |
|
20 * inline javascript, line 2 |
|
21 * src: "return foo;"'] |
|
22 * |
|
23 * @param error An Error object passed to a catch() clause. |
|
24 fnName The name of the function to use in the stack trace message |
|
25 (e.g., 'execute_script'). |
|
26 pythonFile The filename of the test file containing the Marionette |
|
27 command that caused this exception to occur. |
|
28 pythonLine The line number of the above test file. |
|
29 script The JS script being executed in text form. |
|
30 */ |
|
31 this.createStackMessage = function createStackMessage(error, fnName, pythonFile, |
|
32 pythonLine, script) { |
|
33 let python_stack = fnName + " @" + pythonFile; |
|
34 if (pythonLine !== null) { |
|
35 python_stack += ", line " + pythonLine; |
|
36 } |
|
37 let trace, msg; |
|
38 if (typeof(error) == "object" && 'name' in error && 'stack' in error) { |
|
39 let stack = error.stack.split("\n"); |
|
40 let match = stack[0].match(/:(\d+):\d+$/); |
|
41 let line = match ? parseInt(match[1]) : 0; |
|
42 msg = error.name + ('message' in error ? ": " + error.message : ""); |
|
43 trace = python_stack + |
|
44 "\ninline javascript, line " + line + |
|
45 "\nsrc: \"" + script.split("\n")[line] + "\""; |
|
46 } |
|
47 else { |
|
48 trace = python_stack; |
|
49 msg = error + ""; |
|
50 } |
|
51 return [msg, trace]; |
|
52 } |
|
53 |
|
54 this.MarionetteLogObj = function MarionetteLogObj() { |
|
55 this.logs = []; |
|
56 } |
|
57 MarionetteLogObj.prototype = { |
|
58 /** |
|
59 * Log message. Accepts user defined log-level. |
|
60 * @param msg String |
|
61 * The message to be logged |
|
62 * @param level String |
|
63 * The logging level to be used |
|
64 */ |
|
65 log: function ML_log(msg, level) { |
|
66 let lev = level ? level : "INFO"; |
|
67 this.logs.push( [lev, msg, (new Date()).toString()]); |
|
68 }, |
|
69 |
|
70 /** |
|
71 * Add a list of logs to its list |
|
72 * @param msgs Object |
|
73 * Takes a list of strings |
|
74 */ |
|
75 addLogs: function ML_addLogs(msgs) { |
|
76 for (let i = 0; i < msgs.length; i++) { |
|
77 this.logs.push(msgs[i]); |
|
78 } |
|
79 }, |
|
80 |
|
81 /** |
|
82 * Return all logged messages. |
|
83 */ |
|
84 getLogs: function ML_getLogs() { |
|
85 let logs = this.logs; |
|
86 this.clearLogs(); |
|
87 return logs; |
|
88 }, |
|
89 |
|
90 /** |
|
91 * Clears the logs |
|
92 */ |
|
93 clearLogs: function ML_clearLogs() { |
|
94 this.logs = []; |
|
95 }, |
|
96 } |