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