Skip to content

Commit 7b59311

Browse files
committed
feat(log): improve quality of debug level logs
1 parent a594341 commit 7b59311

File tree

6 files changed

+77
-42
lines changed

6 files changed

+77
-42
lines changed

src/client/connect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl Service for HttpConnector {
103103
type Future = HttpConnecting;
104104

105105
fn call(&self, uri: Uri) -> Self::Future {
106-
debug!("Http::connect({:?})", uri);
106+
trace!("Http::connect({:?})", uri);
107107

108108
if self.enforce_http {
109109
if uri.scheme() != Some("http") {
@@ -241,14 +241,14 @@ impl ConnectingTcp {
241241
trace!("connect error {:?}", e);
242242
err = Some(e);
243243
if let Some(addr) = self.addrs.next() {
244-
debug!("connecting to {:?}", addr);
244+
debug!("connecting to {}", addr);
245245
*current = TcpStream::connect(&addr, handle);
246246
continue;
247247
}
248248
}
249249
}
250250
} else if let Some(addr) = self.addrs.next() {
251-
debug!("connecting to {:?}", addr);
251+
debug!("connecting to {}", addr);
252252
self.current = Some(TcpStream::connect(&addr, handle));
253253
continue;
254254
}

src/client/dns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Future for Work {
2424
type Error = io::Error;
2525

2626
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
27-
debug!("resolve host={:?}, port={:?}", self.host, self.port);
27+
debug!("resolving host={:?}, port={:?}", self.host, self.port);
2828
(&*self.host, self.port).to_socket_addrs()
2929
.map(|i| Async::Ready(IpAddrs { iter: i }))
3030
}

src/client/pool.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<T: Clone> Pool<T> {
8585

8686
match entry {
8787
Some(entry) => {
88+
debug!("pooling idle connection for {:?}", key);
8889
inner.idle.entry(key)
8990
.or_insert(Vec::new())
9091
.push(entry);
@@ -95,7 +96,6 @@ impl<T: Clone> Pool<T> {
9596

9697

9798
pub fn pooled(&self, key: Rc<String>, value: T) -> Pooled<T> {
98-
trace!("Pool::pooled {:?}", key);
9999
Pooled {
100100
entry: Entry {
101101
value: value,
@@ -112,7 +112,7 @@ impl<T: Clone> Pool<T> {
112112
}
113113

114114
fn reuse(&self, key: Rc<String>, mut entry: Entry<T>) -> Pooled<T> {
115-
trace!("Pool::reuse {:?}", key);
115+
trace!("reuse {:?}", key);
116116
entry.is_reused = true;
117117
entry.status.set(TimedKA::Busy);
118118
Pooled {
@@ -123,7 +123,7 @@ impl<T: Clone> Pool<T> {
123123
}
124124

125125
fn park(&mut self, key: Rc<String>, tx: relay::Sender<Entry<T>>) {
126-
trace!("Pool::park {:?}", key);
126+
trace!("park; waiting for idle connection: {:?}", key);
127127
self.inner.borrow_mut()
128128
.parked.entry(key)
129129
.or_insert(VecDeque::new())
@@ -133,7 +133,7 @@ impl<T: Clone> Pool<T> {
133133

134134
impl<T> Pool<T> {
135135
fn clean_parked(&mut self, key: &Rc<String>) {
136-
trace!("Pool::clean_parked {:?}", key);
136+
trace!("clean_parked {:?}", key);
137137
let mut inner = self.inner.borrow_mut();
138138

139139
let mut remove_parked = false;
@@ -285,7 +285,7 @@ impl<T: Clone> Future for Checkout<T> {
285285
while let Some(entry) = list.pop() {
286286
match entry.status.get() {
287287
TimedKA::Idle(idle_at) if !expiration.expires(idle_at) => {
288-
trace!("Checkout::poll found idle client for {:?}", key);
288+
debug!("found idle connection for {:?}", key);
289289
should_remove = list.is_empty();
290290
return Some(entry);
291291
},

src/proto/conn.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ where I: AsyncRead + AsyncWrite,
169169
return Err(e);
170170
}
171171
};
172+
173+
debug!("incoming body is {}", decoder);
174+
172175
self.state.busy();
173176
if head.expecting_continue() {
174177
let msg = b"HTTP/1.1 100 Continue\r\n\r\n";
@@ -203,8 +206,11 @@ where I: AsyncRead + AsyncWrite,
203206
if !slice.is_empty() {
204207
return Ok(Async::Ready(Some(super::Chunk::from(slice))));
205208
} else if decoder.is_eof() {
209+
debug!("incoming body completed");
206210
(Reading::KeepAlive, Ok(Async::Ready(None)))
207211
} else {
212+
trace!("decode stream unexpectedly ended");
213+
//TODO: Should this return an UnexpectedEof?
208214
(Reading::Closed, Ok(Async::Ready(None)))
209215
}
210216

@@ -240,10 +246,12 @@ where I: AsyncRead + AsyncWrite,
240246
assert!(!self.can_read_head() && !self.can_read_body());
241247

242248
if !self.io.read_buf().is_empty() {
249+
debug!("received an unexpected {} bytes", self.io.read_buf().len());
243250
Err(io::Error::new(io::ErrorKind::InvalidData, "unexpected bytes after message ended"))
244251
} else {
245252
match self.io.read_from_io() {
246253
Ok(Async::Ready(0)) => {
254+
trace!("try_empty_read; found EOF on connection");
247255
self.state.close_read();
248256
let must_error = !self.state.is_idle() && T::should_error_on_parse_eof();
249257
if must_error {
@@ -252,11 +260,11 @@ where I: AsyncRead + AsyncWrite,
252260
Ok(())
253261
}
254262
},
255-
Ok(Async::Ready(_)) => {
263+
Ok(Async::Ready(n)) => {
264+
debug!("received {} bytes on an idle connection", n);
256265
Err(io::Error::new(io::ErrorKind::InvalidData, "unexpected bytes after message ended"))
257266
},
258267
Ok(Async::NotReady) => {
259-
trace!("try_empty_read; read blocked");
260268
Ok(())
261269
},
262270
Err(e) => {
@@ -616,7 +624,7 @@ where I: AsyncRead + AsyncWrite + 'static,
616624
K: KeepAlive + 'static,
617625
T::Outgoing: fmt::Debug {}
618626

619-
impl<I, B: AsRef<[u8]>, T, K: fmt::Debug> fmt::Debug for Conn<I, B, T, K> {
627+
impl<I, B: AsRef<[u8]>, T, K: KeepAlive> fmt::Debug for Conn<I, B, T, K> {
620628
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
621629
f.debug_struct("Conn")
622630
.field("state", &self.state)
@@ -650,13 +658,13 @@ enum Writing<B> {
650658
Closed,
651659
}
652660

653-
impl<B: AsRef<[u8]>, K: fmt::Debug> fmt::Debug for State<B, K> {
661+
impl<B: AsRef<[u8]>, K: KeepAlive> fmt::Debug for State<B, K> {
654662
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
655663
f.debug_struct("State")
656664
.field("reading", &self.reading)
657665
.field("writing", &self.writing)
658-
.field("keep_alive", &self.keep_alive)
659-
.field("method", &self.method)
666+
.field("keep_alive", &self.keep_alive.status())
667+
//.field("method", &self.method)
660668
.field("read_task", &self.read_task)
661669
.finish()
662670
}

src/proto/h1/decode.rs

+49-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::usize;
23
use std::io;
34

@@ -11,33 +12,21 @@ use self::Kind::{Length, Chunked, Eof};
1112
///
1213
/// If a message body does not include a Transfer-Encoding, it *should*
1314
/// include a Content-Length header.
14-
#[derive(Debug, Clone, PartialEq)]
15+
#[derive(Clone, PartialEq)]
1516
pub struct Decoder {
1617
kind: Kind,
1718
}
1819

19-
impl Decoder {
20-
pub fn length(x: u64) -> Decoder {
21-
Decoder { kind: Kind::Length(x) }
22-
}
23-
24-
pub fn chunked() -> Decoder {
25-
Decoder { kind: Kind::Chunked(ChunkedState::Size, 0) }
26-
}
27-
28-
pub fn eof() -> Decoder {
29-
Decoder { kind: Kind::Eof(false) }
30-
}
31-
}
32-
33-
#[derive(Debug, Clone, PartialEq)]
20+
#[derive(Debug, Clone, Copy, PartialEq)]
3421
enum Kind {
3522
/// A Reader used when a Content-Length header is passed with a positive integer.
3623
Length(u64),
3724
/// A Reader used when Transfer-Encoding is `chunked`.
3825
Chunked(ChunkedState, u64),
3926
/// A Reader used for responses that don't indicate a length or chunked.
4027
///
28+
/// The bool tracks when EOF is seen on the transport.
29+
///
4130
/// Note: This should only used for `Response`s. It is illegal for a
4231
/// `Request` to be made with both `Content-Length` and
4332
/// `Transfer-Encoding: chunked` missing, as explained from the spec:
@@ -53,7 +42,7 @@ enum Kind {
5342
Eof(bool),
5443
}
5544

56-
#[derive(Debug, PartialEq, Clone)]
45+
#[derive(Debug, PartialEq, Clone, Copy)]
5746
enum ChunkedState {
5847
Size,
5948
SizeLws,
@@ -68,6 +57,22 @@ enum ChunkedState {
6857
}
6958

7059
impl Decoder {
60+
// constructors
61+
62+
pub fn length(x: u64) -> Decoder {
63+
Decoder { kind: Kind::Length(x) }
64+
}
65+
66+
pub fn chunked() -> Decoder {
67+
Decoder { kind: Kind::Chunked(ChunkedState::Size, 0) }
68+
}
69+
70+
pub fn eof() -> Decoder {
71+
Decoder { kind: Kind::Eof(false) }
72+
}
73+
74+
// methods
75+
7176
pub fn is_eof(&self) -> bool {
7277
trace!("is_eof? {:?}", self);
7378
match self.kind {
@@ -77,9 +82,7 @@ impl Decoder {
7782
_ => false,
7883
}
7984
}
80-
}
8185

82-
impl Decoder {
8386
pub fn decode<R: MemRead>(&mut self, body: &mut R) -> Poll<Bytes, io::Error> {
8487
match self.kind {
8588
Length(ref mut remaining) => {
@@ -131,6 +134,23 @@ impl Decoder {
131134
}
132135
}
133136

137+
138+
impl fmt::Debug for Decoder {
139+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
140+
fmt::Debug::fmt(&self.kind, f)
141+
}
142+
}
143+
144+
impl fmt::Display for Decoder {
145+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146+
match self.kind {
147+
Kind::Length(n) => write!(f, "content-length ({} bytes)", n),
148+
Kind::Chunked(..) => f.write_str("chunked encoded"),
149+
Kind::Eof(..) => f.write_str("until end-of-file"),
150+
}
151+
}
152+
}
153+
134154
macro_rules! byte (
135155
($rdr:ident) => ({
136156
let buf = try_ready!($rdr.read_mem(1));
@@ -154,7 +174,7 @@ impl ChunkedState {
154174
Size => ChunkedState::read_size(body, size),
155175
SizeLws => ChunkedState::read_size_lws(body),
156176
Extension => ChunkedState::read_extension(body),
157-
SizeLf => ChunkedState::read_size_lf(body, size),
177+
SizeLf => ChunkedState::read_size_lf(body, *size),
158178
Body => ChunkedState::read_body(body, size, buf),
159179
BodyCr => ChunkedState::read_body_cr(body),
160180
BodyLf => ChunkedState::read_body_lf(body),
@@ -209,11 +229,17 @@ impl ChunkedState {
209229
_ => Ok(Async::Ready(ChunkedState::Extension)), // no supported extensions
210230
}
211231
}
212-
fn read_size_lf<R: MemRead>(rdr: &mut R, size: &mut u64) -> Poll<ChunkedState, io::Error> {
232+
fn read_size_lf<R: MemRead>(rdr: &mut R, size: u64) -> Poll<ChunkedState, io::Error> {
213233
trace!("Chunk size is {:?}", size);
214234
match byte!(rdr) {
215-
b'\n' if *size > 0 => Ok(Async::Ready(ChunkedState::Body)),
216-
b'\n' if *size == 0 => Ok(Async::Ready(ChunkedState::EndCr)),
235+
b'\n' => {
236+
if size == 0 {
237+
Ok(Async::Ready(ChunkedState::EndCr))
238+
} else {
239+
debug!("incoming chunked header: {0:#X} ({0} bytes)", size);
240+
Ok(Async::Ready(ChunkedState::Body))
241+
}
242+
},
217243
_ => Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk size LF")),
218244
}
219245
}

src/proto/io.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ impl<T: AsyncRead + AsyncWrite> Buffered<T> {
7070
pub fn parse<S: Http1Transaction>(&mut self) -> Poll<MessageHead<S::Incoming>, ::Error> {
7171
loop {
7272
match try!(S::parse(&mut self.read_buf)) {
73-
Some(head) => {
74-
//trace!("parsed {} bytes out of {}", len, self.read_buf.len());
75-
return Ok(Async::Ready(head.0))
73+
Some((head, len)) => {
74+
debug!("parsed {} headers ({} bytes)", head.headers.len(), len);
75+
return Ok(Async::Ready(head))
7676
},
7777
None => {
7878
if self.read_buf.capacity() >= MAX_BUFFER_SIZE {
@@ -118,6 +118,7 @@ impl<T: AsyncRead + AsyncWrite> Buffered<T> {
118118
return Err(e)
119119
}
120120
};
121+
debug!("read {} bytes", n);
121122
self.read_buf.advance_mut(n);
122123
Ok(Async::Ready(n))
123124
}
@@ -154,7 +155,7 @@ impl<T: Write> Write for Buffered<T> {
154155
} else {
155156
loop {
156157
let n = try!(self.write_buf.write_into(&mut self.io));
157-
trace!("flushed {} bytes", n);
158+
debug!("flushed {} bytes", n);
158159
if self.write_buf.remaining() == 0 {
159160
break;
160161
}

0 commit comments

Comments
 (0)