diff --git a/src/dispatching/dialogue/mod.rs b/src/dispatching/dialogue/mod.rs index a5067ff7..a8cdddc7 100644 --- a/src/dispatching/dialogue/mod.rs +++ b/src/dispatching/dialogue/mod.rs @@ -170,4 +170,4 @@ pub use storage::{RedisStorage, RedisStorageError}; #[cfg(feature = "sqlite-storage")] pub use storage::{SqliteStorage, SqliteStorageError}; -pub use storage::{serializer, InMemStorage, Serializer, Storage}; +pub use storage::{serializer, InMemStorage, Serializer, Storage, TraceStorage}; diff --git a/src/dispatching/dialogue/storage/mod.rs b/src/dispatching/dialogue/storage/mod.rs index 389bacd7..14d6b79b 100644 --- a/src/dispatching/dialogue/storage/mod.rs +++ b/src/dispatching/dialogue/storage/mod.rs @@ -2,6 +2,8 @@ pub mod serializer; mod in_mem_storage; +mod trace_storage; + #[cfg(feature = "redis-storage")] mod redis_storage; @@ -11,6 +13,8 @@ mod sqlite_storage; use futures::future::BoxFuture; pub use in_mem_storage::InMemStorage; +pub use trace_storage::TraceStorage; + #[cfg(feature = "redis-storage")] // FIXME(waffle): use `docsrs` here when issue with combine is resolved #[cfg_attr(all(teloxide_docsrs, feature = "nightly"), doc(cfg(feature = "redis-storage")))] diff --git a/src/dispatching/dialogue/storage/trace_storage.rs b/src/dispatching/dialogue/storage/trace_storage.rs new file mode 100644 index 00000000..69acd5a2 --- /dev/null +++ b/src/dispatching/dialogue/storage/trace_storage.rs @@ -0,0 +1,55 @@ +use std::{fmt::Debug, sync::Arc}; + +use futures::future::BoxFuture; +use log::trace; + +use super::Storage; + +/// Storage wrapper for logging purposes +/// +/// Reports about any dialogue update or removal action on `trace` level +pub struct TraceStorage { + inner: Arc, +} + +impl TraceStorage { + #[must_use] + pub fn new(inner: Arc) -> Arc { + Arc::new(Self { inner }) + } + + pub fn into_inner(self) -> Arc { + self.inner + } +} + +impl Storage for TraceStorage +where + D: Debug, + S: Storage, +{ + type Error = >::Error; + + fn remove_dialogue( + self: Arc, + chat_id: i64, + ) -> BoxFuture<'static, Result, Self::Error>> + where + D: Send + 'static, + { + trace!("Removing dialogue with {}", chat_id); + >::remove_dialogue(self.inner.clone(), chat_id) + } + + fn update_dialogue( + self: Arc, + chat_id: i64, + dialogue: D, + ) -> BoxFuture<'static, Result, Self::Error>> + where + D: Send + 'static, + { + trace!("Updating dialogue with {}: {:#?}", chat_id, dialogue); + >::update_dialogue(self.inner.clone(), chat_id, dialogue) + } +}