From 44a59b66e595e3e4daaf4edabd2ce182ea298e57 Mon Sep 17 00:00:00 2001 From: QiuJiangkun Date: Mon, 28 Jun 2021 18:09:51 +0800 Subject: [PATCH] conditionally disable verbose logging (#92) * conditionally disable verbose logging * disable logging setting context ... conditionally * no verbose logging by default --- Cargo.toml | 1 + src/compat.rs | 9 +++++++++ src/handshake.rs | 4 ++++ src/lib.rs | 3 +++ 4 files changed, 17 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 55de5cb..e6a0f3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ tokio-native-tls = ["tokio-runtime", "real-tokio-native-tls", "real-native-tls", tokio-rustls-webpki-roots = ["tokio-runtime", "real-tokio-rustls", "webpki-roots", "tungstenite/rustls-tls"] tokio-rustls-native-certs = ["tokio-runtime", "real-tokio-rustls", "rustls-native-certs", "tungstenite/rustls-tls"] tokio-openssl = ["tokio-runtime", "real-tokio-openssl", "openssl"] +verbose-logging = [] [package.metadata.docs.rs] features = ["async-std-runtime", "tokio-runtime", "gio-runtime", "async-tls", "async-native-tls", "tokio-native-tls"] diff --git a/src/compat.rs b/src/compat.rs index eed051d..ffc1353 100644 --- a/src/compat.rs +++ b/src/compat.rs @@ -1,3 +1,4 @@ +#[allow(unused_imports)] use log::*; use std::io::{Read, Write}; use std::pin::Pin; @@ -121,6 +122,7 @@ where where F: FnOnce(&mut Context<'_>, Pin<&mut S>) -> Poll>, { + #[cfg(feature = "verbose-logging")] trace!("{}:{} AllowStd.with_context", file!(), line!()); let waker = match kind { ContextWaker::Read => task::waker_ref(&self.read_waker_proxy), @@ -144,8 +146,10 @@ where S: AsyncRead + Unpin, { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + #[cfg(feature = "verbose-logging")] trace!("{}:{} Read.read", file!(), line!()); match self.with_context(ContextWaker::Read, |ctx, stream| { + #[cfg(feature = "verbose-logging")] trace!( "{}:{} Read.with_context read -> poll_read", file!(), @@ -164,8 +168,10 @@ where S: AsyncWrite + Unpin, { fn write(&mut self, buf: &[u8]) -> std::io::Result { + #[cfg(feature = "verbose-logging")] trace!("{}:{} Write.write", file!(), line!()); match self.with_context(ContextWaker::Write, |ctx, stream| { + #[cfg(feature = "verbose-logging")] trace!( "{}:{} Write.with_context write -> poll_write", file!(), @@ -179,8 +185,10 @@ where } fn flush(&mut self) -> std::io::Result<()> { + #[cfg(feature = "verbose-logging")] trace!("{}:{} Write.flush", file!(), line!()); match self.with_context(ContextWaker::Write, |ctx, stream| { + #[cfg(feature = "verbose-logging")] trace!( "{}:{} Write.with_context flush -> poll_flush", file!(), @@ -198,6 +206,7 @@ pub(crate) fn cvt(r: Result) -> Poll> { match r { Ok(v) => Poll::Ready(Ok(v)), Err(WsError::Io(ref e)) if e.kind() == std::io::ErrorKind::WouldBlock => { + #[cfg(feature = "verbose-logging")] trace!("WouldBlock"); Poll::Pending } diff --git a/src/handshake.rs b/src/handshake.rs index a04f67a..b4489f4 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -1,6 +1,7 @@ use crate::compat::{AllowStd, SetWaker}; use crate::WebSocketStream; use futures_io::{AsyncRead, AsyncWrite}; +#[allow(unused_imports)] use log::*; use std::future::Future; use std::io::{Read, Write}; @@ -43,6 +44,7 @@ where .0 .take() .expect("future polled after completion"); + #[cfg(feature = "verbose-logging")] trace!("Setting context when skipping handshake"); let stream = AllowStd::new(inner.stream, ctx.waker()); @@ -129,6 +131,7 @@ where fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll { let inner = self.0.take().expect("future polled after completion"); + #[cfg(feature = "verbose-logging")] trace!("Setting ctx when starting handshake"); let stream = AllowStd::new(inner.stream, ctx.waker()); @@ -155,6 +158,7 @@ where .expect("future polled after completion"); let machine = s.get_mut(); + #[cfg(feature = "verbose-logging")] trace!("Setting context in handshake"); machine.get_mut().set_waker(cx.waker()); diff --git a/src/lib.rs b/src/lib.rs index 7dd2d11..45495b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -260,6 +260,7 @@ impl WebSocketStream { F: FnOnce(&mut WebSocket>) -> R, AllowStd: Read + Write, { + #[cfg(feature = "verbose-logging")] trace!("{}:{} WebSocketStream.with_context", file!(), line!()); if let Some((kind, ctx)) = ctx { self.inner.get_mut().set_waker(kind, &ctx.waker()); @@ -305,8 +306,10 @@ where type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + #[cfg(feature = "verbose-logging")] trace!("{}:{} Stream.poll_next", file!(), line!()); match futures_util::ready!(self.with_context(Some((ContextWaker::Read, cx)), |s| { + #[cfg(feature = "verbose-logging")] trace!( "{}:{} Stream.with_context poll_next -> read_message()", file!(),