Skip to content

Commit d666f07

Browse files
committed
use logging stream for IMAP
1 parent a7d5432 commit d666f07

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

src/imap/client.rs

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

99
use super::capabilities::Capabilities;
1010
use crate::context::Context;
11-
use crate::log::{info, warn};
11+
use crate::log::{LoggingStream, info, warn};
1212
use crate::login_param::{ConnectionCandidate, ConnectionSecurity};
1313
use crate::net::dns::{lookup_host_with_cache, update_connect_timestamp};
1414
use crate::net::proxy::ProxyConfig;
@@ -126,12 +126,12 @@ impl Client {
126126
);
127127
let res = match security {
128128
ConnectionSecurity::Tls => {
129-
Client::connect_secure(resolved_addr, host, strict_tls).await
129+
Client::connect_secure(&context, resolved_addr, host, strict_tls).await
130130
}
131131
ConnectionSecurity::Starttls => {
132132
Client::connect_starttls(resolved_addr, host, strict_tls).await
133133
}
134-
ConnectionSecurity::Plain => Client::connect_insecure(resolved_addr).await,
134+
ConnectionSecurity::Plain => Client::connect_insecure(&context, resolved_addr).await,
135135
};
136136
match res {
137137
Ok(client) => {
@@ -202,9 +202,22 @@ impl Client {
202202
}
203203
}
204204

205-
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> {
206211
let tls_stream = connect_tls_inner(addr, hostname, strict_tls, alpn(addr.port())).await?;
207-
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);
208221
let session_stream: Box<dyn SessionStream> = Box::new(buffered_stream);
209222
let mut client = Client::new(session_stream);
210223
let _greeting = client
@@ -214,9 +227,17 @@ impl Client {
214227
Ok(client)
215228
}
216229

217-
async fn connect_insecure(addr: SocketAddr) -> Result<Self> {
230+
async fn connect_insecure(context: &Context, addr: SocketAddr) -> Result<Self> {
218231
let tcp_stream = connect_tcp_inner(addr).await?;
219-
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);
220241
let session_stream: Box<dyn SessionStream> = Box::new(buffered_stream);
221242
let mut client = Client::new(session_stream);
222243
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_rules! info {
1012
($ctx:expr, $msg:expr) => {
1113
info!($ctx, $msg,)

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)