Skip to content

Commit

Permalink
added args for log init
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed May 1, 2023
1 parent d4df193 commit 52dfa52
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 157 deletions.
2 changes: 1 addition & 1 deletion rust/compiler/src/adlstdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) fn dump_stdlib(opts: &crate::cli::DumpStdlibOpts) -> Result<(), anyho

type Getter = fn(&str) -> Option<EmbeddedFile>;

fn fun_name(mut path: PathBuf, name: Cow<str>, get: Getter) -> Result<(), anyhow::Error> {
fn fun_name(mut path: PathBuf, name: Cow<'_, str>, get: Getter) -> Result<(), anyhow::Error> {
path.push(name.as_ref());
Ok(if let Some(data) = get(name.as_ref()) {
std::fs::create_dir_all(path.parent().unwrap())
Expand Down
43 changes: 41 additions & 2 deletions rust/compiler/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::{path::PathBuf, fmt::Display, collections::HashSet};
use std::{path::PathBuf, fmt::Display};

use anyhow::{anyhow, Error};
use clap::{Args, Parser};
use log::{LevelFilter, Record};
use std::str::FromStr;

use env_logger::{Builder, Env, fmt::Formatter};
use std::io::Write;

use crate::{
adlgen::adlc::packaging::{
GenOutput, ModuleSrc, NpmPackageRef, ReferenceableScopeOption, TsGenRuntime, TsRuntimeOpt,
Expand All @@ -19,9 +23,34 @@ pub mod tsgen;
pub mod verify;
pub mod workspace;

fn init_logger(module: Option<String>, level: Option<LevelFilter>) {

let formatter = |buf: &mut Formatter, record: &Record<'_>| {
let level = buf.default_styled_level(record.level());
let timestamp = buf.timestamp();
let mut location = String::new();
if let (Some(f), Some(l)) = (record.file(), record.line()) {
location.push_str(format!(" {}:{}", f, l).as_str());
}
if let Some(m) = record.module_path() {
location.push_str(format!(" {}", m).as_str());
}
writeln!(buf, "[{timestamp} {level}{location}] {}", record.args())
};

match (&module, level) {
(None, None) => Builder::from_env(Env::default()).format(formatter).init(),
(None, Some(l)) => Builder::from_env(Env::default()).format(formatter).filter_level(l).init(),
(Some(m), None) => Builder::from_env(Env::default()).format(formatter).filter(Some(m.as_str()), log::LevelFilter::Warn).init(),
(Some(m), Some(l)) => Builder::from_env(Env::default()).format(formatter).filter(Some(m.as_str()), l).init(),
}
}

pub fn run_cli() -> i32 {
let cli = Cli::parse();

init_logger(cli.log_filter_module, cli.loglevel);

let r = match cli.command {
Command::Gen(opts) => workspace::workspace(&opts),
Command::Verify(opts) => verify::verify(&opts),
Expand Down Expand Up @@ -91,12 +120,22 @@ pub fn run_cli() -> i32 {
}
}

#[derive(Parser)]
#[derive(Parser, Debug)]
#[command(name = "adlc")]
#[command(author = "Tim Docker")]
#[command(version = "0.1")]
#[command(about = "ADL code generation cli tool", long_about = None)]
struct Cli {
#[arg(short,long,
help = "Set the loglevel. Overrides RUST_LOG if only level is specified.",
long_help = "Set the loglevel. Overrides RUST_LOG if only level is specified, if level and module are specified here or in RUST_LOG, then the result is a combination of the env var and cli args. Possible values [OFF, ERROR, WARN, INFO, DEBUG, TRACE]")]
loglevel: Option<log::LevelFilter>,

#[arg(short = 'm',long,
help = "Set the module to filter. Can be used in conjunction with the env var RUST_LOG.",
)]
log_filter_module: Option<String>,

#[command(subcommand)]
command: Command,
}
Expand Down
1 change: 0 additions & 1 deletion rust/compiler/src/cli/tsgen/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ impl TsGenVisitor<'_> {
let path = if !self.opts.generate_transitive && npm_pkg2 != None && npm_pkg2 != npm_pkg {
npm_pkg_import(npm_pkg2.unwrap(), scoped_name.module_name.clone())
} else {
println!("!!!{:?} {:?}", npm_pkg2, self.npm_pkg.clone());
let same_pkg = npm_pkg2 == self.npm_pkg.clone();

rel_import(same_pkg, &self.module.name, &scoped_name.module_name)
Expand Down
Loading

0 comments on commit 52dfa52

Please sign in to comment.