Skip to content

Commit

Permalink
Add file logging option
Browse files Browse the repository at this point in the history
  • Loading branch information
efoerster committed May 2, 2020
1 parent 5a676d3 commit 5e1fb34
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 89 deletions.
71 changes: 10 additions & 61 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand Down
75 changes: 48 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
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;
use tokio_util::codec::{FramedRead, FramedWrite};

#[tokio::main]
async fn main() -> Result<(), Box<dyn error::Error>> {
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")
.long("quiet")
.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);
Expand Down Expand Up @@ -78,3 +66,36 @@ async fn main() -> Result<(), Box<dyn error::Error>> {

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");
}

0 comments on commit 5e1fb34

Please sign in to comment.