Skip to content

Commit df6d311

Browse files
committed
Remove log and env_log depndencies
1 parent 0851270 commit df6d311

File tree

6 files changed

+37
-52
lines changed

6 files changed

+37
-52
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ path = "../core"
5151
async-std = { version = "1.12.0", features = ["attributes", "unstable"] }
5252
async-compat = "0.2.1"
5353
tokio = { version = "1.27.0", features = ["macros", "net", "process", "rt", "rt-multi-thread", "sync", "time"] }
54-
env_logger = "0.7.1"
5554
anyhow = "1.0.28"
5655
instant = "0.1"
5756
criterion = { version = "0.4", features = ["async_std"] }
5857
pretty-bytes = "0.2.2"
5958
duplexify = "1.1.0"
6059
sluice = "0.5.4"
6160
futures = "0.3.13"
62-
log = "0.4"
6361
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "fmt"] }
6462
tracing-tree = "0.4.0"
6563
tokio-util = { version = "0.7.14", features = ["compat"] }

benches/pipe.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
#[path = "../src/test_utils.rs"]
2+
mod test_utils;
13
use async_std::task;
24
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
35
use futures::{
46
io::{AsyncRead, AsyncWrite},
57
stream::StreamExt,
68
};
79
use hypercore_protocol::{schema::*, Channel, Duplex, Event, Message, Protocol, ProtocolBuilder};
8-
use log::*;
910
use pretty_bytes::converter::convert as pretty_bytes;
1011
use sluice::pipe::pipe;
1112
use std::{io::Result, time::Instant};
13+
use tracing::{debug, error};
1214

1315
const COUNT: u64 = 1000;
1416
const SIZE: u64 = 100;
1517
const CONNS: u64 = 10;
1618

1719
fn bench_throughput(c: &mut Criterion) {
18-
env_logger::from_env(env_logger::Env::default().default_filter_or("error")).init();
20+
test_utils::log();
1921
let mut group = c.benchmark_group("pipe");
2022
group.sample_size(10);
2123
group.throughput(Throughput::Bytes(SIZE * COUNT * CONNS));

benches/throughput.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[path = "../src/test_utils.rs"]
2+
mod test_utils;
13
use async_std::{
24
net::{Shutdown, TcpListener, TcpStream},
35
task,
@@ -9,16 +11,16 @@ use futures::{
911
stream::{FuturesUnordered, StreamExt},
1012
};
1113
use hypercore_protocol::{schema::*, Channel, Event, Message, ProtocolBuilder};
12-
use log::*;
1314
use std::time::Instant;
15+
use tracing::{debug, info, trace};
1416

1517
const PORT: usize = 11011;
1618
const SIZE: u64 = 1000;
1719
const COUNT: u64 = 200;
1820
const CLIENTS: usize = 1;
1921

2022
fn bench_throughput(c: &mut Criterion) {
21-
env_logger::from_env(env_logger::Env::default().default_filter_or("error")).init();
23+
test_utils::log();
2224
let address = format!("localhost:{}", PORT);
2325

2426
let mut group = c.benchmark_group("throughput");
@@ -67,7 +69,7 @@ criterion_main!(server_benches);
6769

6870
async fn start_server(address: &str) -> futures::channel::oneshot::Sender<()> {
6971
let listener = TcpListener::bind(&address).await.unwrap();
70-
log::info!("listening on {}", listener.local_addr().unwrap());
72+
info!("listening on {}", listener.local_addr().unwrap());
7173
let (kill_tx, mut kill_rx) = futures::channel::oneshot::channel();
7274
task::spawn(async move {
7375
let mut incoming = listener.incoming();

examples/replication.rs

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[path = "../src/test_utils.rs"]
2+
mod test_utils;
13
use anyhow::Result;
24
use async_std::{
35
net::{TcpListener, TcpStream},
@@ -10,13 +12,13 @@ use hypercore::{
1012
Hypercore, HypercoreBuilder, PartialKeypair, RequestBlock, RequestUpgrade, Storage,
1113
VerifyingKey,
1214
};
13-
use std::{collections::HashMap, convert::TryInto, env, fmt::Debug, sync::OnceLock};
14-
use tracing::{error, info};
15+
use std::{collections::HashMap, convert::TryInto, env, fmt::Debug};
16+
use tracing::{error, info, instrument};
1517

1618
use hypercore_protocol::{discovery_key, schema::*, Channel, Event, Message, ProtocolBuilder};
1719

1820
fn main() {
19-
log();
21+
test_utils::log();
2022
if env::args().count() < 3 {
2123
usage();
2224
}
@@ -62,12 +64,11 @@ fn main() {
6264
hypercore_store.add(hypercore_wrapper);
6365
let hypercore_store = Arc::new(hypercore_store);
6466

65-
let result = match mode.as_ref() {
67+
let _ = match mode.as_ref() {
6668
"server" => tcp_server(address, onconnection, hypercore_store).await,
6769
"client" => tcp_client(address, onconnection, hypercore_store).await,
6870
_ => panic!("{:?}", usage()),
6971
};
70-
log_if_error(&result);
7172
});
7273
}
7374

@@ -81,6 +82,7 @@ fn usage() {
8182
// or once when connected (if client).
8283
// Unfortunately, everything that touches the hypercore_store or a hypercore has to be generic
8384
// at the moment.
85+
#[instrument(skip_all, ret)]
8486
async fn onconnection(
8587
stream: TcpStream,
8688
is_initiator: bool,
@@ -123,17 +125,17 @@ struct HypercoreStore {
123125
hypercores: HashMap<String, Arc<HypercoreWrapper>>,
124126
}
125127
impl HypercoreStore {
126-
pub fn new() -> Self {
128+
fn new() -> Self {
127129
let hypercores = HashMap::new();
128130
Self { hypercores }
129131
}
130132

131-
pub fn add(&mut self, hypercore: HypercoreWrapper) {
133+
fn add(&mut self, hypercore: HypercoreWrapper) {
132134
let hdkey = hex::encode(hypercore.discovery_key);
133135
self.hypercores.insert(hdkey, Arc::new(hypercore));
134136
}
135137

136-
pub fn get(&self, discovery_key: &[u8; 32]) -> Option<&Arc<HypercoreWrapper>> {
138+
fn get(&self, discovery_key: &[u8; 32]) -> Option<&Arc<HypercoreWrapper>> {
137139
let hdkey = hex::encode(discovery_key);
138140
self.hypercores.get(&hdkey)
139141
}
@@ -148,7 +150,7 @@ struct HypercoreWrapper {
148150
}
149151

150152
impl HypercoreWrapper {
151-
pub fn from_memory_hypercore(hypercore: Hypercore) -> Self {
153+
fn from_memory_hypercore(hypercore: Hypercore) -> Self {
152154
let key = hypercore.key_pair().public.to_bytes();
153155
HypercoreWrapper {
154156
key,
@@ -157,11 +159,11 @@ impl HypercoreWrapper {
157159
}
158160
}
159161

160-
pub fn key(&self) -> &[u8; 32] {
162+
fn key(&self) -> &[u8; 32] {
161163
&self.key
162164
}
163165

164-
pub fn onpeer(&self, mut channel: Channel) {
166+
fn onpeer(&self, mut channel: Channel) {
165167
let mut peer_state = PeerState::default();
166168
let mut hypercore = self.hypercore.clone();
167169
task::spawn(async move {
@@ -415,32 +417,9 @@ async fn onmessage(
415417
Ok(())
416418
}
417419

418-
#[allow(unused)]
419-
pub fn log() {
420-
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
421-
static START_LOGS: OnceLock<()> = OnceLock::new();
422-
START_LOGS.get_or_init(|| {
423-
tracing_subscriber::fmt()
424-
.with_target(true)
425-
.with_line_number(true)
426-
// print when instrumented funtion enters
427-
.with_span_events(FmtSpan::ENTER | FmtSpan::EXIT)
428-
.with_file(true)
429-
.with_env_filter(EnvFilter::from_default_env()) // Reads `RUST_LOG` environment variable
430-
.without_time()
431-
.init();
432-
});
433-
}
434-
435-
/// Log a result if it's an error.
436-
pub fn log_if_error(result: &Result<()>) {
437-
if let Err(err) = result.as_ref() {
438-
log::error!("error: {}", err);
439-
}
440-
}
441-
442420
/// A simple async TCP server that calls an async function for each incoming connection.
443-
pub async fn tcp_server<F, C>(
421+
#[instrument(skip_all, ret)]
422+
async fn tcp_server<F, C>(
444423
address: String,
445424
onconnection: impl Fn(TcpStream, bool, C) -> F + Send + Sync + Copy + 'static,
446425
context: C,
@@ -450,22 +429,22 @@ where
450429
C: Clone + Send + 'static,
451430
{
452431
let listener = TcpListener::bind(&address).await?;
453-
log::info!("listening on {}", listener.local_addr()?);
432+
tracing::info!("listening on {}", listener.local_addr()?);
454433
let mut incoming = listener.incoming();
455434
while let Some(Ok(stream)) = incoming.next().await {
456435
let context = context.clone();
457436
let peer_addr = stream.peer_addr().unwrap();
458-
log::info!("new connection from {}", peer_addr);
437+
tracing::info!("new connection from {}", peer_addr);
459438
task::spawn(async move {
460-
let result = onconnection(stream, false, context).await;
461-
log_if_error(&result);
462-
log::info!("connection closed from {}", peer_addr);
439+
let _ = onconnection(stream, false, context).await;
440+
tracing::info!("connection closed from {}", peer_addr);
463441
});
464442
}
465443
Ok(())
466444
}
467445

468446
/// A simple async TCP client that calls an async function when connected.
447+
#[instrument(skip_all, ret)]
469448
pub async fn tcp_client<F, C>(
470449
address: String,
471450
onconnection: impl Fn(TcpStream, bool, C) -> F + Send + Sync + Copy + 'static,
@@ -475,8 +454,8 @@ where
475454
F: Future<Output = Result<()>> + Send,
476455
C: Clone + Send + 'static,
477456
{
478-
log::info!("attempting connection to {address}");
457+
tracing::info!("attempting connection to {address}");
479458
let stream = TcpStream::connect(&address).await?;
480-
log::info!("connected to {address}");
459+
tracing::info!("connected to {address}");
481460
onconnection(stream, true, context).await
482461
}

src/test_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
#![allow(dead_code)]
12
use std::{
23
io::{self, ErrorKind},
34
pin::Pin,
45
task::{Context, Poll},
56
};
67

7-
//use async_channel::{unbounded, Receiver, io::Error, Sender};
88
use futures::{
99
channel::mpsc::{unbounded, UnboundedReceiver as Receiver, UnboundedSender as Sender},
10-
Sink, SinkExt, Stream, StreamExt,
10+
Sink, Stream, StreamExt,
1111
};
1212

1313
#[derive(Debug)]
@@ -99,6 +99,7 @@ pub(crate) fn log() {
9999

100100
#[tokio::test]
101101
async fn way_one() {
102+
use futures::SinkExt;
102103
let mut a = Io::default();
103104
let _ = a.send(b"hello".into()).await;
104105
let Some(res) = a.next().await else { panic!() };
@@ -107,6 +108,7 @@ async fn way_one() {
107108

108109
#[tokio::test]
109110
async fn split() {
111+
use futures::SinkExt;
110112
let (mut left, mut right) = (TwoWay::default()).split_sides();
111113

112114
left.send(b"hello".to_vec()).await.unwrap();

tests/js_interop.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[path = "../src/test_utils.rs"]
2+
mod test_utils;
13
use _util::wait_for_localhost_port;
24
use anyhow::Result;
35
use futures::Future;

0 commit comments

Comments
 (0)