conditionally disable verbose logging (#92)

* conditionally disable verbose logging

* disable logging setting context ... conditionally

* no verbose logging by default
pull/93/head
QiuJiangkun 4 years ago committed by GitHub
parent 83395f3d7c
commit 44a59b66e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Cargo.toml
  2. 9
      src/compat.rs
  3. 4
      src/handshake.rs
  4. 3
      src/lib.rs

@ -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-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-rustls-native-certs = ["tokio-runtime", "real-tokio-rustls", "rustls-native-certs", "tungstenite/rustls-tls"]
tokio-openssl = ["tokio-runtime", "real-tokio-openssl", "openssl"] tokio-openssl = ["tokio-runtime", "real-tokio-openssl", "openssl"]
verbose-logging = []
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = ["async-std-runtime", "tokio-runtime", "gio-runtime", "async-tls", "async-native-tls", "tokio-native-tls"] features = ["async-std-runtime", "tokio-runtime", "gio-runtime", "async-tls", "async-native-tls", "tokio-native-tls"]

@ -1,3 +1,4 @@
#[allow(unused_imports)]
use log::*; use log::*;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::pin::Pin; use std::pin::Pin;
@ -121,6 +122,7 @@ where
where where
F: FnOnce(&mut Context<'_>, Pin<&mut S>) -> Poll<std::io::Result<R>>, F: FnOnce(&mut Context<'_>, Pin<&mut S>) -> Poll<std::io::Result<R>>,
{ {
#[cfg(feature = "verbose-logging")]
trace!("{}:{} AllowStd.with_context", file!(), line!()); trace!("{}:{} AllowStd.with_context", file!(), line!());
let waker = match kind { let waker = match kind {
ContextWaker::Read => task::waker_ref(&self.read_waker_proxy), ContextWaker::Read => task::waker_ref(&self.read_waker_proxy),
@ -144,8 +146,10 @@ where
S: AsyncRead + Unpin, S: AsyncRead + Unpin,
{ {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
#[cfg(feature = "verbose-logging")]
trace!("{}:{} Read.read", file!(), line!()); trace!("{}:{} Read.read", file!(), line!());
match self.with_context(ContextWaker::Read, |ctx, stream| { match self.with_context(ContextWaker::Read, |ctx, stream| {
#[cfg(feature = "verbose-logging")]
trace!( trace!(
"{}:{} Read.with_context read -> poll_read", "{}:{} Read.with_context read -> poll_read",
file!(), file!(),
@ -164,8 +168,10 @@ where
S: AsyncWrite + Unpin, S: AsyncWrite + Unpin,
{ {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
#[cfg(feature = "verbose-logging")]
trace!("{}:{} Write.write", file!(), line!()); trace!("{}:{} Write.write", file!(), line!());
match self.with_context(ContextWaker::Write, |ctx, stream| { match self.with_context(ContextWaker::Write, |ctx, stream| {
#[cfg(feature = "verbose-logging")]
trace!( trace!(
"{}:{} Write.with_context write -> poll_write", "{}:{} Write.with_context write -> poll_write",
file!(), file!(),
@ -179,8 +185,10 @@ where
} }
fn flush(&mut self) -> std::io::Result<()> { fn flush(&mut self) -> std::io::Result<()> {
#[cfg(feature = "verbose-logging")]
trace!("{}:{} Write.flush", file!(), line!()); trace!("{}:{} Write.flush", file!(), line!());
match self.with_context(ContextWaker::Write, |ctx, stream| { match self.with_context(ContextWaker::Write, |ctx, stream| {
#[cfg(feature = "verbose-logging")]
trace!( trace!(
"{}:{} Write.with_context flush -> poll_flush", "{}:{} Write.with_context flush -> poll_flush",
file!(), file!(),
@ -198,6 +206,7 @@ pub(crate) fn cvt<T>(r: Result<T, WsError>) -> Poll<Result<T, WsError>> {
match r { match r {
Ok(v) => Poll::Ready(Ok(v)), Ok(v) => Poll::Ready(Ok(v)),
Err(WsError::Io(ref e)) if e.kind() == std::io::ErrorKind::WouldBlock => { Err(WsError::Io(ref e)) if e.kind() == std::io::ErrorKind::WouldBlock => {
#[cfg(feature = "verbose-logging")]
trace!("WouldBlock"); trace!("WouldBlock");
Poll::Pending Poll::Pending
} }

@ -1,6 +1,7 @@
use crate::compat::{AllowStd, SetWaker}; use crate::compat::{AllowStd, SetWaker};
use crate::WebSocketStream; use crate::WebSocketStream;
use futures_io::{AsyncRead, AsyncWrite}; use futures_io::{AsyncRead, AsyncWrite};
#[allow(unused_imports)]
use log::*; use log::*;
use std::future::Future; use std::future::Future;
use std::io::{Read, Write}; use std::io::{Read, Write};
@ -43,6 +44,7 @@ where
.0 .0
.take() .take()
.expect("future polled after completion"); .expect("future polled after completion");
#[cfg(feature = "verbose-logging")]
trace!("Setting context when skipping handshake"); trace!("Setting context when skipping handshake");
let stream = AllowStd::new(inner.stream, ctx.waker()); let stream = AllowStd::new(inner.stream, ctx.waker());
@ -129,6 +131,7 @@ where
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = self.0.take().expect("future polled after completion"); let inner = self.0.take().expect("future polled after completion");
#[cfg(feature = "verbose-logging")]
trace!("Setting ctx when starting handshake"); trace!("Setting ctx when starting handshake");
let stream = AllowStd::new(inner.stream, ctx.waker()); let stream = AllowStd::new(inner.stream, ctx.waker());
@ -155,6 +158,7 @@ where
.expect("future polled after completion"); .expect("future polled after completion");
let machine = s.get_mut(); let machine = s.get_mut();
#[cfg(feature = "verbose-logging")]
trace!("Setting context in handshake"); trace!("Setting context in handshake");
machine.get_mut().set_waker(cx.waker()); machine.get_mut().set_waker(cx.waker());

@ -260,6 +260,7 @@ impl<S> WebSocketStream<S> {
F: FnOnce(&mut WebSocket<AllowStd<S>>) -> R, F: FnOnce(&mut WebSocket<AllowStd<S>>) -> R,
AllowStd<S>: Read + Write, AllowStd<S>: Read + Write,
{ {
#[cfg(feature = "verbose-logging")]
trace!("{}:{} WebSocketStream.with_context", file!(), line!()); trace!("{}:{} WebSocketStream.with_context", file!(), line!());
if let Some((kind, ctx)) = ctx { if let Some((kind, ctx)) = ctx {
self.inner.get_mut().set_waker(kind, &ctx.waker()); self.inner.get_mut().set_waker(kind, &ctx.waker());
@ -305,8 +306,10 @@ where
type Item = Result<Message, WsError>; type Item = Result<Message, WsError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
#[cfg(feature = "verbose-logging")]
trace!("{}:{} Stream.poll_next", file!(), line!()); trace!("{}:{} Stream.poll_next", file!(), line!());
match futures_util::ready!(self.with_context(Some((ContextWaker::Read, cx)), |s| { match futures_util::ready!(self.with_context(Some((ContextWaker::Read, cx)), |s| {
#[cfg(feature = "verbose-logging")]
trace!( trace!(
"{}:{} Stream.with_context poll_next -> read_message()", "{}:{} Stream.with_context poll_next -> read_message()",
file!(), file!(),

Loading…
Cancel
Save