sapling/cmds/configlint.rs
Thomas Orozco 53bf2886cc mononoke: connect stdlog and slog
Summary:
This wires up the stdlog crate with our slog output. The upshot is that we can
now run binaries with `RUST_LOG` set and expect it to work.

This is nice because many crates use stdlog (e.g.  Tokio, Hyper), so this is
convenient to get access to their logging.  For example, if you run with
`RUST_LOG=gotham=info,hyper=debug`, then you get debug logs from Hyper and info
logs from Gotham.

The way this works is by registering a stdlog logger that uses the env_logger's
filter (the one that "invented" `RUST_LOG`) to filter logs, and routes them to
slog if they pass. Note that the slog Logger used there doesn't do any
filtering, since we already do it before sending logs there.

One thing to keep in mind is that we should only register the stdlog global
logger once. I've renamed `get_logger` to `init_logging` to make this clearer.
This behavior is similar to what we do with `init_cachelib`. I've updated
callsites accordingly.

Note that we explicitly tell the stdlog framework to ignore anything that we
won't consider for logging. If you don't set `RUST_LOG`, then the default
logging level is `Error`, which means that anything below error that is sent to
stdlog won't even get to out filtering logic (the stdlog macros for logging
check for the global level before actually logging), so this is cheap unless
you do set `RUST_LOG`.

As part of this, I've also updated all our binaries (and therefore, tests) to
use glog for logging. We had been meaning to do this, and it was convenient to
do it here because the other logger factory we were using didn't make it easy
to get a Drain without putting it a Logger.

Reviewed By: ahornby

Differential Revision: D17314200

fbshipit-source-id: 19b5e8edc3bbe7ba02ccec4f1852dc3587373fff
2019-09-12 04:13:11 -07:00

96 lines
2.7 KiB
Rust

// Copyright (c) 2019-present, Facebook, Inc.
// All Rights Reserved.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
use itertools::Itertools;
use std::collections::BTreeMap;
use cmdlib::args;
use failure_ext::{err_msg, Result};
fn main() -> Result<()> {
let app = args::MononokeApp {
hide_advanced_args: true,
};
let matches = app
.build("Lint Mononoke config files")
.version("0.0.0")
.about("Check Mononoke server configs for syntax and sanity.")
.args_from_usage(
r#"
-q --quiet 'Only print errors'
-v --verbose 'Dump content of configs'
"#,
)
.get_matches();
let quiet = matches.is_present("quiet");
let verbose = matches.is_present("verbose");
// Most of the work is done here - this validates that the files are present,
// are correctly formed, and have the right fields (not too many, not too few).
let configs = match args::read_configs(&matches) {
Err(err) => {
eprintln!("Error loading configs: {:#?}", err);
return Err(err);
}
Ok(configs) => configs,
};
if verbose {
println!("Configs:\n{:#?}", configs)
}
// Keep track of what repo ids we've seen
let mut repoids = BTreeMap::<_, Vec<_>>::new();
// Have we seen something suspect?
let mut bad = false;
for (name, config) in &configs.repos {
let (isbad, locality) = match (
config.storage_config.dbconfig.is_local(),
config.storage_config.blobstore.is_local(),
) {
(true, true) => (false, "local"),
(false, false) => (false, "remote"),
(true, false) => (true, "MIXED - local DB, remote blobstore"),
(false, true) => (true, "MIXED - remote DB, local blobstore"),
};
bad |= isbad;
repoids
.entry(config.repoid)
.and_modify(|names| names.push(name.as_str()))
.or_insert(vec![name.as_str()]);
if isbad || !quiet {
println!(
"Repo {}: {} - enabled: {:?} locality: {}",
config.repoid, name, config.enabled, locality
);
}
}
for (id, names) in repoids {
assert!(!names.is_empty());
if names.len() > 1 {
eprintln!(
"ERROR: Repo Id {} used for repos: {}",
id,
names.into_iter().join(", ")
);
bad = true;
}
}
if bad {
Err(err_msg("Anomaly detected"))
} else {
Ok(())
}
}