add basic infomation log

This commit is contained in:
xfy
2024-04-25 15:28:55 +08:00
parent 46394827c9
commit 4c5aaf781b
4 changed files with 28 additions and 6 deletions

6
src/consts.rs Normal file
View File

@ -0,0 +1,6 @@
use std::env;
pub const NAME: &str = env!("CARGO_PKG_NAME");
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const OS: &str = env::consts::OS;
pub const ARCH: &str = env::consts::ARCH;

View File

@ -5,10 +5,12 @@ use tracing::{debug, info};
use crate::{
config::{init_config, Settings},
consts::{ARCH, NAME, OS, VERSION},
utils::init_logger,
};
mod config;
mod consts;
mod error;
mod service;
mod utils;
@ -19,6 +21,8 @@ async fn main() -> Result<()> {
let settings = init_config().with_context(|| "init config failed")?;
let settings: &'static Settings = Box::leak(Box::new(settings));
debug!("settings {:?}", settings);
info!("{}/{}", NAME, VERSION);
info!("OS: {} {}", OS, ARCH);
let mut servers = settings
.host
@ -26,7 +30,7 @@ async fn main() -> Result<()> {
.map(|host| host.mk_server())
.collect::<JoinSet<_>>();
info!("Server started");
info!("server started");
while let Some(res) = servers.join_next().await {
res??;

View File

@ -1,3 +1,5 @@
use std::time::{self, Instant};
use crate::error::{Error, Result};
use futures_util::{Future, TryStreamExt};
@ -11,7 +13,7 @@ use hyper::{
use hyper_util::rt::TokioIo;
use tokio::{fs::File, net::TcpListener};
use tokio_util::io::ReaderStream;
use tracing::{error, warn};
use tracing::{error, info, warn};
use crate::config::SettingHost;
@ -22,12 +24,15 @@ impl SettingHost {
let addr = format!("{}:{}", self.ip, self.port);
#[allow(unreachable_code)]
async move {
let listener = TcpListener::bind(addr).await?;
let listener = TcpListener::bind(&addr).await?;
info!("host bind on {}", addr);
loop {
let (stream, _) = listener.accept().await?;
let (stream, addr) = listener.accept().await?;
info!("accept from {}", &addr);
let io = TokioIo::new(stream);
let service = move |req| async move {
let start_time = time::Instant::now();
let res = handle_connection(req, self).await;
let response = match res {
Ok(res) => res,
@ -37,6 +42,9 @@ impl SettingHost {
}
_ => todo!(),
};
let end_time = (Instant::now() - start_time).as_micros() as f32;
let end_time = end_time / 1000_f32;
info!("end {} {:.3}ms", addr, end_time);
anyhow::Ok(response)
};

View File

@ -1,9 +1,13 @@
use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter};
use tracing_subscriber::{
fmt::{self},
prelude::*,
registry, EnvFilter,
};
pub fn init_logger() {
let formatting_layer = fmt::layer()
// .pretty()
.with_thread_ids(false)
.with_thread_ids(true)
.with_target(false)
.with_writer(std::io::stdout);