Skip to content

Commit 1fe3509

Browse files
authored
fix: improve log (#108)
* fix: improve log format * fix: set data channel errors to warn level * fix: make span name clearer
1 parent 8caaf0f commit 1fe3509

5 files changed

Lines changed: 27 additions & 29 deletions

File tree

src/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ async fn do_data_channel_handshake<T: Transport>(
174174
.connector
175175
.connect(&args.remote_addr)
176176
.await
177-
.with_context(|| "Failed to connect to remote_addr")?;
177+
.with_context(|| format!("Failed to connect to {}", &args.remote_addr))?;
178178
T::hint(&conn, args.socket_opts);
179179

180180
Ok(conn)
181181
},
182182
|e, duration| {
183-
warn!("{:?}. Retry in {:?}", e, duration);
183+
warn!("{:#}. Retry in {:?}", e, duration);
184184
},
185185
)
186186
.await?;
@@ -220,7 +220,7 @@ async fn run_data_channel_for_tcp<T: Transport>(
220220

221221
let mut local = TcpStream::connect(local_addr)
222222
.await
223-
.with_context(|| "Failed to connect to local_addr")?;
223+
.with_context(|| format!("Failed to connect to {}", local_addr))?;
224224
let _ = copy_bidirectional(&mut conn, &mut local).await;
225225
Ok(())
226226
}
@@ -295,7 +295,7 @@ async fn run_data_channel_for_udp<T: Transport>(conn: T::Stream, local_addr: &st
295295
));
296296
}
297297
Err(e) => {
298-
error!("{:?}", e);
298+
error!("{:#}", e);
299299
}
300300
}
301301
}
@@ -383,7 +383,7 @@ impl<T: 'static + Transport> ControlChannel<T> {
383383
.transport
384384
.connect(&self.remote_addr)
385385
.await
386-
.with_context(|| format!("Failed to connect to the server: {}", &self.remote_addr))?;
386+
.with_context(|| format!("Failed to connect to {}", &self.remote_addr))?;
387387
T::hint(&conn, SocketOpts::for_control_channel());
388388

389389
// Send hello
@@ -448,7 +448,7 @@ impl<T: 'static + Transport> ControlChannel<T> {
448448
let args = data_ch_args.clone();
449449
tokio::spawn(async move {
450450
if let Err(e) = run_data_channel(args).await.with_context(|| "Failed to run the data channel") {
451-
error!("{:?}", e);
451+
warn!("{:#}", e);
452452
}
453453
}.instrument(Span::current()));
454454
}
@@ -466,7 +466,7 @@ impl<T: 'static + Transport> ControlChannel<T> {
466466
}
467467

468468
impl ControlChannelHandle {
469-
#[instrument(skip_all, fields(service = %service.name))]
469+
#[instrument(name="handle", skip_all, fields(service = %service.name))]
470470
fn new<T: 'static + Transport>(
471471
service: ClientServiceConfig,
472472
remote_addr: String,
@@ -497,10 +497,10 @@ impl ControlChannelHandle {
497497
}
498498

499499
if let Some(duration) = backoff.next_backoff() {
500-
error!("{:?}\n\nRetry in {:?}...", err, duration);
500+
error!("{:#}. Retry in {:?}...", err, duration);
501501
time::sleep(duration).await;
502502
} else {
503-
error!("{:?}. Break", err);
503+
error!("{:#}. Break", err);
504504
}
505505
}
506506
}

src/config_watcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async fn config_watcher(
149149
let _ = fevent_tx.blocking_send(true);
150150
}
151151
}
152-
Err(e) => error!("watch error: {:?}", e),
152+
Err(e) => error!("watch error: {:#}", e),
153153
})?;
154154

155155
watcher.watch(&path, RecursiveMode::NonRecursive)?;
@@ -164,7 +164,7 @@ async fn config_watcher(
164164
let new = match Config::from_file(&path).await.with_context(|| "The changed configuration is invalid. Ignored") {
165165
Ok(v) => v,
166166
Err(e) => {
167-
error!("{:?}", e);
167+
error!("{:#}", e);
168168
// If the config is invalid, just ignore it
169169
continue;
170170
}

src/protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub async fn read_control_cmd<T: AsyncRead + AsyncWrite + Unpin>(
205205
let mut bytes = vec![0u8; PACKET_LEN.c_cmd];
206206
conn.read_exact(&mut bytes)
207207
.await
208-
.with_context(|| "Failed to read control cmd")?;
208+
.with_context(|| "Failed to read cmd")?;
209209
bincode::deserialize(&bytes).with_context(|| "Failed to deserialize control cmd")
210210
}
211211

@@ -215,6 +215,6 @@ pub async fn read_data_cmd<T: AsyncRead + AsyncWrite + Unpin>(
215215
let mut bytes = vec![0u8; PACKET_LEN.d_cmd];
216216
conn.read_exact(&mut bytes)
217217
.await
218-
.with_context(|| "Failed to read data cmd")?;
218+
.with_context(|| "Failed to read cmd")?;
219219
bincode::deserialize(&bytes).with_context(|| "Failed to deserialize data cmd")
220220
}

src/server.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, T: 'static + Transport> Server<'a, T> {
149149
// EMFILE. So sleep for a while and retry
150150
// TODO: Only sleep for EMFILE, ENFILE, ENOMEM, ENOBUFS
151151
if let Some(d) = backoff.next_backoff() {
152-
error!("Failed to accept: {}. Retry in {:?}...", err, d);
152+
error!("Failed to accept: {:#}. Retry in {:?}...", err, d);
153153
time::sleep(d).await;
154154
} else {
155155
// This branch will never be executed according to the current retry policy
@@ -172,11 +172,11 @@ impl<'a, T: 'static + Transport> Server<'a, T> {
172172
let control_channels = self.control_channels.clone();
173173
tokio::spawn(async move {
174174
if let Err(err) = handle_connection(conn, services, control_channels).await {
175-
error!("{:?}", err);
175+
error!("{:#}", err);
176176
}
177-
}.instrument(info_span!("handle_connection", %addr)));
177+
}.instrument(info_span!("connection", %addr)));
178178
}, Err(e) => {
179-
error!("{:?}", e);
179+
error!("{:#}", e);
180180
}
181181
}
182182
},
@@ -369,7 +369,7 @@ where
369369
{
370370
// Create a control channel handle, where the control channel handling task
371371
// and the connection pool task are created.
372-
#[instrument(skip_all, fields(service = %service.name))]
372+
#[instrument(name = "handle", skip_all, fields(service = %service.name))]
373373
fn new(conn: T::Stream, service: ServerServiceConfig) -> ControlChannelHandle<T> {
374374
// Create a shutdown channel
375375
let (shutdown_tx, shutdown_rx) = broadcast::channel::<bool>(1);
@@ -406,7 +406,7 @@ where
406406
.await
407407
.with_context(|| "Failed to run TCP connection pool")
408408
{
409-
error!("{:?}", e);
409+
error!("{:#}", e);
410410
}
411411
}
412412
.instrument(Span::current()),
@@ -422,7 +422,7 @@ where
422422
.await
423423
.with_context(|| "Failed to run TCP connection pool")
424424
{
425-
error!("{:?}", e);
425+
error!("{:#}", e);
426426
}
427427
}
428428
.instrument(Span::current()),
@@ -433,15 +433,14 @@ where
433433
let ch = ControlChannel::<T> {
434434
conn,
435435
shutdown_rx,
436-
service: service.clone(),
437436
data_ch_req_rx,
438437
};
439438

440439
// Run the control channel
441440
tokio::spawn(
442441
async move {
443442
if let Err(err) = ch.run().await {
444-
error!("{:?}", err);
443+
error!("{:#}", err);
445444
}
446445
}
447446
.instrument(Span::current()),
@@ -458,14 +457,13 @@ where
458457
// Control channel, using T as the transport layer. P is TcpStream or UdpTraffic
459458
struct ControlChannel<T: Transport> {
460459
conn: T::Stream, // The connection of control channel
461-
service: ServerServiceConfig, // A copy of the corresponding service config
462460
shutdown_rx: broadcast::Receiver<bool>, // Receives the shutdown signal
463461
data_ch_req_rx: mpsc::UnboundedReceiver<bool>, // Receives visitor connections
464462
}
465463

466464
impl<T: Transport> ControlChannel<T> {
467465
// Run a control channel
468-
#[instrument(skip(self), fields(service = %self.service.name))]
466+
#[instrument(skip_all)]
469467
async fn run(mut self) -> Result<()> {
470468
let cmd = bincode::serialize(&ControlChannelCmd::CreateDataChannel).unwrap();
471469

@@ -476,11 +474,11 @@ impl<T: Transport> ControlChannel<T> {
476474
match val {
477475
Some(_) => {
478476
if let Err(e) = self.conn.write_all(&cmd).await.with_context(||"Failed to write control cmds") {
479-
error!("{:?}", e);
477+
error!("{:#}", e);
480478
break;
481479
}
482480
if let Err(e) = self.conn.flush().await.with_context(|| "Failed to flush control cmds") {
483-
error!("{:?}", e);
481+
error!("{:#}", e);
484482
break;
485483
}
486484
}
@@ -522,7 +520,7 @@ fn tcp_listen_and_send(
522520
let l: TcpListener = match l {
523521
Ok(v) => v,
524522
Err(e) => {
525-
error!("{:?}", e);
523+
error!("{:#}", e);
526524
return;
527525
}
528526
};

src/transport/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl SocketOpts {
110110
if let Err(e) = try_set_tcp_keepalive(conn, keepalive_duration, keepalive_interval)
111111
.with_context(|| "Failed to set keepalive")
112112
{
113-
error!("{:?}", e);
113+
error!("{:#}", e);
114114
}
115115
}
116116

@@ -120,7 +120,7 @@ impl SocketOpts {
120120
.set_nodelay(nodelay)
121121
.with_context(|| "Failed to set nodelay")
122122
{
123-
error!("{:?}", e);
123+
error!("{:#}", e);
124124
}
125125
}
126126
}

0 commit comments

Comments
 (0)