/**1* Copyright (c) 2014, Facebook, Inc. All rights reserved.2*3* This source code is licensed under the BSD-style license found in the4* LICENSE file in the root directory of this source tree. An additional grant5* of patent rights can be found in the PATENTS file in the same directory.6*/78// Copyright Joyent, Inc. and other Node contributors.9//10// Permission is hereby granted, free of charge, to any person obtaining a11// copy of this software and associated documentation files (the12// "Software"), to deal in the Software without restriction, including13// without limitation the rights to use, copy, modify, merge, publish,14// distribute, sublicense, and/or sell copies of the Software, and to permit15// persons to whom the Software is furnished to do so, subject to the16// following conditions:17//18// The above copyright notice and this permission notice shall be included19// in all copies or substantial portions of the Software.20//21// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS22// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF23// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN24// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,25// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR26// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE27// USE OR OTHER DEALINGS IN THE SOFTWARE.2829/*jshint strict:false*/3031var util = require('util');3233function Console(messageQueue) {34if (!(this instanceof Console)) {35return new Console(messageQueue);36}3738Object.defineProperty(this, '_messageQueue', {39value: messageQueue,40writable: true,41enumerable: false,42configurable: true43});4445Object.defineProperty(this, '_times', {46value: {},47writable: true,48enumerable: false,49configurable: true50});5152// bind the prototype functions to this Console instance53var keys = Object.keys(Console.prototype);54for (var v = 0; v < keys.length; v++) {55var k = keys[v];56this[k] = this[k].bind(this);57}58}5960Console.prototype.log = function() {61this._messageQueue.push({62type: 'log',63data: util.format.apply(this, arguments) + '\n'64});65};666768Console.prototype.info = Console.prototype.log;697071Console.prototype.warn = function() {72this._messageQueue.push({73type: 'warn',74data: util.format.apply(this, arguments) + '\n'75});76};777879Console.prototype.error = function() {80this._messageQueue.push({81type: 'error',82data: util.format.apply(this, arguments) + '\n'83});84};858687Console.prototype.dir = function(object, options) {88this._messageQueue.push({89type: 'dir',90data: util.inspect(object, util._extend({91customInspect: false92}, options)) + '\n'93});94};959697Console.prototype.time = function(label) {98this._times[label] = Date.now();99};100101102Console.prototype.timeEnd = function(label) {103var time = this._times[label];104if (!time) {105throw new Error('No such label: ' + label);106}107var duration = Date.now() - time;108this.log('%s: %dms', label, duration);109};110111112Console.prototype.trace = function() {113// TODO probably can to do this better with V8's debug object once that is114// exposed.115var err = new Error();116err.name = 'Trace';117err.message = util.format.apply(this, arguments);118/*jshint noarg:false*/119Error.captureStackTrace(err, arguments.callee);120this.error(err.stack);121};122123124Console.prototype.assert = function(expression) {125if (!expression) {126var arr = Array.prototype.slice.call(arguments, 1);127require('assert').ok(false, util.format.apply(this, arr));128}129};130131module.exports = Console;132133134