diff --git a/Cargo.lock b/Cargo.lock index 11f6bbb..6366fba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,6 +170,19 @@ dependencies = [ "pin-project 0.4.23", ] +[[package]] +name = "actix-slog" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c697a62a2f51c5c26af6b1dded0622f15bec690da191615947e0c1b2b7b75198" +dependencies = [ + "actix-web", + "chrono", + "futures", + "pin-project 0.4.23", + "slog", +] + [[package]] name = "actix-testing" version = "1.0.0" @@ -196,7 +209,7 @@ dependencies = [ "lazy_static", "log", "num_cpus", - "parking_lot 0.10.0", + "parking_lot", "threadpool", ] @@ -336,6 +349,12 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff" +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + [[package]] name = "arrayvec" version = "0.4.12" @@ -345,6 +364,12 @@ dependencies = [ "nodrop", ] +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "async-trait" version = "0.1.40" @@ -447,6 +472,17 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "constant_time_eq", +] + [[package]] name = "block-buffer" version = "0.9.0" @@ -541,13 +577,15 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "chrono" -version = "0.4.10" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31850b4a4d6bae316f7a09e691c944c28299298837edc0a03f755618c23cbc01" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" dependencies = [ + "libc", "num-integer", "num-traits", - "time 0.1.42", + "time 0.1.44", + "winapi 0.3.8", ] [[package]] @@ -574,21 +612,18 @@ dependencies = [ "bitflags", ] -[[package]] -name = "cloudabi" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4344512281c643ae7638bbabc3af17a11307803ec8f0fcad9fae512a8bf36467" -dependencies = [ - "bitflags", -] - [[package]] name = "const_fn" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce90df4c658c62f12d78f7508cf92f9173e5184a539c10bfe54a3107b3ffd0f2" +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + [[package]] name = "cookie" version = "0.14.2" @@ -637,6 +672,27 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87" +dependencies = [ + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + [[package]] name = "derive_more" version = "0.99.2" @@ -657,6 +713,27 @@ dependencies = [ "generic-array", ] +[[package]] +name = "dirs" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" +dependencies = [ + "cfg-if", + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" +dependencies = [ + "libc", + "redox_users", + "winapi 0.3.8", +] + [[package]] name = "discard" version = "1.0.4" @@ -867,7 +944,7 @@ checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.9.0+wasi-snapshot-preview1", ] [[package]] @@ -927,14 +1004,13 @@ name = "hoc" version = "0.14.3" dependencies = [ "actix-rt", + "actix-slog", "actix-web", "badge", "bytes 0.6.0", "futures", "git2", "lazy_static", - "log", - "log4rs", "number_prefix", "openssl-probe", "reqwest", @@ -942,6 +1018,10 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "slog", + "slog-async", + "slog-atomic", + "slog-term", "structopt", "tempfile", "vergen", @@ -984,15 +1064,6 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "hyper" version = "0.13.4" @@ -1011,7 +1082,7 @@ dependencies = [ "log", "net2", "pin-project 0.4.23", - "time 0.1.42", + "time 0.1.44", "tokio", "tower-service", "want", @@ -1050,12 +1121,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "instant" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" - [[package]] name = "iovec" version = "0.1.4" @@ -1144,7 +1209,7 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14" dependencies = [ - "arrayvec", + "arrayvec 0.4.12", "cfg-if", "rustc_version", "ryu", @@ -1212,15 +1277,6 @@ dependencies = [ "scopeguard", ] -[[package]] -name = "lock_api" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28247cc5a5be2f05fbcd76dd0cf2c7d3b5400cb978a28042abcd4fa0b3f8261c" -dependencies = [ - "scopeguard", -] - [[package]] name = "log" version = "0.4.11" @@ -1228,38 +1284,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ "cfg-if", - "serde", -] - -[[package]] -name = "log-mdc" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7" - -[[package]] -name = "log4rs" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1e1ad45e4584824d760c35d71868dd7e6e5acd8f5195a9573743b369fc86cd6" -dependencies = [ - "arc-swap", - "chrono", - "flate2", - "fnv", - "humantime", - "libc", - "log", - "log-mdc", - "parking_lot 0.11.0", - "serde", - "serde-value", - "serde_derive", - "serde_json", - "serde_yaml", - "thread-id", - "typemap", - "winapi 0.3.8", ] [[package]] @@ -1277,6 +1301,12 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "md5" version = "0.7.0" @@ -1492,15 +1522,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "ordered-float" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18869315e81473c951eb56ad5558bbc56978562d3ecfb87abb7a1e944cea4518" -dependencies = [ - "num-traits", -] - [[package]] name = "owned_ttf_parser" version = "0.6.0" @@ -1516,19 +1537,8 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" dependencies = [ - "lock_api 0.3.3", - "parking_lot_core 0.7.0", -] - -[[package]] -name = "parking_lot" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733" -dependencies = [ - "instant", - "lock_api 0.4.1", - "parking_lot_core 0.8.0", + "lock_api", + "parking_lot_core", ] [[package]] @@ -1538,22 +1548,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7582838484df45743c8434fbff785e8edf260c28748353d44bc0da32e0ceabf1" dependencies = [ "cfg-if", - "cloudabi 0.0.3", - "libc", - "redox_syscall", - "smallvec", - "winapi 0.3.8", -] - -[[package]] -name = "parking_lot_core" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" -dependencies = [ - "cfg-if", - "cloudabi 0.1.0", - "instant", + "cloudabi", "libc", "redox_syscall", "smallvec", @@ -1744,6 +1739,17 @@ version = "0.1.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" +[[package]] +name = "redox_users" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + [[package]] name = "regex" version = "1.3.4" @@ -1829,6 +1835,18 @@ dependencies = [ "nom 5.1.0", ] +[[package]] +name = "rust-argon2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dab61250775933275e84053ac235621dfb739556d5c54a2f2e9313b7cf43a19" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + [[package]] name = "rustc-demangle" version = "0.1.16" @@ -1921,16 +1939,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-value" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a65a7291a8a568adcae4c10a677ebcedbc6c9cec91c054dee2ce40b0e3290eb" -dependencies = [ - "ordered-float", - "serde", -] - [[package]] name = "serde_derive" version = "1.0.117" @@ -1965,18 +1973,6 @@ dependencies = [ "url", ] -[[package]] -name = "serde_yaml" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "691b17f19fc1ec9d94ec0b5864859290dff279dbd7b03f017afda54eb36c3c35" -dependencies = [ - "dtoa", - "linked-hash-map", - "serde", - "yaml-rust", -] - [[package]] name = "sha-1" version = "0.9.1" @@ -2012,6 +2008,47 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "slog" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cc9c640a4adbfbcc11ffb95efe5aa7af7309e002adab54b185507dbf2377b99" + +[[package]] +name = "slog-async" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b3336ce47ce2f96673499fc07eb85e3472727b9a7a2959964b002c2ce8fbbb" +dependencies = [ + "crossbeam-channel", + "slog", + "take_mut", + "thread_local", +] + +[[package]] +name = "slog-atomic" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87803759296346553aabece939e9ff174e7fc7ce2406c1fbd235b0c0a322523a" +dependencies = [ + "arc-swap", + "slog", +] + +[[package]] +name = "slog-term" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab1d807cf71129b05ce36914e1dbb6fbfbdecaf686301cb457f4fa967f9f5b6" +dependencies = [ + "atty", + "chrono", + "slog", + "term", + "thread_local", +] + [[package]] name = "smallvec" version = "1.4.1" @@ -2149,6 +2186,12 @@ dependencies = [ "syn", ] +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + [[package]] name = "tempfile" version = "3.1.0" @@ -2163,6 +2206,16 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "term" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0863a3345e70f61d613eab32ee046ccd1bcc5f9105fe402c61fcd0c13eeb8b5" +dependencies = [ + "dirs", + "winapi 0.3.8", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -2192,17 +2245,6 @@ dependencies = [ "syn", ] -[[package]] -name = "thread-id" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fbf4c9d56b320106cd64fd024dadfa0be7cb4706725fc44a7d7ce952d820c1" -dependencies = [ - "libc", - "redox_syscall", - "winapi 0.3.8", -] - [[package]] name = "thread_local" version = "1.0.1" @@ -2223,12 +2265,12 @@ dependencies = [ [[package]] name = "time" -version = "0.1.42" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", - "redox_syscall", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi 0.3.8", ] @@ -2350,12 +2392,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" -[[package]] -name = "traitobject" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" - [[package]] name = "trust-dns-proto" version = "0.19.5" @@ -2408,15 +2444,6 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc" -[[package]] -name = "typemap" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "653be63c80a3296da5551e1bfd2cca35227e13cdd08c6668903ae2f4f77aa1f6" -dependencies = [ - "unsafe-any", -] - [[package]] name = "typenum" version = "1.12.0" @@ -2468,15 +2495,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" -[[package]] -name = "unsafe-any" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30360d7979f5e9c6e6cea48af192ea8fab4afb3cf72597154b8f08935bc9c7f" -dependencies = [ - "traitobject", -] - [[package]] name = "url" version = "2.1.1" @@ -2538,6 +2556,12 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasm-bindgen" version = "0.2.58" @@ -2720,12 +2744,3 @@ dependencies = [ "winapi 0.2.8", "winapi-build", ] - -[[package]] -name = "yaml-rust" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d" -dependencies = [ - "linked-hash-map", -] diff --git a/Cargo.toml b/Cargo.toml index 2e1f58d..b2e905e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,22 +6,25 @@ edition = "2018" build = "build.rs" [dependencies] +actix-rt = "1.1.1" +actix-slog = "0.2.1" actix-web = "3.1.0" badge = "0.3.0" bytes = "0.6.0" futures = "0.3.7" git2 = "0.13.12" lazy_static = "1.4.0" -log = "0.4.11" -log4rs = "0.13.0" number_prefix = "0.4.0" openssl-probe = "0.1.2" reqwest = "0.10.8" serde = "1.0.117" serde_derive = "1.0.103" serde_json = "1.0.59" +slog = "2.5.2" +slog-async = "2.5.0" +slog-atomic = "3.0.0" +slog-term = "2.6.0" structopt = "0.3.20" -actix-rt = "1.1.1" [build-dependencies] ructe = "0.12.0" diff --git a/src/config.rs b/src/config.rs index 1b82cad..bfff158 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,5 @@ -use crate::{error::Result, statics::OPT}; -use log::LevelFilter; -use log4rs::{ - append::{console::ConsoleAppender, file::FileAppender}, - config::{Appender, Config, Root}, - encode::pattern::PatternEncoder, -}; +use slog::{Drain, Logger}; +use slog_atomic::AtomicSwitch; use std::path::PathBuf; use structopt::StructOpt; @@ -38,33 +33,28 @@ pub(crate) struct Opt { #[structopt(short = "w", long = "workers", default_value = "4")] /// Number of worker threads pub(crate) workers: usize, - #[structopt( - short = "l", - long = "logfile", - parse(from_os_str), - default_value = "./hoc.log" - )] - /// The logfile - pub(crate) logfile: PathBuf, + // #[structopt( + // short = "l", + // long = "logfile", + // parse(from_os_str), + // default_value = "./hoc.log" + // )] + // /// The logfile + // pub(crate) logfile: PathBuf, } -pub(crate) async fn init() -> Result<()> { +pub(crate) fn init() -> Logger { std::env::set_var("RUST_LOG", "actix_web=info,hoc=info"); openssl_probe::init_ssl_cert_env_vars(); - let stdout = ConsoleAppender::builder().build(); - let file = FileAppender::builder() - .encoder(Box::new(PatternEncoder::new("{d} - {m}{n}"))) - .build(&OPT.logfile) - .unwrap(); - let config = Config::builder() - .appender(Appender::builder().build("stdout", Box::new(stdout))) - .appender(Appender::builder().build("file", Box::new(file))) - .build( - Root::builder() - .appender("stdout") - .appender("file") - .build(LevelFilter::Info), - )?; - log4rs::init_config(config)?; - Ok(()) + + let decorator = slog_term::PlainDecorator::new(std::io::stdout()); + let drain = slog_term::FullFormat::new(decorator).build().fuse(); + let drain = slog_async::Async::new(drain).build().fuse(); + let drain = AtomicSwitch::new(drain); + + let root = Logger::root(drain, o!("version" => env!("CARGO_PKG_VERSION"))); + + info!(root, "Logging initialized"); + + root } diff --git a/src/error.rs b/src/error.rs index 76d36a1..afe0a63 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,8 +14,6 @@ pub(crate) enum Error { Git(git2::Error), Internal, Io(std::io::Error), - Log(log::SetLoggerError), - LogBuilder(log4rs::config::Errors), Parse(std::num::ParseIntError), Serial(serde_json::Error), BranchNotFound, @@ -29,8 +27,6 @@ impl fmt::Display for Error { Error::Git(e) => write!(fmt, "Git({})", e), Error::Internal => write!(fmt, "Internal Error"), Error::Io(e) => write!(fmt, "Io({})", e), - Error::Log(e) => write!(fmt, "Log({})", e), - Error::LogBuilder(e) => write!(fmt, "LogBuilder({})", e), Error::Parse(e) => write!(fmt, "Parse({})", e), Error::Serial(e) => write!(fmt, "Serial({})", e), Error::BranchNotFound => write!(fmt, "Repo doesn't have master branch"), @@ -76,12 +72,6 @@ impl From for Error { } } -impl From for Error { - fn from(err: log::SetLoggerError) -> Self { - Error::Log(err) - } -} - impl From for Error { fn from(err: std::io::Error) -> Self { Error::Io(err) @@ -100,12 +90,6 @@ impl From for Error { } } -impl From for Error { - fn from(err: log4rs::config::Errors) -> Self { - Error::LogBuilder(err) - } -} - impl From for Error { fn from(err: std::num::ParseIntError) -> Self { Error::Parse(err) diff --git a/src/main.rs b/src/main.rs index 40c521a..164d551 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,12 @@ extern crate actix_web; #[macro_use] extern crate lazy_static; -#[macro_use] -extern crate log; +// #[macro_use] +// extern crate log; #[macro_use] extern crate serde_derive; +#[macro_use] +extern crate slog; mod cache; mod config; @@ -35,6 +37,7 @@ use actix_web::{ use badge::{Badge, BadgeOptions}; use git2::{BranchType, Repository}; use number_prefix::NumberPrefix; +use slog::Logger; use std::{ borrow::Cow, fs::create_dir_all, @@ -59,6 +62,7 @@ struct GeneratorForm<'a> { pub(crate) struct State { repos: String, cache: String, + logger: Logger, } #[derive(Serialize)] @@ -81,7 +85,13 @@ fn pull(path: impl AsRef) -> Result<()> { Ok(()) } -fn hoc(repo: &str, repo_dir: &str, cache_dir: &str, branch: &str) -> Result<(u64, String, u64)> { +fn hoc( + repo: &str, + repo_dir: &str, + cache_dir: &str, + branch: &str, + logger: &Logger, +) -> Result<(u64, String, u64)> { let repo_dir = format!("{}/{}", repo_dir, repo); let cache_dir = format!("{}/{}.json", cache_dir, repo); let cache_dir = Path::new(&cache_dir); @@ -108,16 +118,16 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str, branch: &str) -> Result<(u64 let cache = CacheState::read_from_file(&cache_dir, branch, &head)?; match &cache { CacheState::Current { count, commits, .. } => { - info!("Using cache for {}", repo_dir); + info!(logger, "Using cache"); return Ok((*count, head, *commits)); } CacheState::Old { head, .. } => { - info!("Updating cache for {}", repo_dir); + info!(logger, "Updating cache"); arg.push(format!("{}..{}", head, branch)); arg_commit_count.push(format!("{}..{}", head, branch)); } CacheState::No | CacheState::NoneForBranch(..) => { - info!("Creating cache for {}", repo_dir); + info!(logger, "Creating cache"); arg.push(branch.to_string()); arg_commit_count.push(branch.to_string()); } @@ -180,13 +190,16 @@ where T: Service, { let data = data.into_inner(); + let logger = state + .logger + .new(o!("service" => T::domain(), "user" => data.0.clone(), "repo" => data.1.clone())); let repo = format!( "{}/{}/{}", T::domain(), data.0.to_lowercase(), data.1.to_lowercase() ); - info!("Deleting cache and repository for {}", repo); + info!(logger, "Deleting cache and repository"); let cache_dir = format!("{}/{}.json", &state.cache, repo); let repo_dir = format!("{}/{}", &state.repos, repo); std::fs::remove_file(&cache_dir).or_else(|e| { @@ -223,6 +236,9 @@ where F: Fn(HocResult) -> Result, { let data = data.into_inner(); + let logger = state + .logger + .new(o!("service" => T::domain(), "user" => data.0.clone(), "repo" => data.1.clone(), "branch" => branch.to_string())); let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase()); let service_path = format!("{}/{}", T::url_path(), repo); let service_url = format!("{}/{}", T::domain(), repo); @@ -232,10 +248,10 @@ where let file = Path::new(&path); if !file.exists() { if !remote_exists { - warn!("Repository does not exist: {}", url); + warn!(logger, "Repository does not exist"); return mapper(HocResult::NotFound); } - info!("Cloning {} for the first time", url); + info!(logger, "Cloning for the first time"); create_dir_all(file)?; let repo = Repository::init_bare(file)?; repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?; @@ -243,7 +259,7 @@ where REPO_COUNT.fetch_add(1, Ordering::Relaxed); } pull(&path)?; - let (hoc, head, commits) = hoc(&service_url, &state.repos, &state.cache, branch)?; + let (hoc, head, commits) = hoc(&service_url, &state.repos, &state.cache, branch, &logger)?; let hoc_pretty = match NumberPrefix::decimal(hoc as f64) { NumberPrefix::Standalone(hoc) => hoc.to_string(), NumberPrefix::Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix), @@ -403,16 +419,19 @@ fn favicon32() -> HttpResponse { HttpResponse::Ok().content_type("image/png").body(FAVICON) } -async fn start_server() -> std::io::Result<()> { +async fn start_server(logger: Logger) -> std::io::Result<()> { let interface = format!("{}:{}", OPT.host, OPT.port); let state = Arc::new(State { repos: OPT.outdir.display().to_string(), cache: OPT.cachedir.display().to_string(), + logger, }); HttpServer::new(move || { App::new() .data(state.clone()) - .wrap(middleware::Logger::default()) + .wrap(actix_slog::StructuredLogger::new( + state.logger.new(o!("log_type" => "access")), + )) .wrap(middleware::NormalizePath::new(TrailingSlash::Trim)) .service(index) .service(web::resource("/tacit-css.min.css").route(web::get().to(css))) @@ -450,6 +469,6 @@ async fn start_server() -> std::io::Result<()> { #[actix_rt::main] async fn main() -> std::io::Result<()> { - config::init().await.unwrap(); - start_server().await + let logger = config::init(); + start_server(logger).await } diff --git a/src/tests.rs b/src/tests.rs index eeef7b0..47c838b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -23,7 +23,11 @@ macro_rules! test_service { let cache_dir = dbg!(tempdir().unwrap()); let repos = format!("{}/", repo_dir.path().display()); let cache = format!("{}/", cache_dir.path().display()); - let state = dbg!(State { repos, cache }); + let state = dbg!(State { + repos, + cache, + logger: crate::config::init(), + }); let mut app = test_app!(state, web::resource($path).to($what::));