From 5e1fb34e00cd4978635f4572c7fdac5b4f5989e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20F=C3=B6rster?= Date: Sat, 2 May 2020 18:22:24 +0200 Subject: [PATCH] Add file logging option --- Cargo.lock | 71 +++++++------------------------------------------- Cargo.toml | 2 +- src/main.rs | 75 ++++++++++++++++++++++++++++++++++------------------- 3 files changed, 59 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b63c0e33..8bfe3a12e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -218,16 +218,6 @@ dependencies = [ "parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "chrono" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "citeproc" version = "0.0.1" @@ -573,6 +563,14 @@ name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fern" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "fixedbitset" version = "0.1.9" @@ -986,11 +984,6 @@ dependencies = [ "regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "lazy_static" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "lazy_static" version = "1.4.0" @@ -1974,18 +1967,6 @@ name = "static_assertions" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "stderrlog" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "string_cache" version = "0.7.5" @@ -2101,14 +2082,6 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "termcolor" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "termion" version = "1.5.5" @@ -2128,6 +2101,7 @@ dependencies = [ "chashmap 2.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "criterion 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "fern 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures-boxed 0.1.0", "indoc 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2140,7 +2114,6 @@ dependencies = [ "regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)", - "stderrlog 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "texlab-citeproc 0.1.0", "texlab-completion 0.1.0", @@ -2417,15 +2390,6 @@ dependencies = [ "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "thread_local" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "thread_local" version = "1.0.1" @@ -2444,16 +2408,6 @@ dependencies = [ "miniz_oxide 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "time" -version = "0.1.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)", - "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tinytemplate" version = "1.0.3" @@ -2850,7 +2804,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cesu8 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum chashmap 2.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ff41a3c2c1e39921b9003de14bf0439c7b63a9039637c291e1a64925d8ddfa45" -"checksum chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" "checksum citeproc 0.0.1 (git+https://github.com/cormacrelf/citeproc-rs?rev=68c7a7c16088b5d7751641ab37299ad064cdb221)" = "" "checksum citeproc-db 0.1.0 (git+https://github.com/cormacrelf/citeproc-rs?rev=68c7a7c16088b5d7751641ab37299ad064cdb221)" = "" "checksum citeproc-io 0.1.0 (git+https://github.com/cormacrelf/citeproc-rs?rev=68c7a7c16088b5d7751641ab37299ad064cdb221)" = "" @@ -2882,6 +2835,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ena 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8944dc8fa28ce4a38f778bd46bf7d923fe73eed5a439398507246c8e017e6f36" "checksum error-chain 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +"checksum fern 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8c9a4820f0ccc8a7afd67c39a0f1a0f4b07ca1725164271a64939d7aeb9af065" "checksum fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33" "checksum fixedbitset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" @@ -2924,7 +2878,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lalrpop 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "de5e019883a6e9734d093f34216a3857160c6bc2a9a1ec196a177aaa737c74af" "checksum lalrpop-util 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d6e9bc1801eb54529fd6a020aaf9514e8193bb6b42d96d0fe7da99187efa93d" -"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum lexical-core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d7043aa5c05dd34fb73b47acb8c3708eac428de4545ea3682ed2f11293ebd890" "checksum libc 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "eb147597cdf94ed43ab7a9038716637d2d1bf2bc571da995d0028dec06bd3018" @@ -3042,7 +2995,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" -"checksum stderrlog 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "32e5ee9b90a5452c570a0b0ac1c99ae9498db7e56e33d74366de7f2a7add7f25" "checksum string_cache 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "89c058a82f9fd69b1becf8c274f412281038877c553182f1d02eb027045a2d67" "checksum string_cache_codegen 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f45ed1b65bf9a4bf2f7b7dc59212d1926e9eaf00fa998988e420fd124467c6" "checksum string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" @@ -3056,15 +3008,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum tendril 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "707feda9f2582d5d680d733e38755547a3e8fb471e7ba11452ecfd9ce93a5d3b" "checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" -"checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" "checksum termion 1.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c22cec9d8978d906be5ac94bceb5a010d885c626c4c8855721a4dbd20e3ac905" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thiserror 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "54b3d3d2ff68104100ab257bb6bb0cb26c901abe4bd4ba15961f3bf867924012" "checksum thiserror-impl 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972988113b7715266f91250ddb98070d033c62a011fa0fcc57434a649310dd" -"checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14" "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" "checksum tiff 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "002351e428db1eb1d8656d4ca61947c3519ac3191e1c804d4600cd32093b77ad" -"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tinytemplate 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "57a3c6667d3e65eb1bc3aed6fd14011c6cbc3a0665218ab7f5daf040b9ec371a" "checksum titlecase 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f565e410cfc24c2f2a89960b023ca192689d7f77d3f8d4f4af50c2d8affe1117" "checksum tokio 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "7d9c43f1bb96970e153bcbae39a65e249ccb942bd9d36dbdf086024920417c9c" diff --git a/Cargo.toml b/Cargo.toml index 57d0dc929..50108b558 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ doctest = false aovec = "1.1" chashmap = "2.2" clap = "2.33" +fern = "0.6" futures = "0.3" futures-boxed = { path = "crates/futures_boxed" } itertools = "0.9" @@ -30,7 +31,6 @@ petgraph = { version = "0.5", features = ["serde-1"] } regex = "1.3" serde = { version = "1.0", features = ["derive", "rc"] } serde_json = "1.0" -stderrlog = "0.4" tempfile = "3.1" texlab-citeproc = { path = "crates/texlab_citeproc" } texlab-completion = { path = "crates/texlab_completion" } diff --git a/src/main.rs b/src/main.rs index ab3c6fdba..9dd3f26ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ -use clap::{app_from_crate, crate_authors, crate_description, crate_name, crate_version, Arg}; +use clap::{ + app_from_crate, crate_authors, crate_description, crate_name, crate_version, Arg, ArgMatches, +}; use futures::{channel::mpsc, prelude::*}; use jsonrpc::MessageHandler; -use std::{env, error, sync::Arc}; -use stderrlog::{ColorChoice, Timestamp}; +use log::LevelFilter; +use std::{env, error, fs::OpenOptions, sync::Arc}; + use texlab::server::LatexLspServer; use texlab_protocol::{LatexLspClient, LspCodec}; use texlab_tex::DynamicDistribution; @@ -10,13 +13,13 @@ use tokio_util::codec::{FramedRead, FramedWrite}; #[tokio::main] async fn main() -> Result<(), Box> { - let matches = app_from_crate!() + let opts = app_from_crate!() .author("") .arg( Arg::with_name("verbosity") .short("v") .multiple(true) - .help("Increase message verbosity"), + .help("Increase message verbosity (-vvvv for max verbosity)"), ) .arg( Arg::with_name("quiet") @@ -24,30 +27,15 @@ async fn main() -> Result<(), Box> { .short("q") .help("No output printed to stderr"), ) + .arg( + Arg::with_name("log_file") + .long("log-file") + .value_name("FILE") + .help("Send the logs to the given file"), + ) .get_matches(); - stderrlog::new() - .module(module_path!()) - .module("jsonrpc") - .module("texlab-citeproc") - .module("texlab-completion") - .module("texlab-components") - .module("texlab-definition") - .module("texlab-feature") - .module("texlab-folding") - .module("texlab-hover") - .module("texlab-protocol") - .module("texlab-reference") - .module("texlab-rename") - .module("texlab-symbol") - .module("texlab-syntax") - .module("texlab-tex") - .verbosity(matches.occurrences_of("verbosity") as usize) - .quiet(matches.is_present("quiet")) - .timestamp(Timestamp::Off) - .color(ColorChoice::Never) - .init() - .expect("failed to initialize logger"); + setup_logger(opts); let mut stdin = FramedRead::new(tokio::io::stdin(), LspCodec); let (stdout_tx, mut stdout_rx) = mpsc::channel(0); @@ -78,3 +66,36 @@ async fn main() -> Result<(), Box> { Ok(()) } + +fn setup_logger(opts: ArgMatches) { + let verbosity_level = if !opts.is_present("quiet") { + match opts.occurrences_of("verbosity") { + 0 => LevelFilter::Error, + 1 => LevelFilter::Warn, + 2 => LevelFilter::Info, + 3 => LevelFilter::Debug, + _ => LevelFilter::Trace, + } + } else { + LevelFilter::Off + }; + + let logger = fern::Dispatch::new() + .format(|out, message, record| out.finish(format_args!("{} - {}", record.level(), message))) + .level(verbosity_level) + .filter(|metadata| metadata.target() == "jsonrpc" || metadata.target().contains("texlab")) + .chain(std::io::stderr()); + + let logger = match opts.value_of("log_file") { + Some(log_file) => logger.chain( + OpenOptions::new() + .write(true) + .create(true) + .open(log_file) + .expect("failed to open log file"), + ), + None => logger, + }; + + logger.apply().expect("failed to initialize logger"); +}