Logs session end with total bytes transfered

This commit is contained in:
eric
2023-05-28 13:24:42 +08:00
parent 52f7a21939
commit 9a6f28c0bc
2 changed files with 34 additions and 15 deletions

View File

@@ -30,26 +30,28 @@ fn log_request(
handshake_time: Option<u128>,
) {
let hs = handshake_time.map_or("failed".to_string(), |hs| format!("{}ms", hs));
if !*crate::option::LOG_NO_COLOR {
let (network, outbound_tag) = if !*crate::option::LOG_NO_COLOR {
use colored::Colorize;
let network_color = match sess.network {
Network::Tcp => colored::Color::Blue,
Network::Udp => colored::Color::Yellow,
};
info!(
"[{}] [{}] [{}] [{}] {}",
&sess.inbound_tag,
sess.network.to_string().color(network_color),
outbound_tag.color(*outbound_tag_color),
hs,
&sess.destination,
);
(
sess.network.to_string().color(network_color).to_string(),
outbound_tag.color(*outbound_tag_color).to_string(),
)
} else {
info!(
"[{}] [{}] [{}] [{}] {}",
sess.network, &sess.inbound_tag, outbound_tag, hs, &sess.destination,
);
}
(sess.network.to_string(), outbound_tag.to_string())
};
info!(
"[{}] [{}] [{}] [{}] [{}] [{}]",
sess.forwarded_source.unwrap_or_else(|| sess.source.ip()),
network,
&sess.inbound_tag,
outbound_tag,
hs,
&sess.destination,
);
}
pub struct Dispatcher {

View File

@@ -163,6 +163,22 @@ impl Counter {
}
}
#[inline]
fn log_session_end(c: &Counter) {
log::info!(
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [END]",
c.sess
.forwarded_source
.unwrap_or_else(|| c.sess.source.ip()),
c.sess.network,
c.sess.inbound_tag,
c.sess.outbound_tag,
c.sess.destination,
c.bytes_sent(),
c.bytes_recvd(),
);
}
pub struct StatManager {
pub counters: Vec<Counter>,
}
@@ -182,7 +198,8 @@ impl StatManager {
let mut i = 0;
while i < sm.counters.len() {
if sm.counters[i].recv_completed() && sm.counters[i].send_completed() {
sm.counters.swap_remove(i);
let c = sm.counters.swap_remove(i);
log_session_end(&c);
} else {
i += 1;
}