From 8152fa44e099a10ce354250b096b926b789924e8 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Fri, 26 Feb 2021 17:53:39 +0000 Subject: [PATCH] Add more logging scopes to session IDs This uses prefix chaining to correlate several scopes together. --- src/crypto/OlmDevice.js | 12 ++++++++---- src/crypto/olmlib.js | 4 ++-- src/crypto/store/indexeddb-crypto-store.js | 2 +- src/logger.ts | 5 +++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/crypto/OlmDevice.js b/src/crypto/OlmDevice.js index cd04f785f..0989d19f3 100644 --- a/src/crypto/OlmDevice.js +++ b/src/crypto/OlmDevice.js @@ -656,13 +656,14 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK * @param {boolean} nowait Don't wait for an in-progress session to complete. * This should only be set to true of the calling function is the function * that marked the session as being in-progress. + * @param {Logger} [log] A possibly customised log * @return {Promise} session id, or null if no established session */ OlmDevice.prototype.getSessionIdForDevice = async function( - theirDeviceIdentityKey, nowait, + theirDeviceIdentityKey, nowait, log, ) { const sessionInfos = await this.getSessionInfoForDevice( - theirDeviceIdentityKey, nowait, + theirDeviceIdentityKey, nowait, log, ); if (sessionInfos.length === 0) { @@ -702,10 +703,13 @@ OlmDevice.prototype.getSessionIdForDevice = async function( * @param {boolean} nowait Don't wait for an in-progress session to complete. * This should only be set to true of the calling function is the function * that marked the session as being in-progress. + * @param {Logger} [log] A possibly customised log * @return {Array.<{sessionId: string, hasReceivedMessage: Boolean}>} */ -OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) { - const log = logger.withPrefix("[getSessionInfoForDevice]"); +OlmDevice.prototype.getSessionInfoForDevice = async function( + deviceIdentityKey, nowait, log = logger, +) { + log = log.withPrefix("[getSessionInfoForDevice]"); if (this._sessionsInProgress[deviceIdentityKey] && !nowait) { log.debug(`Waiting for Olm session for ${deviceIdentityKey} to be created`); diff --git a/src/crypto/olmlib.js b/src/crypto/olmlib.js index b11af14d7..c4e3278cf 100644 --- a/src/crypto/olmlib.js +++ b/src/crypto/olmlib.js @@ -183,7 +183,7 @@ export async function getExistingOlmSessions( * @param {Array} [failedServers] An array to fill with remote servers that * failed to respond to one-time-key requests. * - * @param {Object} [log] A possibly customised log + * @param {Logger} [log] A possibly customised log * * @return {Promise} resolves once the sessions are complete, to * an Object mapping from userId to deviceId to @@ -257,7 +257,7 @@ export async function ensureOlmSessionsForDevices( ); } const sessionId = await olmDevice.getSessionIdForDevice( - key, resolveSession[key], + key, resolveSession[key], log, ); log.debug(`Got Olm session ${sessionId} ${forWhom}`); if (sessionId !== null && resolveSession[key]) { diff --git a/src/crypto/store/indexeddb-crypto-store.js b/src/crypto/store/indexeddb-crypto-store.js index 217c5e621..50f3c2678 100644 --- a/src/crypto/store/indexeddb-crypto-store.js +++ b/src/crypto/store/indexeddb-crypto-store.js @@ -596,7 +596,7 @@ export class IndexedDBCryptoStore { * @param {function(*)} func Function called with the * transaction object: an opaque object that should be passed * to store functions. - * @param {object} [log] A possibly customised log + * @param {Logger} [log] A possibly customised log * @return {Promise} Promise that resolves with the result of the `func` * when the transaction is complete. If the backend is * async (ie. the indexeddb backend) any of the callback diff --git a/src/logger.ts b/src/logger.ts index 4a6b96158..e5fced726 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -68,8 +68,9 @@ interface PrefixedLogger extends Logger { } function extendLogger(logger: PrefixedLogger) { - logger.withPrefix = function(prefix: string) { - return getPrefixedLogger(this.prefix + prefix); + logger.withPrefix = function(prefix: string): PrefixedLogger { + const existingPrefix = this.prefix || ""; + return getPrefixedLogger(existingPrefix + prefix); }; }