Skip to content

Commit ad521bf

Browse files
authored
Merge pull request #1621 from matrix-org/jryans/megolm-logs-2021-02-26
Add logging to in progress Olm sessions
2 parents bfad21f + 8152fa4 commit ad521bf

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

src/crypto/OlmDevice.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
1818

19-
import {getPrefixedLogger, logger} from '../logger';
19+
import {logger} from '../logger';
2020
import {IndexedDBCryptoStore} from './store/indexeddb-crypto-store';
2121
import * as algorithms from './algorithms';
2222

@@ -545,7 +545,7 @@ OlmDevice.prototype.createOutboundSession = async function(
545545
}
546546
});
547547
},
548-
getPrefixedLogger("[createOutboundSession]"),
548+
logger.withPrefix("[createOutboundSession]"),
549549
);
550550
return newSessionId;
551551
};
@@ -606,7 +606,7 @@ OlmDevice.prototype.createInboundSession = async function(
606606
}
607607
});
608608
},
609-
getPrefixedLogger("[createInboundSession]"),
609+
logger.withPrefix("[createInboundSession]"),
610610
);
611611

612612
return result;
@@ -621,8 +621,10 @@ OlmDevice.prototype.createInboundSession = async function(
621621
* @return {Promise<string[]>} a list of known session ids for the device
622622
*/
623623
OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityKey) {
624+
const log = logger.withPrefix("[getSessionIdsForDevice]");
625+
624626
if (this._sessionsInProgress[theirDeviceIdentityKey]) {
625-
logger.log("waiting for olm session to be created");
627+
log.debug(`Waiting for Olm session for ${theirDeviceIdentityKey} to be created`);
626628
try {
627629
await this._sessionsInProgress[theirDeviceIdentityKey];
628630
} catch (e) {
@@ -640,7 +642,7 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK
640642
},
641643
);
642644
},
643-
getPrefixedLogger("[getSessionIdsForDevice]"),
645+
log,
644646
);
645647

646648
return sessionIds;
@@ -654,13 +656,14 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK
654656
* @param {boolean} nowait Don't wait for an in-progress session to complete.
655657
* This should only be set to true of the calling function is the function
656658
* that marked the session as being in-progress.
659+
* @param {Logger} [log] A possibly customised log
657660
* @return {Promise<?string>} session id, or null if no established session
658661
*/
659662
OlmDevice.prototype.getSessionIdForDevice = async function(
660-
theirDeviceIdentityKey, nowait,
663+
theirDeviceIdentityKey, nowait, log,
661664
) {
662665
const sessionInfos = await this.getSessionInfoForDevice(
663-
theirDeviceIdentityKey, nowait,
666+
theirDeviceIdentityKey, nowait, log,
664667
);
665668

666669
if (sessionInfos.length === 0) {
@@ -700,11 +703,16 @@ OlmDevice.prototype.getSessionIdForDevice = async function(
700703
* @param {boolean} nowait Don't wait for an in-progress session to complete.
701704
* This should only be set to true of the calling function is the function
702705
* that marked the session as being in-progress.
706+
* @param {Logger} [log] A possibly customised log
703707
* @return {Array.<{sessionId: string, hasReceivedMessage: Boolean}>}
704708
*/
705-
OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) {
709+
OlmDevice.prototype.getSessionInfoForDevice = async function(
710+
deviceIdentityKey, nowait, log = logger,
711+
) {
712+
log = log.withPrefix("[getSessionInfoForDevice]");
713+
706714
if (this._sessionsInProgress[deviceIdentityKey] && !nowait) {
707-
logger.log("waiting for olm session to be created");
715+
log.debug(`Waiting for Olm session for ${deviceIdentityKey} to be created`);
708716
try {
709717
await this._sessionsInProgress[deviceIdentityKey];
710718
} catch (e) {
@@ -730,7 +738,7 @@ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey,
730738
}
731739
});
732740
},
733-
getPrefixedLogger("[getSessionInfoForDevice]"),
741+
log,
734742
);
735743

736744
return info;
@@ -765,7 +773,7 @@ OlmDevice.prototype.encryptMessage = async function(
765773
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
766774
});
767775
},
768-
getPrefixedLogger("[encryptMessage]"),
776+
logger.withPrefix("[encryptMessage]"),
769777
);
770778
return res;
771779
};
@@ -799,7 +807,7 @@ OlmDevice.prototype.decryptMessage = async function(
799807
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
800808
});
801809
},
802-
getPrefixedLogger("[decryptMessage]"),
810+
logger.withPrefix("[decryptMessage]"),
803811
);
804812
return payloadString;
805813
};
@@ -831,7 +839,7 @@ OlmDevice.prototype.matchesSession = async function(
831839
matches = sessionInfo.session.matches_inbound(ciphertext);
832840
});
833841
},
834-
getPrefixedLogger("[matchesSession]"),
842+
logger.withPrefix("[matchesSession]"),
835843
);
836844
return matches;
837845
};
@@ -1102,7 +1110,7 @@ OlmDevice.prototype.addInboundGroupSession = async function(
11021110
},
11031111
);
11041112
},
1105-
getPrefixedLogger("[addInboundGroupSession]"),
1113+
logger.withPrefix("[addInboundGroupSession]"),
11061114
);
11071115
};
11081116

@@ -1273,7 +1281,7 @@ OlmDevice.prototype.decryptGroupMessage = async function(
12731281
},
12741282
);
12751283
},
1276-
getPrefixedLogger("[decryptGroupMessage]"),
1284+
logger.withPrefix("[decryptGroupMessage]"),
12771285
);
12781286

12791287
if (error) {
@@ -1319,7 +1327,7 @@ OlmDevice.prototype.hasInboundSessionKeys = async function(roomId, senderKey, se
13191327
},
13201328
);
13211329
},
1322-
getPrefixedLogger("[hasInboundSessionKeys]"),
1330+
logger.withPrefix("[hasInboundSessionKeys]"),
13231331
);
13241332

13251333
return result;
@@ -1379,7 +1387,7 @@ OlmDevice.prototype.getInboundGroupSessionKey = async function(
13791387
},
13801388
);
13811389
},
1382-
getPrefixedLogger("[getInboundGroupSessionKey]"),
1390+
logger.withPrefix("[getInboundGroupSessionKey]"),
13831391
);
13841392

13851393
return result;

src/crypto/algorithms/megolm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ limitations under the License.
2222
* @module crypto/algorithms/megolm
2323
*/
2424

25-
import {getPrefixedLogger, logger} from '../../logger';
25+
import {logger} from '../../logger';
2626
import * as utils from "../../utils";
2727
import {polyfillSuper} from "../../utils";
2828
import * as olmlib from "../olmlib";
@@ -736,7 +736,7 @@ MegolmEncryption.prototype._shareKeyWithDevices = async function(
736736
logger.debug(`Ensuring Olm sessions for devices in ${this._roomId}`);
737737
const devicemap = await olmlib.ensureOlmSessionsForDevices(
738738
this._olmDevice, this._baseApis, devicesByUser, otkTimeout, failedServers,
739-
getPrefixedLogger(`[${this._roomId}]`),
739+
logger.withPrefix(`[${this._roomId}]`),
740740
);
741741
logger.debug(`Ensured Olm sessions for devices in ${this._roomId}`);
742742

src/crypto/olmlib.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export async function getExistingOlmSessions(
183183
* @param {Array} [failedServers] An array to fill with remote servers that
184184
* failed to respond to one-time-key requests.
185185
*
186-
* @param {Object} [log] A possibly customised log
186+
* @param {Logger} [log] A possibly customised log
187187
*
188188
* @return {Promise} resolves once the sessions are complete, to
189189
* an Object mapping from userId to deviceId to
@@ -232,18 +232,23 @@ export async function ensureOlmSessionsForDevices(
232232
continue;
233233
}
234234

235+
const forWhom = `for ${key} (${userId}:${deviceId})`;
236+
log.debug(`Ensuring Olm session ${forWhom}`);
235237
if (!olmDevice._sessionsInProgress[key]) {
236238
// pre-emptively mark the session as in-progress to avoid race
237239
// conditions. If we find that we already have a session, then
238240
// we'll resolve
241+
log.debug(`Marking Olm session in progress ${forWhom}`);
239242
olmDevice._sessionsInProgress[key] = new Promise(
240243
(resolve, reject) => {
241244
resolveSession[key] = {
242245
resolve: (...args) => {
246+
log.debug(`Resolved Olm session in progress ${forWhom}`);
243247
delete olmDevice._sessionsInProgress[key];
244248
resolve(...args);
245249
},
246250
reject: (...args) => {
251+
log.debug(`Rejected Olm session in progress ${forWhom}`);
247252
delete olmDevice._sessionsInProgress[key];
248253
reject(...args);
249254
},
@@ -252,8 +257,9 @@ export async function ensureOlmSessionsForDevices(
252257
);
253258
}
254259
const sessionId = await olmDevice.getSessionIdForDevice(
255-
key, resolveSession[key],
260+
key, resolveSession[key], log,
256261
);
262+
log.debug(`Got Olm session ${sessionId} ${forWhom}`);
257263
if (sessionId !== null && resolveSession[key]) {
258264
// we found a session, but we had marked the session as
259265
// in-progress, so unmark it and unblock anything that was
@@ -264,9 +270,9 @@ export async function ensureOlmSessionsForDevices(
264270
}
265271
if (sessionId === null || force) {
266272
if (force) {
267-
log.info(`Forcing new Olm session for ${userId}:${deviceId}`);
273+
log.info(`Forcing new Olm session ${forWhom}`);
268274
} else {
269-
log.info(`Making new Olm session for ${userId}:${deviceId}`);
275+
log.info(`Making new Olm session ${forWhom}`);
270276
}
271277
devicesWithoutSession.push([userId, deviceId]);
272278
}

src/crypto/store/indexeddb-crypto-store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ export class IndexedDBCryptoStore {
596596
* @param {function(*)} func Function called with the
597597
* transaction object: an opaque object that should be passed
598598
* to store functions.
599-
* @param {object} [log] A possibly customised log
599+
* @param {Logger} [log] A possibly customised log
600600
* @return {Promise} Promise that resolves with the result of the `func`
601601
* when the transaction is complete. If the backend is
602602
* async (ie. the indexeddb backend) any of the callback

src/logger.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,28 @@ log.methodFactory = function(methodName, logLevel, loggerName) {
5959
* Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}.
6060
* Can be tailored down to specific use cases if needed.
6161
*/
62-
export const logger = log.getLogger(DEFAULT_NAMESPACE);
62+
export const logger: PrefixedLogger = log.getLogger(DEFAULT_NAMESPACE);
6363
logger.setLevel(log.levels.DEBUG);
6464

6565
interface PrefixedLogger extends Logger {
66-
prefix?: any;
66+
withPrefix?: (prefix: string) => PrefixedLogger;
67+
prefix?: string;
6768
}
6869

69-
export function getPrefixedLogger(prefix): PrefixedLogger {
70+
function extendLogger(logger: PrefixedLogger) {
71+
logger.withPrefix = function(prefix: string): PrefixedLogger {
72+
const existingPrefix = this.prefix || "";
73+
return getPrefixedLogger(existingPrefix + prefix);
74+
};
75+
}
76+
77+
extendLogger(logger);
78+
79+
function getPrefixedLogger(prefix): PrefixedLogger {
7080
const prefixLogger: PrefixedLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`);
7181
if (prefixLogger.prefix !== prefix) {
7282
// Only do this setup work the first time through, as loggers are saved by name.
83+
extendLogger(prefixLogger);
7384
prefixLogger.prefix = prefix;
7485
prefixLogger.setLevel(log.levels.DEBUG);
7586
}

0 commit comments

Comments
 (0)