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 12 matching lines...) Expand all Loading... |
42 } | 45 } |
43 | 46 |
44 /** | 47 /** |
45 * Logs a client session state change. | 48 * Logs a client session state change. |
46 * | 49 * |
47 * @param {remoting.ClientSession.State} state | 50 * @param {remoting.ClientSession.State} state |
48 * @param {remoting.ClientSession.ConnectionError} connectionError | 51 * @param {remoting.ClientSession.ConnectionError} connectionError |
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.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.makeStats(this.statsAccumulator); |
| 91 if (entry) { |
| 92 entry.addHostFields(); |
| 93 entry.addChromeVersionField(); |
| 94 entry.addWebappVersionField(); |
| 95 entry.addIdField(this.getId()); |
| 96 this.log(entry); |
| 97 } |
| 98 this.statsAccumulator.empty(); |
| 99 }; |
| 100 |
| 101 /** |
62 * Sends a log entry to the server. | 102 * Sends a log entry to the server. |
63 * | 103 * |
64 * @private | 104 * @private |
65 * @param {remoting.ServerLogEntry} entry | 105 * @param {remoting.ServerLogEntry} entry |
66 */ | 106 */ |
67 remoting.LogToServer.prototype.log = function(entry) { | 107 remoting.LogToServer.prototype.log = function(entry) { |
68 if (!this.isEnabled()) { | 108 if (!this.isEnabled()) { |
69 return; | 109 return; |
70 } | 110 } |
71 // Store a stanza for the entry | 111 // Send the stanza to the debug log. |
| 112 remoting.debug.log('Enqueueing log entry:'); |
| 113 entry.toDebugLog(1); |
| 114 // Store a stanza for the entry. |
72 this.pendingEntries.push(entry.toStanza()); | 115 this.pendingEntries.push(entry.toStanza()); |
73 // Stop if there's no connection to the server. | 116 // Stop if there's no connection to the server. |
74 if (!remoting.wcs) { | 117 if (!remoting.wcs) { |
75 return; | 118 return; |
76 } | 119 } |
77 // Send all pending entries to the server. | 120 // Send all pending entries to the server. |
| 121 remoting.debug.log('Sending ' + this.pendingEntries.length + ' log ' + |
| 122 ((this.pendingEntries.length == 1) ? 'entry' : 'entries') + |
| 123 ' to the server.'); |
78 var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + | 124 var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + |
79 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; | 125 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; |
80 while (this.pendingEntries.length > 0) { | 126 while (this.pendingEntries.length > 0) { |
81 stanza += /** @type string */ this.pendingEntries.shift(); | 127 stanza += /** @type string */ this.pendingEntries.shift(); |
82 } | 128 } |
83 stanza += '</gr:log></cli:iq>'; | 129 stanza += '</gr:log></cli:iq>'; |
84 remoting.debug.logIq(true, stanza); | |
85 remoting.wcs.sendIq(stanza); | 130 remoting.wcs.sendIq(stanza); |
86 }; | 131 }; |
87 | 132 |
88 /** | 133 /** |
89 * Whether logging is enabled. | 134 * Whether logging is enabled. |
90 * | 135 * |
91 * @private | 136 * @private |
92 * @return {boolean} whether logging is enabled | 137 * @return {boolean} whether logging is enabled |
93 */ | 138 */ |
94 remoting.LogToServer.prototype.isEnabled = function() { | 139 remoting.LogToServer.prototype.isEnabled = function() { |
(...skipping 30 matching lines...) Expand all Loading... |
125 * @return {string} an ID | 170 * @return {string} an ID |
126 */ | 171 */ |
127 remoting.LogToServer.prototype.generateId = function() { | 172 remoting.LogToServer.prototype.generateId = function() { |
128 var idArray = []; | 173 var idArray = []; |
129 for (var i = 0; i < this.ID_LEN_; i++) { | 174 for (var i = 0; i < this.ID_LEN_; i++) { |
130 var index = Math.random() * this.ID_ALPHABET_.length; | 175 var index = Math.random() * this.ID_ALPHABET_.length; |
131 idArray.push(this.ID_ALPHABET_.slice(index, index + 1)); | 176 idArray.push(this.ID_ALPHABET_.slice(index, index + 1)); |
132 } | 177 } |
133 return idArray.join(''); | 178 return idArray.join(''); |
134 }; | 179 }; |
OLD | NEW |