From 7729e2b11f0b65b97a8aee92ca715117f0b9d52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 2 Mar 2021 16:22:38 +0100 Subject: [PATCH] matrix-sdk: Add some custom debug implementations This should avoid polluting the logs with sled trees and a lot of redundant info in a device if a device or store ends up in the structured logs. --- matrix_sdk_base/src/store/sled_store/mod.rs | 36 ++++++++++++++++----- matrix_sdk_crypto/src/identities/device.rs | 25 ++++++++++++-- matrix_sdk_crypto/src/store/sled.rs | 26 +++++++++++---- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/matrix_sdk_base/src/store/sled_store/mod.rs b/matrix_sdk_base/src/store/sled_store/mod.rs index 36a27d8b..5e8662c5 100644 --- a/matrix_sdk_base/src/store/sled_store/mod.rs +++ b/matrix_sdk_base/src/store/sled_store/mod.rs @@ -14,7 +14,13 @@ mod store_key; -use std::{collections::BTreeSet, convert::TryFrom, path::Path, sync::Arc, time::SystemTime}; +use std::{ + collections::BTreeSet, + convert::TryFrom, + path::{Path, PathBuf}, + sync::Arc, + time::SystemTime, +}; use futures::{ stream::{self, Stream}, @@ -128,8 +134,9 @@ impl EncodeKey for (&str, &str, &str) { } } -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SledStore { + path: Option, pub(crate) inner: Db, store_key: Arc>, session: Tree, @@ -148,8 +155,20 @@ pub struct SledStore { presence: Tree, } +impl std::fmt::Debug for SledStore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(path) = &self.path { + f.debug_struct("SledStore").field("path", &path).finish() + } else { + f.debug_struct("SledStore") + .field("path", &"memory store") + .finish() + } + } +} + impl SledStore { - fn open_helper(db: Db, store_key: Option) -> Result { + fn open_helper(db: Db, path: Option, store_key: Option) -> Result { let session = db.open_tree("session")?; let account_data = db.open_tree("account_data")?; @@ -169,6 +188,7 @@ impl SledStore { let stripped_room_state = db.open_tree("stripped_room_state")?; Ok(Self { + path, inner: db, store_key: store_key.into(), session, @@ -191,12 +211,12 @@ impl SledStore { pub fn open() -> Result { let db = Config::new().temporary(true).open()?; - SledStore::open_helper(db, None) + SledStore::open_helper(db, None, None) } pub fn open_with_passphrase(path: impl AsRef, passphrase: &str) -> Result { let path = path.as_ref().join("matrix-sdk-state"); - let db = Config::new().temporary(false).path(path).open()?; + let db = Config::new().temporary(false).path(&path).open()?; let store_key: Option = db .get("store_key".encode())? @@ -219,14 +239,14 @@ impl SledStore { key }; - SledStore::open_helper(db, Some(store_key)) + SledStore::open_helper(db, Some(path), Some(store_key)) } pub fn open_with_path(path: impl AsRef) -> Result { let path = path.as_ref().join("matrix-sdk-state"); - let db = Config::new().temporary(false).path(path).open()?; + let db = Config::new().temporary(false).path(&path).open()?; - SledStore::open_helper(db, None) + SledStore::open_helper(db, Some(path), None) } fn serialize_event( diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index 3e79d3f4..76e2498d 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -59,7 +59,7 @@ use crate::{ use super::{atomic_bool_deserializer, atomic_bool_serializer}; /// A read-only version of a `Device`. -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct ReadOnlyDevice { user_id: Arc, device_id: Arc, @@ -79,6 +79,19 @@ pub struct ReadOnlyDevice { trust_state: Arc>, } +impl std::fmt::Debug for ReadOnlyDevice { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ReadOnlyDevice") + .field("user_id", self.user_id()) + .field("device_id", &self.device_id()) + .field("display_name", self.display_name()) + .field("keys", self.keys()) + .field("deleted", &self.deleted.load(Ordering::SeqCst)) + .field("trust_state", &self.trust_state) + .finish() + } +} + fn local_trust_serializer(x: &Atomic, s: S) -> Result where S: Serializer, @@ -95,7 +108,7 @@ where Ok(Arc::new(Atomic::new(value))) } -#[derive(Debug, Clone)] +#[derive(Clone)] /// A device represents a E2EE capable client of an user. pub struct Device { pub(crate) inner: ReadOnlyDevice, @@ -105,6 +118,14 @@ pub struct Device { pub(crate) device_owner_identity: Option, } +impl std::fmt::Debug for Device { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Device") + .field("device", &self.inner) + .finish() + } +} + impl Deref for Device { type Target = ReadOnlyDevice; diff --git a/matrix_sdk_crypto/src/store/sled.rs b/matrix_sdk_crypto/src/store/sled.rs index 5bbc5562..f833b770 100644 --- a/matrix_sdk_crypto/src/store/sled.rs +++ b/matrix_sdk_crypto/src/store/sled.rs @@ -15,7 +15,7 @@ use std::{ collections::{HashMap, HashSet}, convert::TryFrom, - path::Path, + path::{Path, PathBuf}, sync::Arc, }; @@ -96,8 +96,9 @@ impl EncodeKey for (&str, &str, &str) { } /// An in-memory only store that will forget all the E2EE key once it's dropped. -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct SledStore { + path: Option, inner: Db, pickle_key: Arc, @@ -121,6 +122,18 @@ pub struct SledStore { values: Tree, } +impl std::fmt::Debug for SledStore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(path) = &self.path { + f.debug_struct("SledStore").field("path", &path).finish() + } else { + f.debug_struct("SledStore") + .field("path", &"memory store") + .finish() + } + } +} + impl From> for CryptoStoreError { fn from(e: TransactionError) -> Self { match e { @@ -135,18 +148,18 @@ impl SledStore { /// passphrase to encrypt private data. pub fn open_with_passphrase(path: impl AsRef, passphrase: Option<&str>) -> Result { let path = path.as_ref().join("matrix-sdk-crypto"); - let db = Config::new().temporary(false).path(path).open()?; + let db = Config::new().temporary(false).path(&path).open()?; - SledStore::open_helper(db, passphrase) + SledStore::open_helper(db, Some(path), passphrase) } /// Create a sled based cryptostore using the given sled database. /// The given passphrase will be used to encrypt private data. pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result { - SledStore::open_helper(db, passphrase) + SledStore::open_helper(db, None, passphrase) } - fn open_helper(db: Db, passphrase: Option<&str>) -> Result { + fn open_helper(db: Db, path: Option, passphrase: Option<&str>) -> Result { let account = db.open_tree("account")?; let private_identity = db.open_tree("private_identity")?; @@ -171,6 +184,7 @@ impl SledStore { }; Ok(Self { + path, inner: db, pickle_key: pickle_key.into(), account,