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.
This commit is contained in:
Damir Jelić 2021-03-02 16:22:38 +01:00
parent 00df34ed59
commit 7729e2b11f
3 changed files with 71 additions and 16 deletions

View file

@ -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<PathBuf>,
pub(crate) inner: Db,
store_key: Arc<Option<StoreKey>>,
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<StoreKey>) -> Result<Self> {
fn open_helper(db: Db, path: Option<PathBuf>, store_key: Option<StoreKey>) -> Result<Self> {
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<Self> {
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<Path>, passphrase: &str) -> Result<Self> {
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<DatabaseType> = 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<Path>) -> Result<Self> {
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(

View file

@ -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<UserId>,
device_id: Arc<DeviceIdBox>,
@ -79,6 +79,19 @@ pub struct ReadOnlyDevice {
trust_state: Arc<Atomic<LocalTrust>>,
}
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<S>(x: &Atomic<LocalTrust>, s: S) -> Result<S::Ok, S::Error>
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<UserIdentities>,
}
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;

View file

@ -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<PathBuf>,
inner: Db,
pickle_key: Arc<PickleKey>,
@ -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<TransactionError<serde_json::Error>> for CryptoStoreError {
fn from(e: TransactionError<serde_json::Error>) -> Self {
match e {
@ -135,18 +148,18 @@ impl SledStore {
/// passphrase to encrypt private data.
pub fn open_with_passphrase(path: impl AsRef<Path>, passphrase: Option<&str>) -> Result<Self> {
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<Self> {
SledStore::open_helper(db, passphrase)
SledStore::open_helper(db, None, passphrase)
}
fn open_helper(db: Db, passphrase: Option<&str>) -> Result<Self> {
fn open_helper(db: Db, path: Option<PathBuf>, passphrase: Option<&str>) -> Result<Self> {
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,