# lib/webdriver/logging.js

    1// Copyright 2013 Selenium comitters2// Copyright 2013 Software Freedom Conservancy3//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 at7//8//      http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// 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 and14// limitations under the License.1516goog.provide('webdriver.logging');17goog.provide('webdriver.logging.Preferences');1819goog.require('goog.object');202122/**23 * Log level names from WebDriver's JSON wire protocol.24 * @enum {string}25 */26webdriver.logging.LevelName = {27  ALL: 'ALL',28  DEBUG: 'DEBUG',29  INFO: 'INFO',30  WARNING: 'WARNING',31  SEVERE: 'SEVERE',32  OFF: 'OFF'33};343536/**37 * Logging levels.38 * @enum {{value: number, name: webdriver.logging.LevelName}}39 */40webdriver.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};484950/**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, to55 *     convert .56 * @return {!webdriver.logging.Level} The converted level.57 */58webdriver.logging.getLevel = function(nameOrValue) {59  var predicate = goog.isString(nameOrValue) ?60      function(val) { return val.name === nameOrValue; } :61      function(val) { return val.value === nameOrValue; };6263  return goog.object.findValue(webdriver.logging.Level, predicate) ||64      webdriver.logging.Level.ALL;65};666768/**69 * Common log types.70 * @enum {string}71 */72webdriver.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};848586/**87 * A hash describing log preferences.88 * @typedef {Object.<webdriver.logging.Type, webdriver.logging.LevelName>}89 */90webdriver.logging.Preferences;919293/**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, in98 *     milliseconds since 0:00:00, January 1, 1970 UTC. If omitted, the99 *     current time will be used.100 * @param {string=} opt_type The log type, if known.101 * @constructor102 */103webdriver.logging.Entry = function(level, message, opt_timestamp, opt_type) {104105  /** @type {!webdriver.logging.Level} */106  this.level =107      goog.isString(level) ? webdriver.logging.getLevel(level) : level;108109  /** @type {string} */110  this.message = message;111112  /** @type {number} */113  this.timestamp = goog.isNumber(opt_timestamp) ? opt_timestamp : goog.now();114115  /** @type {string} */116  this.type = opt_type || '';117};118119120/**121 * @return {{level: string, message: string, timestamp: number,122 *           type: string}} The JSON representation of this entry.123 */124webdriver.logging.Entry.prototype.toJSON = function() {125  return {126    'level': this.level.name,127    'message': this.message,128    'timestamp': this.timestamp,129    'type': this.type130  };131};132133134/**135 * Converts a {@link goog.debug.LogRecord} into a136 * {@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 */141webdriver.logging.Entry.fromClosureLogRecord = function(logRecord, opt_type) {142  var closureLevel = logRecord.getLevel();143  var level = webdriver.logging.Level.SEVERE;144145  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  }152153  return new webdriver.logging.Entry(154      level,155      '[' + logRecord.getLoggerName() + '] ' + logRecord.getMessage(),156      logRecord.getMillis(),157      opt_type);158};