Skip to content

Introduce a logging system. #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ path = "src/main.rs"
cast = "0.2.2"
clap = "2.26.0"
either = "1.0.3"
env_logger = "~0.5"
error-chain = "0.11.0"
inflections = "1.1.0"
log = { version = "~0.4", features = ["std"] }
quote = "0.3.15"
svd-parser = "0.6"
syn = "0.11.11"
13 changes: 7 additions & 6 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use either::Either;
use quote::{ToTokens, Tokens};
use crate::svd::{Cluster, ClusterInfo, Defaults, Peripheral, Register};
use syn::{self, Ident};
use log::warn;

use crate::errors::*;
use crate::util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, BITS_PER_BYTE};
Expand Down Expand Up @@ -341,8 +342,8 @@ impl FieldRegions {
r.fields.len() > 1 && (idents.iter().filter(|ident| **ident == r.ident).count() > 1)
})
.inspect(|r| {
eprintln!(
"WARNING: Found type name conflict with region {:?}, renamed to {:?}",
warn!(
"Found type name conflict with region {:?}, renamed to {:?}",
r.ident,
r.shortest_ident()
)
Expand Down Expand Up @@ -384,8 +385,8 @@ fn register_or_cluster_block_stable(
let pad = if let Some(pad) = reg_block_field.offset.checked_sub(offset) {
pad
} else {
eprintln!(
"WARNING {:?} overlaps with another register block at offset {}. \
warn!(
"{:?} overlaps with another register block at offset {}. \
Ignoring.",
reg_block_field.field.ident, reg_block_field.offset
);
Expand Down Expand Up @@ -473,8 +474,8 @@ fn register_or_cluster_block_nightly(
if reg_block_field.offset != region.offset {
// TODO: need to emit padding for this case.
// Happens for freescale_mkl43z4
eprintln!(
"WARNING: field {:?} has different offset {} than its union container {}",
warn!(
"field {:?} has different offset {} than its union container {}",
reg_block_field.field.ident, reg_block_field.offset, region.offset
);
}
Expand Down
55 changes: 48 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
extern crate cast;
extern crate clap;
extern crate either;
extern crate env_logger;
#[macro_use]
extern crate error_chain;
extern crate inflections;
#[macro_use]
extern crate log;
#[macro_use]
extern crate quote;
extern crate svd_parser as svd;
extern crate syn;
Expand Down Expand Up @@ -48,12 +51,25 @@ fn run() -> Result<()> {
.long("nightly")
.help("Enable features only available to nightly rustc"),
)
.arg(
Arg::with_name("log_level")
.long("log")
.short("l")
.help(&format!(
"Choose which messages to log (overrides {})",
env_logger::DEFAULT_FILTER_ENV
))
.takes_value(true)
.possible_values(&["off", "error", "warn", "info", "debug", "trace"])
)
.version(concat!(
env!("CARGO_PKG_VERSION"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
))
.get_matches();

setup_logging(&matches);

let target = matches
.value_of("target")
.map(|s| Target::parse(s))
Expand Down Expand Up @@ -93,21 +109,46 @@ fn run() -> Result<()> {
Ok(())
}

fn setup_logging(matches: &clap::ArgMatches) {
// * Log at info by default.
// * Allow users the option of setting complex logging filters using
// env_logger's `RUST_LOG` environment variable.
// * Override both of those if the logging level is set via the `--log`
// command line argument.
let env = env_logger::Env::default()
.filter_or(env_logger::DEFAULT_FILTER_ENV, "info");
let mut builder = env_logger::Builder::from_env(env);
builder.default_format_timestamp(false);

let log_lvl_from_env =
std::env::var_os(env_logger::DEFAULT_FILTER_ENV).is_some();

if log_lvl_from_env {
log::set_max_level(log::LevelFilter::Trace);
} else {
let level = match matches.value_of("log_level") {
Some(lvl) => lvl.parse().unwrap(),
None => log::LevelFilter::Info,
};
log::set_max_level(level);
builder.filter_level(level);
}

builder.init();
}

fn main() {
if let Err(ref e) = run() {
let stderr = io::stderr();
let mut stderr = stderr.lock();

writeln!(stderr, "error: {}", e).ok();
error!("{}", e);

for e in e.iter().skip(1) {
writeln!(stderr, "caused by: {}", e).ok();
error!("caused by: {}", e);
}

if let Some(backtrace) = e.backtrace() {
writeln!(stderr, "backtrace: {:?}", backtrace).ok();
error!("backtrace: {:?}", backtrace);
} else {
writeln!(stderr, "note: run with `RUST_BACKTRACE=1` for a backtrace").ok();
error!("note: run with `RUST_BACKTRACE=1` for a backtrace")
}

process::exit(1);
Expand Down