| 1 | // Copyright 2013 Selenium comitters |
| 2 | // Copyright 2013 Software Freedom Conservancy |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | goog.provide('webdriver.logging'); |
| 17 | goog.provide('webdriver.logging.Preferences'); |
| 18 | |
| 19 | goog.require('goog.object'); |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Log level names from WebDriver's JSON wire protocol. |
| 24 | * @enum {string} |
| 25 | */ |
| 26 | webdriver.logging.LevelName = { |
| 27 | ALL: 'ALL', |
| 28 | DEBUG: 'DEBUG', |
| 29 | INFO: 'INFO', |
| 30 | WARNING: 'WARNING', |
| 31 | SEVERE: 'SEVERE', |
| 32 | OFF: 'OFF' |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Logging levels. |
| 38 | * @enum {{value: number, name: webdriver.logging.LevelName}} |
| 39 | */ |
| 40 | webdriver.logging.Level = { |
| 41 | ALL: {value: Number.MIN_VALUE, name: webdriver.logging.LevelName.ALL}, |
| 42 | DEBUG: {value: 700, name: webdriver.logging.LevelName.DEBUG}, |
| 43 | INFO: {value: 800, name: webdriver.logging.LevelName.INFO}, |
| 44 | WARNING: {value: 900, name: webdriver.logging.LevelName.WARNING}, |
| 45 | SEVERE: {value: 1000, name: webdriver.logging.LevelName.SEVERE}, |
| 46 | OFF: {value: Number.MAX_VALUE, name: webdriver.logging.LevelName.OFF} |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Converts a level name or value to a {@link webdriver.logging.Level} value. |
| 52 | * If the name/value is not recognized, {@link webdriver.logging.Level.ALL} |
| 53 | * will be returned. |
| 54 | * @param {(number|string)} nameOrValue The log level name, or value, to |
| 55 | * convert . |
| 56 | * @return {!webdriver.logging.Level} The converted level. |
| 57 | */ |
| 58 | webdriver.logging.getLevel = function(nameOrValue) { |
| 59 | var predicate = goog.isString(nameOrValue) ? |
| 60 | function(val) { return val.name === nameOrValue; } : |
| 61 | function(val) { return val.value === nameOrValue; }; |
| 62 | |
| 63 | return goog.object.findValue(webdriver.logging.Level, predicate) || |
| 64 | webdriver.logging.Level.ALL; |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Common log types. |
| 70 | * @enum {string} |
| 71 | */ |
| 72 | webdriver.logging.Type = { |
| 73 | /** Logs originating from the browser. */ |
| 74 | BROWSER: 'browser', |
| 75 | /** Logs from a WebDriver client. */ |
| 76 | CLIENT: 'client', |
| 77 | /** Logs from a WebDriver implementation. */ |
| 78 | DRIVER: 'driver', |
| 79 | /** Logs related to performance. */ |
| 80 | PERFORMANCE: 'performance', |
| 81 | /** Logs from the remote server. */ |
| 82 | SERVER: 'server' |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * A hash describing log preferences. |
| 88 | * @typedef {Object.<webdriver.logging.Type, webdriver.logging.LevelName>} |
| 89 | */ |
| 90 | webdriver.logging.Preferences; |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * A single log entry. |
| 95 | * @param {(!webdriver.logging.Level|string)} level The entry level. |
| 96 | * @param {string} message The log message. |
| 97 | * @param {number=} opt_timestamp The time this entry was generated, in |
| 98 | * milliseconds since 0:00:00, January 1, 1970 UTC. If omitted, the |
| 99 | * current time will be used. |
| 100 | * @param {string=} opt_type The log type, if known. |
| 101 | * @constructor |
| 102 | */ |
| 103 | webdriver.logging.Entry = function(level, message, opt_timestamp, opt_type) { |
| 104 | |
| 105 | /** @type {!webdriver.logging.Level} */ |
| 106 | this.level = |
| 107 | goog.isString(level) ? webdriver.logging.getLevel(level) : level; |
| 108 | |
| 109 | /** @type {string} */ |
| 110 | this.message = message; |
| 111 | |
| 112 | /** @type {number} */ |
| 113 | this.timestamp = goog.isNumber(opt_timestamp) ? opt_timestamp : goog.now(); |
| 114 | |
| 115 | /** @type {string} */ |
| 116 | this.type = opt_type || ''; |
| 117 | }; |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * @return {{level: string, message: string, timestamp: number, |
| 122 | * type: string}} The JSON representation of this entry. |
| 123 | */ |
| 124 | webdriver.logging.Entry.prototype.toJSON = function() { |
| 125 | return { |
| 126 | 'level': this.level.name, |
| 127 | 'message': this.message, |
| 128 | 'timestamp': this.timestamp, |
| 129 | 'type': this.type |
| 130 | }; |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Converts a {@link goog.debug.LogRecord} into a |
| 136 | * {@link webdriver.logging.Entry}. |
| 137 | * @param {!goog.debug.LogRecord} logRecord The record to convert. |
| 138 | * @param {string=} opt_type The log type. |
| 139 | * @return {!webdriver.logging.Entry} The converted entry. |
| 140 | */ |
| 141 | webdriver.logging.Entry.fromClosureLogRecord = function(logRecord, opt_type) { |
| 142 | var closureLevel = logRecord.getLevel(); |
| 143 | var level = webdriver.logging.Level.SEVERE; |
| 144 | |
| 145 | if (closureLevel.value <= webdriver.logging.Level.DEBUG.value) { |
| 146 | level = webdriver.logging.Level.DEBUG; |
| 147 | } else if (closureLevel.value <= webdriver.logging.Level.INFO.value) { |
| 148 | level = webdriver.logging.Level.INFO; |
| 149 | } else if (closureLevel.value <= webdriver.logging.Level.WARNING.value) { |
| 150 | level = webdriver.logging.Level.WARNING; |
| 151 | } |
| 152 | |
| 153 | return new webdriver.logging.Entry( |
| 154 | level, |
| 155 | '[' + logRecord.getLoggerName() + '] ' + logRecord.getMessage(), |
| 156 | logRecord.getMillis(), |
| 157 | opt_type); |
| 158 | }; |