Skip to content

Commit fdbb367

Browse files
committed
use logging stream for IMAP
1 parent 5fbb1a8 commit fdbb367

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/imap/client.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use tokio::io::BufWriter;
88

99
use super::capabilities::Capabilities;
1010
use crate::context::Context;
11+
use crate::log::LoggingStream;
1112
use crate::login_param::{ConnectionCandidate, ConnectionSecurity};
1213
use crate::net::dns::{lookup_host_with_cache, update_connect_timestamp};
1314
use crate::net::proxy::ProxyConfig;
@@ -125,12 +126,12 @@ impl Client {
125126
);
126127
let res = match security {
127128
ConnectionSecurity::Tls => {
128-
Client::connect_secure(resolved_addr, host, strict_tls).await
129+
Client::connect_secure(&context, resolved_addr, host, strict_tls).await
129130
}
130131
ConnectionSecurity::Starttls => {
131132
Client::connect_starttls(resolved_addr, host, strict_tls).await
132133
}
133-
ConnectionSecurity::Plain => Client::connect_insecure(resolved_addr).await,
134+
ConnectionSecurity::Plain => Client::connect_insecure(&context, resolved_addr).await,
134135
};
135136
match res {
136137
Ok(client) => {
@@ -201,9 +202,22 @@ impl Client {
201202
}
202203
}
203204

204-
async fn connect_secure(addr: SocketAddr, hostname: &str, strict_tls: bool) -> Result<Self> {
205+
async fn connect_secure(
206+
context: &Context,
207+
addr: SocketAddr,
208+
hostname: &str,
209+
strict_tls: bool,
210+
) -> Result<Self> {
205211
let tls_stream = connect_tls_inner(addr, hostname, strict_tls, alpn(addr.port())).await?;
206-
let buffered_stream = BufWriter::new(tls_stream);
212+
let account_id = context.get_id();
213+
let events = context.events.clone();
214+
let logging_stream = LoggingStream::new(
215+
tls_stream,
216+
"some IMAP TLS stream".to_string(),
217+
account_id,
218+
events,
219+
);
220+
let buffered_stream = BufWriter::new(logging_stream);
207221
let session_stream: Box<dyn SessionStream> = Box::new(buffered_stream);
208222
let mut client = Client::new(session_stream);
209223
let _greeting = client
@@ -213,9 +227,17 @@ impl Client {
213227
Ok(client)
214228
}
215229

216-
async fn connect_insecure(addr: SocketAddr) -> Result<Self> {
230+
async fn connect_insecure(context: &Context, addr: SocketAddr) -> Result<Self> {
217231
let tcp_stream = connect_tcp_inner(addr).await?;
218-
let buffered_stream = BufWriter::new(tcp_stream);
232+
let account_id = context.get_id();
233+
let events = context.events.clone();
234+
let logging_stream = LoggingStream::new(
235+
tcp_stream,
236+
"some IMAP insecure TLS stream".to_string(),
237+
account_id,
238+
events,
239+
);
240+
let buffered_stream = BufWriter::new(logging_stream);
219241
let session_stream: Box<dyn SessionStream> = Box::new(buffered_stream);
220242
let mut client = Client::new(session_stream);
221243
let _greeting = client

src/log.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::context::Context;
66

77
mod logging_stream;
88

9+
pub(crate) use logging_stream::LoggingStream;
10+
911
#[macro_export]
1012
macro_rules! info {
1113
($ctx:expr, $msg:expr) => {

src/log/logging_stream.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,41 @@
55
//! that occur are logged before
66
//! they are processed.
77
8-
use std::task::{Context, Poll};
98
use std::pin::Pin;
9+
use std::task::{Context, Poll};
1010
use std::time::Duration;
1111

1212
use pin_project::pin_project;
1313

14+
use crate::events::{Event, EventType, Events};
1415
use crate::net::session::SessionStream;
1516

16-
use tokio::io::{AsyncWrite, AsyncRead, ReadBuf};
17+
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
1718

1819
/// Stream that logs errors to the event channel.
1920
#[derive(Debug)]
2021
#[pin_project]
21-
pub struct LoggingStream<S: SessionStream> {
22+
pub(crate) struct LoggingStream<S: SessionStream> {
2223
#[pin]
2324
inner: S,
2425

2526
/// Name of the stream to distinguish log messages produced by it.
26-
name: String
27+
tag: String,
28+
29+
/// Account ID for logging.
30+
account_id: u32,
31+
32+
/// Event channel.
33+
events: Events,
2734
}
2835

2936
impl<S: SessionStream> LoggingStream<S> {
30-
pub fn new(inner: S, name: String) -> Self {
37+
pub fn new(inner: S, tag: String, account_id: u32, events: Events) -> Self {
3138
Self {
3239
inner,
33-
name
40+
tag,
41+
account_id,
42+
events,
3443
}
3544
}
3645
}
@@ -51,7 +60,15 @@ impl<S: SessionStream> AsyncWrite for LoggingStream<S> {
5160
cx: &mut std::task::Context<'_>,
5261
buf: &[u8],
5362
) -> Poll<std::io::Result<usize>> {
54-
self.project().inner.poll_write(cx, buf)
63+
let log_message = format!("WRITING {}", buf.len());
64+
65+
let projected = self.project();
66+
projected.events.emit(Event {
67+
id: 0,
68+
typ: EventType::Info(log_message),
69+
});
70+
71+
projected.inner.poll_write(cx, buf)
5572
}
5673

5774
fn poll_flush(

0 commit comments

Comments
 (0)