From e22153003736b445ae6a8212e0ebc89dd7293d72 Mon Sep 17 00:00:00 2001 From: dianhsu Date: Fri, 29 Dec 2023 23:19:45 +0800 Subject: [PATCH] Add dependencies and execute arguments to Cargo.toml and README.md --- Cargo.lock | 327 ++++++++++++++++++++++++++++++++++---------------- Cargo.toml | 8 +- README.md | 13 +- src/config.rs | 49 ++------ src/main.rs | 64 +++++++--- 5 files changed, 283 insertions(+), 178 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e9bc84..5691c43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[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.6.5" @@ -165,6 +180,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "bumpalo" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" + [[package]] name = "bytes" version = "1.5.0" @@ -182,17 +209,17 @@ dependencies = [ [[package]] name = "ccs" -version = "0.1.3" +version = "0.1.4" dependencies = [ "axum", + "chrono", "clap", - "dotenvy", + "env_logger", "lazy_static", + "log", "serde", "serde_json", "tokio", - "tracing", - "tracing-subscriber", ] [[package]] @@ -201,6 +228,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.48.5", +] + [[package]] name = "clap" version = "4.4.12" @@ -248,10 +289,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" [[package]] -name = "dotenvy" -version = "0.15.7" +name = "core-foundation-sys" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] [[package]] name = "equivalent" @@ -259,6 +313,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "fnv" version = "1.0.7" @@ -402,6 +466,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.1.0" @@ -439,6 +509,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "indexmap" version = "2.1.0" @@ -449,12 +542,32 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "is-terminal" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "itoa" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "js-sys" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -467,6 +580,12 @@ version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +[[package]] +name = "linux-raw-sys" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" + [[package]] name = "lock_api" version = "0.4.11" @@ -483,15 +602,6 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -[[package]] -name = "matchers" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" -dependencies = [ - "regex-automata 0.1.10", -] - [[package]] name = "matchit" version = "0.7.3" @@ -531,13 +641,12 @@ dependencies = [ ] [[package]] -name = "nu-ansi-term" -version = "0.46.0" +name = "num-traits" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" dependencies = [ - "overload", - "winapi", + "autocfg", ] [[package]] @@ -565,12 +674,6 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" -[[package]] -name = "overload" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" - [[package]] name = "parking_lot" version = "0.12.1" @@ -656,7 +759,7 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -667,17 +770,8 @@ checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.3", - "regex-syntax 0.8.2", -] - -[[package]] -name = "regex-automata" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" -dependencies = [ - "regex-syntax 0.6.29", + "regex-automata", + "regex-syntax", ] [[package]] @@ -688,15 +782,9 @@ checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.2", + "regex-syntax", ] -[[package]] -name = "regex-syntax" -version = "0.6.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" - [[package]] name = "regex-syntax" version = "0.8.2" @@ -709,6 +797,19 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustix" +version = "0.38.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "rustversion" version = "1.0.14" @@ -780,15 +881,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -847,13 +939,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" [[package]] -name = "thread_local" -version = "1.1.7" +name = "termcolor" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" dependencies = [ - "cfg-if", - "once_cell", + "winapi-util", ] [[package]] @@ -936,21 +1027,9 @@ checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", "pin-project-lite", - "tracing-attributes", "tracing-core", ] -[[package]] -name = "tracing-attributes" -version = "0.1.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "tracing-core" version = "0.1.32" @@ -958,61 +1037,79 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", - "valuable", ] [[package]] -name = "tracing-log" -version = "0.2.0" +name = "unicode-ident" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" -dependencies = [ - "log", - "once_cell", - "tracing-core", -] +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] -name = "tracing-subscriber" -version = "0.3.18" +name = "utf8parse" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[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.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", + "cfg-if", + "wasm-bindgen-macro", ] [[package]] -name = "unicode-ident" -version = "1.0.12" +name = "wasm-bindgen-backend" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] [[package]] -name = "utf8parse" -version = "0.2.1" +name = "wasm-bindgen-macro" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] [[package]] -name = "valuable" -version = "0.1.0" +name = "wasm-bindgen-macro-support" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] [[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" +name = "wasm-bindgen-shared" +version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" [[package]] name = "winapi" @@ -1030,12 +1127,30 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 62c5a5f..5a9689d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,17 +1,17 @@ [package] name = "ccs" -version = "0.1.3" +version = "0.1.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] axum = "0.7.2" +chrono = "0.4.31" clap = { version = "4.4.12", features = ["derive"] } -dotenvy = "0.15.7" +env_logger = "0.10.1" lazy_static = "1.4.0" +log = "0.4.20" serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" tokio = { version = "1.35.1", features = ["full"] } -tracing = "0.1.40" -tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } diff --git a/README.md b/README.md index 1a58ac5..ed0f0a5 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,12 @@ Need [Competitive Companion](https://github.com/jmerle/competitive-companion) -## Environments +## Execute arguments -| Key | Sample Value | Description | -| ---------------- | -------------------------------------------- | ------------------------------------------------------------------------ | -| `workspace` | `/home/dianhsu/algorithm` | Generate file location. | +| Key | Sample Value | Description | +| ---------------- | -------------------------------------------- | ------------------------------------------------------------------------------- | +| `workspace` | `/home/dianhsu/algorithm` | Generate file location. | | `templates` | `/home/dianhsu/sol.cpp,/home/dianhsu/sol.py` | Template file location, multiple templates path should be seperated by a comma. | -| `port` | `27121` | Listening port of CCS, default is `27121`. | -| `open_by_vscode` | `true` or `false` | Enable open vscode after templates generated. | +| `port` | `27121` | Listening port of CCS, default is `27121`. | +| `open_by_vscode` | `true` or `false` | Enable open vscode after templates generated. | +| `verbose` | | Verbose show logs. | \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 4e965ac..1ee94fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,7 @@ -use std::env; - use clap::Parser; -use dotenvy::dotenv; - #[derive(Debug, Parser)] #[command(author, version, about)] pub struct ServerConfig { - /// store the workspace path #[arg(short, long, default_value = "")] pub workspace: String, @@ -16,47 +11,17 @@ pub struct ServerConfig { pub templates: Vec, /// allow open by vscode - #[arg(short, long, default_value = "false")] + #[arg(short, long, default_value_t = false)] pub open_by_vscode: bool, /// server port - #[arg(short, long, default_value = "27121")] + #[arg(short, long, default_value_t = 27121)] pub port: i32, + + /// verbose mode + #[arg(short, long, default_value_t = false)] + pub verbose: bool, } lazy_static! { - pub static ref SERVER_CONFIG: ServerConfig = { - dotenv().ok(); - let mut args = ServerConfig::parse(); - if args.workspace.is_empty() { - args.workspace = match env::var("WORKSPACE") { - Ok(workspace) => workspace, - Err(_) => String::new(), - }; - } - if args.templates.is_empty() { - args.templates = match env::var("TEMPLATES") { - Ok(templates) => templates - .split(',') - .into_iter() - .map(|s| s.to_string().trim().to_string()) - .filter(|s| !s.is_empty()) - .collect::>(), - Err(_) => Vec::new(), - }; - } - if args.open_by_vscode == false { - args.open_by_vscode = match env::var("OPEN_BY_VSCODE") { - Ok(open_by_vscode) => open_by_vscode == "true", - Err(_) => false, - }; - } - if args.port == 27121 { - args.port = match env::var("PORT") { - Ok(port) => port.parse::().unwrap_or(27121), - Err(_) => 27121, - }; - } - println!("Server config: {:?}", args); - args - }; + pub static ref SERVER_CONFIG: ServerConfig = ServerConfig::parse(); } diff --git a/src/main.rs b/src/main.rs index 6348450..f45d208 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,15 @@ use axum::http::StatusCode; use axum::routing::{get, post}; use axum::{Json, Router}; -use std::fs; use std::path::Path; +use tokio::fs; mod config; mod model; mod template; use config::SERVER_CONFIG; +use log; +use log::LevelFilter; +use std::io::Write; #[macro_use] extern crate lazy_static; async fn main_post(Json(task): Json) -> StatusCode { @@ -17,15 +20,15 @@ async fn main_post(Json(task): Json) -> StatusCode { let path_str = match path.to_str() { Some(path_str) => path_str, None => { - println!("Failed to convert path to string: {:?}", path); + log::error!("Failed to convert path to string: {:?}", path); return StatusCode::INTERNAL_SERVER_ERROR; } }; if !path.exists() { - match fs::create_dir_all(path.clone()) { + match fs::create_dir_all(path.clone()).await { Ok(_) => {} Err(_) => { - println!("Failed to create directory: {}", path_str); + log::error!("Failed to create directory: {}", path_str); return StatusCode::INTERNAL_SERVER_ERROR; } } @@ -36,17 +39,17 @@ async fn main_post(Json(task): Json) -> StatusCode { let test = &task.tests[idx]; let input_path = path.join(format!("{:02}.i.txt", idx + 1)); let output_path = path.join(format!("{:02}.o.txt", idx + 1)); - match fs::write(input_path, &test.input) { + match fs::write(input_path, &test.input).await { Ok(_) => {} Err(err) => { - println!("Failed to write input file: {}", err); + log::error!("Failed to write input file: {}", err); return StatusCode::INTERNAL_SERVER_ERROR; } } - match fs::write(output_path, &test.output) { + match fs::write(output_path, &test.output).await { Ok(_) => {} Err(err) => { - println!("Failed to write output file: {}", err); + log::error!("Failed to write output file: {}", err); return StatusCode::INTERNAL_SERVER_ERROR; } } @@ -57,7 +60,7 @@ async fn main_post(Json(task): Json) -> StatusCode { let src_filename = match source_path.file_name() { Some(filename) => filename, None => { - println!("Failed to get file name: {:?}", source_path); + log::error!("Failed to get file name: {:?}", source_path); return StatusCode::INTERNAL_SERVER_ERROR; } }; @@ -66,7 +69,7 @@ async fn main_post(Json(task): Json) -> StatusCode { let destination = match destination_path.to_str() { Some(destination) => destination, None => { - println!("Failed to convert path to string: {:?}", destination_path); + log::error!("Failed to convert path to string: {:?}", destination_path); return StatusCode::INTERNAL_SERVER_ERROR; } }; @@ -80,30 +83,51 @@ async fn main_post(Json(task): Json) -> StatusCode { match command.spawn() { Ok(_) => {} Err(_) => { - println!("Failed to open file by vscode: {}", destination); + log::error!("Failed to open file by vscode: {}", destination); } } } - #[cfg(target_os = "linux")] + #[cfg(not(target_os = "windows"))] { let mut command = std::process::Command::new("code"); command.arg(destination); match command.spawn() { Ok(_) => {} Err(_) => { - println!("Failed to open file by vscode: {}", destination); + log::error!("Failed to open file by vscode: {}", destination); } } } } } - println!("Problem created: {}", path_str); + log::info!("Problem created: {}", path_str); return StatusCode::OK; } #[tokio::main] async fn main() { - println!("Server started."); - tracing_subscriber::fmt::init(); + env_logger::builder() + .format(|buf, record| { + writeln!( + buf, + "[{} {}:{}] [{}] - {}", + chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"), + record.file().unwrap_or("unknown"), + record.line().unwrap_or(0), + record.level(), + record.args() + ) + }) + .filter( + Some("ccs"), + if SERVER_CONFIG.verbose { + LevelFilter::Debug + } else { + LevelFilter::Info + }, + ) + .write_style(env_logger::WriteStyle::Auto) + .init(); + log::info!("Server started."); let app = Router::new() .route("/", get(|| async { "Hello, World!" })) .route("/", post(main_post)); @@ -111,16 +135,16 @@ async fn main() { match tokio::net::TcpListener::bind(format!("127.0.0.1:{}", SERVER_CONFIG.port)).await { Ok(listener) => listener, Err(err) => { - println!("Failed to bind port: {}", err); + log::error!("Failed to bind port: {}", err); return; } }; match axum::serve(listener, app).await { Ok(_) => { - println!("Server stopped."); + log::info!("Server stopped."); } - Err(_) => { - println!("Server stopped."); + Err(err) => { + log::error!("Failed to serve: {}", err); } } }