From 6a2b51bb946f93dcb13f8d3b12254851fe7e5caa Mon Sep 17 00:00:00 2001 From: DefectingCat Date: Thu, 13 Apr 2023 20:53:48 +0800 Subject: [PATCH] Add logger init --- Cargo.lock | 291 +++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 + config.example.json | 1 + rustfmt.toml | 1 + src/config.rs | 13 +- src/error.rs | 19 +++ src/logger.rs | 68 +++++++++++ src/main.rs | 20 ++- 8 files changed, 406 insertions(+), 10 deletions(-) create mode 100644 rustfmt.toml create mode 100644 src/error.rs diff --git a/Cargo.lock b/Cargo.lock index f24f96e..6ce0de5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.2.6" @@ -51,6 +60,12 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "anyhow" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" + [[package]] name = "autocfg" version = "1.1.0" @@ -63,6 +78,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" + [[package]] name = "bytes" version = "1.4.0" @@ -81,6 +102,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits", + "time", + "wasm-bindgen", + "winapi", +] + [[package]] name = "clap" version = "4.2.1" @@ -114,7 +150,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.14", ] [[package]] @@ -123,6 +159,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "concolor-override" version = "1.0.0" @@ -138,6 +184,56 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + +[[package]] +name = "cxx" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn 2.0.14", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.14", +] + [[package]] name = "env_logger" version = "0.10.0" @@ -199,6 +295,30 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "io-lifetimes" version = "1.0.10" @@ -228,12 +348,30 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.3.1" @@ -273,10 +411,29 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.45.0", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -370,11 +527,14 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" name = "rua-list" version = "0.1.0" dependencies = [ + "anyhow", + "chrono", "clap", "env_logger", "log", "serde", "serde_json", + "thiserror", "tokio", ] @@ -404,6 +564,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" + [[package]] name = "serde" version = "1.0.160" @@ -421,7 +587,7 @@ checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.14", ] [[package]] @@ -466,6 +632,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.14" @@ -486,6 +663,37 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "thiserror" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.14", +] + +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tokio" version = "1.27.0" @@ -513,7 +721,7 @@ checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.14", ] [[package]] @@ -522,18 +730,84 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "utf8parse" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 1.0.109", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" + [[package]] name = "winapi" version = "0.3.9" @@ -565,6 +839,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/Cargo.toml b/Cargo.toml index 01ab1e1..75008a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,6 @@ tokio = { version = "1.27.0", features = ["full"] } serde = { version = "1.0.160", features = ["derive"] } serde_json = "1.0.96" clap = { version = "4.2.1", features = ["derive"] } +thiserror = "1.0.40" +anyhow = "1.0.70" +chrono = "0.4.24" diff --git a/config.example.json b/config.example.json index cc74d31..3c0cf2a 100644 --- a/config.example.json +++ b/config.example.json @@ -1,5 +1,6 @@ { "log_level": "info", + "log_path": "/tmp/rua-list/log/", "port": 3000, "list": { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..3a26366 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +edition = "2021" diff --git a/src/config.rs b/src/config.rs index 00b9db4..a8fcb55 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,7 +12,8 @@ pub struct List { #[derive(Serialize, Deserialize, Debug)] pub struct Config { - pub log_info: Option, + pub log_level: Option, + pub log_path: Option, // Listen port pub port: Option, pub list: List, @@ -22,6 +23,7 @@ impl Config { pub fn build() -> Self { let args = Args::parse(); + // Read config file location from args. let config_path = if let Some(path) = args.config { path } else { @@ -30,12 +32,15 @@ impl Config { let config = fs::read_to_string(config_path).expect("Failed to read config file"); let mut config: Config = serde_json::from_str(&config).expect("Config file format error"); - // Initial default config + // Initial default config. if config.port.is_none() { config.port = Some(3000) } - if config.log_info.is_none() { - config.log_info = Some("info".to_owned()); + if config.log_level.is_none() { + config.log_level = Some("info".to_owned()); + } + if config.log_path.is_none() { + config.log_path = Some(PathBuf::from("/tmp/rua-list/log")) } config diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..66c3966 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,19 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum CandyError { + #[error("can not parse target")] + Parse(String), + + #[error("page not found")] + // NotFound(#[from] io::Error), + NotFound, + + #[error("unknown file type, found file {file:?}")] + UnknownFileType { file: String }, + + // #[error("invalid header (expected {expected:?}, found {found:?})")] + // InvalidHeader { expected: String, found: String }, + #[error("unknown data store error")] + Unknown, +} \ No newline at end of file diff --git a/src/logger.rs b/src/logger.rs index e69de29..bb2eb88 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -0,0 +1,68 @@ +use std::io::Write; +use std::path::PathBuf; + +use crate::config::Config; +use anyhow::Result; +use chrono::Local; +use env_logger::{Builder, Env}; +use tokio::{ + fs::{self, OpenOptions}, + io::AsyncWriteExt, +}; + +pub async fn create_folder(file_path: &PathBuf) -> Result<()> { + if file_path.exists() { + return Ok(()); + } else { + fs::create_dir_all( + file_path + .parent() + .expect("Can not access log parent folder"), + ) + .await?; + } + Ok(()) +} + +pub async fn init_logger(config: &Config) -> Result<()> { + let log_path = if let Some(path) = &config.log_path { + path + } else { + panic!("Can not read log path from config") + }; + let log_level = config.log_level.clone().unwrap(); + + let now = Local::now(); + let formatted = format!("{}.log", now.format("%Y-%m-%d")); + let file_path = PathBuf::from(&log_path).join(formatted); + create_folder(&file_path).await?; + + let env = Env::default().filter_or("RUA_LOG_LEVEL", &log_level); + let mut builder = Builder::from_env(env); + + builder + .format(move |buf, record| { + let formatted = format!("{}", now.format("%Y-%m-%d %H:%M:%S")); + let log = format!("{} - {} - {}", formatted, record.level(), record.args()); + writeln!(buf, "{log}")?; + + let file_path = file_path.clone(); + tokio::spawn(async move { + let mut file = OpenOptions::new() + .write(true) + .read(true) + .create(true) + .append(true) + .open(&file_path) + .await + .expect("Can not write log file"); + file.write_all(log.as_bytes()) + .await + .expect("Can not write log file"); + }); + Ok(()) + }) + .init(); + + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9c47349..c263136 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,26 @@ +use std::process::exit; + +use anyhow::Result; +use log::{error, info}; + use crate::config::Config; mod arg; mod config; +mod error; mod logger; -fn main() { +#[tokio::main] +async fn main() -> Result<()> { let config = Config::build(); - println!("{config:?}") + if let Err(err) = logger::init_logger(&config).await { + error!("Failed to create logger; {}", err.to_string()); + exit(1); + } + + info!("Server starting"); + + println!("{config:?}"); + + Ok(()) } \ No newline at end of file