Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * Module for sending log entries to the server. | 7 * Module for sending log entries to the server. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * @constructor | 16 * @constructor |
| 17 */ | 17 */ |
| 18 remoting.LogToServer = function() { | 18 remoting.LogToServer = function() { |
| 19 /** @type Array.<string> */ this.pendingEntries = []; | 19 /** @type Array.<string> */ |
| 20 this.pendingEntries = []; | |
| 21 /** @type {remoting.StatsAccumulator} */ | |
| 22 this.statsAccumulator = new remoting.StatsAccumulator(); | |
| 20 }; | 23 }; |
| 21 | 24 |
| 22 // Local storage keys. | 25 // Local storage keys. |
| 23 /** @private */ | 26 /** @private */ |
| 24 remoting.LogToServer.prototype.KEY_ENABLED_ = 'remoting.LogToServer.enabled'; | 27 remoting.LogToServer.prototype.KEY_ENABLED_ = 'remoting.LogToServer.enabled'; |
| 25 /** @private */ | 28 /** @private */ |
| 26 remoting.LogToServer.prototype.KEY_ID_ = 'remoting.LogToServer.id'; | 29 remoting.LogToServer.prototype.KEY_ID_ = 'remoting.LogToServer.id'; |
| 27 | 30 |
| 28 // Constants used for generating an ID. | 31 // Constants used for generating an ID. |
| 29 /** @private */ | 32 /** @private */ |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 49 */ | 52 */ |
| 50 remoting.LogToServer.prototype.logClientSessionStateChange = | 53 remoting.LogToServer.prototype.logClientSessionStateChange = |
| 51 function(state, connectionError) { | 54 function(state, connectionError) { |
| 52 var entry = remoting.ServerLogEntry.prototype.makeClientSessionStateChange( | 55 var entry = remoting.ServerLogEntry.prototype.makeClientSessionStateChange( |
| 53 state, connectionError); | 56 state, connectionError); |
| 54 entry.addHostFields(); | 57 entry.addHostFields(); |
| 55 entry.addChromeVersionField(); | 58 entry.addChromeVersionField(); |
| 56 entry.addWebappVersionField(); | 59 entry.addWebappVersionField(); |
| 57 entry.addIdField(this.getId()); | 60 entry.addIdField(this.getId()); |
| 58 this.log(entry); | 61 this.log(entry); |
| 62 // Don't accumulate connection statistics across state changes. | |
| 63 this.logAccumulatedStatistics(); | |
| 64 this.statsAccumulator.empty(); | |
| 59 }; | 65 }; |
| 60 | 66 |
| 61 /** | 67 /** |
| 68 * Logs connection statistics. | |
| 69 * @param {Object.<string, number>} stats the connection statistics | |
| 70 */ | |
| 71 remoting.LogToServer.prototype.logStatistics = function(stats) { | |
| 72 // Store the statistics. | |
| 73 this.statsAccumulator.add(stats); | |
| 74 // Send statistics to the server if they've been accumulating for at least | |
| 75 // 60 seconds. | |
| 76 if (this.statsAccumulator.getTimeSinceFirstValue() >= 60 * 1000) { | |
| 77 this.logAccumulatedStatistics(); | |
| 78 } | |
| 79 }; | |
| 80 | |
| 81 /** | |
| 82 * Moves connection statistics from the accumulator to the log server. | |
| 83 * | |
| 84 * If all the statistics are zero, then the accumulator is still emptied, | |
| 85 * but the statistics are not sent to the log server. | |
| 86 * | |
| 87 * @private | |
| 88 */ | |
| 89 remoting.LogToServer.prototype.logAccumulatedStatistics = function() { | |
| 90 var entry = remoting.ServerLogEntry.prototype.makeStats( | |
| 91 this.statsAccumulator); | |
|
Jamie
2011/12/09 19:47:56
Directly invoking a method on the prototype seems
simonmorris
2011/12/09 21:12:12
This was just because I didn't have the convention
| |
| 92 if (entry) { | |
| 93 entry.addHostFields(); | |
| 94 entry.addChromeVersionField(); | |
| 95 entry.addWebappVersionField(); | |
| 96 entry.addIdField(this.getId()); | |
| 97 this.log(entry); | |
| 98 } | |
| 99 this.statsAccumulator.empty(); | |
| 100 }; | |
| 101 | |
| 102 /** | |
| 62 * Sends a log entry to the server. | 103 * Sends a log entry to the server. |
| 63 * | 104 * |
| 64 * @private | 105 * @private |
| 65 * @param {remoting.ServerLogEntry} entry | 106 * @param {remoting.ServerLogEntry} entry |
| 66 */ | 107 */ |
| 67 remoting.LogToServer.prototype.log = function(entry) { | 108 remoting.LogToServer.prototype.log = function(entry) { |
| 68 if (!this.isEnabled()) { | 109 if (!this.isEnabled()) { |
| 69 return; | 110 return; |
| 70 } | 111 } |
| 71 // Store a stanza for the entry | 112 // Send the stanza to the debug log. |
| 113 remoting.debug.logIndent(0, 'Enqueueing log entry:'); | |
| 114 entry.toDebugLog(1); | |
| 115 // Store a stanza for the entry. | |
| 72 this.pendingEntries.push(entry.toStanza()); | 116 this.pendingEntries.push(entry.toStanza()); |
| 73 // Stop if there's no connection to the server. | 117 // Stop if there's no connection to the server. |
| 74 if (!remoting.wcs) { | 118 if (!remoting.wcs) { |
| 75 return; | 119 return; |
| 76 } | 120 } |
| 77 // Send all pending entries to the server. | 121 // Send all pending entries to the server. |
| 122 remoting.debug.log('Sending ' + this.pendingEntries.length + ' log ' + | |
| 123 ((this.pendingEntries.length == 1) ? 'entry' : 'entries') + | |
| 124 ' to the server.'); | |
| 78 var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + | 125 var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + |
| 79 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; | 126 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; |
| 80 while (this.pendingEntries.length > 0) { | 127 while (this.pendingEntries.length > 0) { |
| 81 stanza += /** @type string */ this.pendingEntries.shift(); | 128 stanza += /** @type string */ this.pendingEntries.shift(); |
| 82 } | 129 } |
| 83 stanza += '</gr:log></cli:iq>'; | 130 stanza += '</gr:log></cli:iq>'; |
| 84 remoting.debug.logIq(true, stanza); | |
| 85 remoting.wcs.sendIq(stanza); | 131 remoting.wcs.sendIq(stanza); |
| 86 }; | 132 }; |
| 87 | 133 |
| 88 /** | 134 /** |
| 89 * Whether logging is enabled. | 135 * Whether logging is enabled. |
| 90 * | 136 * |
| 91 * @private | 137 * @private |
| 92 * @return {boolean} whether logging is enabled | 138 * @return {boolean} whether logging is enabled |
| 93 */ | 139 */ |
| 94 remoting.LogToServer.prototype.isEnabled = function() { | 140 remoting.LogToServer.prototype.isEnabled = function() { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 125 * @return {string} an ID | 171 * @return {string} an ID |
| 126 */ | 172 */ |
| 127 remoting.LogToServer.prototype.generateId = function() { | 173 remoting.LogToServer.prototype.generateId = function() { |
| 128 var idArray = []; | 174 var idArray = []; |
| 129 for (var i = 0; i < this.ID_LEN_; i++) { | 175 for (var i = 0; i < this.ID_LEN_; i++) { |
| 130 var index = Math.random() * this.ID_ALPHABET_.length; | 176 var index = Math.random() * this.ID_ALPHABET_.length; |
| 131 idArray.push(this.ID_ALPHABET_.slice(index, index + 1)); | 177 idArray.push(this.ID_ALPHABET_.slice(index, index + 1)); |
| 132 } | 178 } |
| 133 return idArray.join(''); | 179 return idArray.join(''); |
| 134 }; | 180 }; |
| 181 | |
| 182 /** | |
| 183 * Sends a message describing a log entry to the debug log. | |
| 184 * | |
| 185 * @private | |
| 186 * @param {remoting.ServerLogEntry} entry | |
| 187 */ | |
| 188 remoting.LogToServer.prototype.logToConsole = function(entry) { | |
| 189 remoting.debug.logIndent(0, 'Queueing log entry:'); | |
| 190 entry.toDebugLog(1) | |
| 191 }; | |
|
Jamie
2011/12/09 19:47:56
This function looks like it's unused.
simonmorris
2011/12/09 21:12:12
Done.
| |
| OLD | NEW |