1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * @constructor 7 * @extends {WebInspector.Object} 8 */ 9WebInspector.Console = function() 10{ 11 /** @type {!Array.<!WebInspector.Console.Message>} */ 12 this._messages = []; 13} 14 15/** 16 * @enum {string} 17 */ 18WebInspector.Console.Events = { 19 MessageAdded: "messageAdded" 20} 21 22/** 23 * @enum {string} 24 */ 25WebInspector.Console.MessageLevel = { 26 Log: "log", 27 Warning: "warning", 28 Error: "error" 29} 30 31/** 32 * @constructor 33 * @param {string} text 34 * @param {!WebInspector.Console.MessageLevel} level 35 * @param {number} timestamp 36 * @param {boolean} show 37 */ 38WebInspector.Console.Message = function(text, level, timestamp, show) 39{ 40 this.text = text; 41 this.level = level; 42 this.timestamp = (typeof timestamp === "number") ? timestamp : Date.now(); 43 this.show = show; 44} 45 46/** 47 * @interface 48 */ 49WebInspector.Console.UIDelegate = function() 50{ 51} 52 53WebInspector.Console.UIDelegate.prototype = { 54 showConsole: function() { } 55} 56 57WebInspector.Console.prototype = { 58 /** 59 * @param {!WebInspector.Console.UIDelegate} uiDelegate 60 */ 61 setUIDelegate: function(uiDelegate) 62 { 63 this._uiDelegate = uiDelegate; 64 }, 65 66 /** 67 * @param {string} text 68 * @param {!WebInspector.Console.MessageLevel} level 69 * @param {boolean=} show 70 */ 71 addMessage: function(text, level, show) 72 { 73 var message = new WebInspector.Console.Message(text, level || WebInspector.Console.MessageLevel.Log, Date.now(), show || false); 74 this._messages.push(message); 75 this.dispatchEventToListeners(WebInspector.Console.Events.MessageAdded, message); 76 }, 77 78 /** 79 * @param {string} text 80 */ 81 log: function(text) 82 { 83 this.addMessage(text, WebInspector.Console.MessageLevel.Log); 84 }, 85 86 /** 87 * @param {string} text 88 */ 89 warn: function(text) 90 { 91 this.addMessage(text, WebInspector.Console.MessageLevel.Warning); 92 }, 93 94 /** 95 * @param {string} text 96 */ 97 error: function(text) 98 { 99 this.addMessage(text, WebInspector.Console.MessageLevel.Error, true); 100 }, 101 102 /** 103 * @return {!Array.<!WebInspector.Console.Message>} 104 */ 105 messages: function() 106 { 107 return this._messages; 108 }, 109 110 show: function() 111 { 112 if (this._uiDelegate) 113 this._uiDelegate.showConsole(); 114 }, 115 116 __proto__: WebInspector.Object.prototype 117} 118 119WebInspector.console = new WebInspector.Console(); 120