Skip to content

Commit b9fbd55

Browse files
bartlomiejury
authored andcommitted
feat: log permission access (#2518)
Replaces -D/--log-debug flag with --log-level=debug --log-level=info displays permission access
1 parent 988bcbb commit b9fbd55

File tree

4 files changed

+146
-83
lines changed

4 files changed

+146
-83
lines changed

cli/flags.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
22
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
33
use crate::deno_dir;
4+
use log::Level;
45

56
// Creates vector of strings, Vec<String>
67
macro_rules! svec {
@@ -9,7 +10,7 @@ macro_rules! svec {
910

1011
#[derive(Clone, Debug, PartialEq, Default)]
1112
pub struct DenoFlags {
12-
pub log_debug: bool,
13+
pub log_level: Option<Level>,
1314
pub version: bool,
1415
pub reload: bool,
1516
/// When the `--config`/`-c` flag is used to pass the name, this will be set
@@ -127,10 +128,12 @@ To get help on the another subcommands (run in this case):
127128
128129
deno help run")
129130
.arg(
130-
Arg::with_name("log-debug")
131-
.short("D")
132-
.long("log-debug")
133-
.help("Log debug output")
131+
Arg::with_name("log-level")
132+
.short("L")
133+
.long("log-level")
134+
.help("Set log level")
135+
.takes_value(true)
136+
.possible_values(&["debug", "info"])
134137
.global(true),
135138
).arg(
136139
Arg::with_name("reload")
@@ -409,8 +412,12 @@ fn resolve_paths(paths: Vec<String>) -> Vec<String> {
409412
pub fn parse_flags(matches: &ArgMatches) -> DenoFlags {
410413
let mut flags = DenoFlags::default();
411414

412-
if matches.is_present("log-debug") {
413-
flags.log_debug = true;
415+
if matches.is_present("log-level") {
416+
flags.log_level = match matches.value_of("log-level").unwrap() {
417+
"debug" => Some(Level::Debug),
418+
"info" => Some(Level::Info),
419+
_ => unreachable!(),
420+
};
414421
}
415422
if matches.is_present("version") {
416423
flags.version = true;
@@ -743,11 +750,10 @@ mod tests {
743750
#[test]
744751
fn test_flags_from_vec_2() {
745752
let (flags, subcommand, argv) =
746-
flags_from_vec(svec!["deno", "-r", "-D", "run", "script.ts"]);
753+
flags_from_vec(svec!["deno", "-r", "run", "script.ts"]);
747754
assert_eq!(
748755
flags,
749756
DenoFlags {
750-
log_debug: true,
751757
reload: true,
752758
..DenoFlags::default()
753759
}
@@ -758,19 +764,12 @@ mod tests {
758764

759765
#[test]
760766
fn test_flags_from_vec_3() {
761-
let (flags, subcommand, argv) = flags_from_vec(svec![
762-
"deno",
763-
"run",
764-
"-r",
765-
"-D",
766-
"--allow-write",
767-
"script.ts"
768-
]);
767+
let (flags, subcommand, argv) =
768+
flags_from_vec(svec!["deno", "run", "-r", "--allow-write", "script.ts"]);
769769
assert_eq!(
770770
flags,
771771
DenoFlags {
772772
reload: true,
773-
log_debug: true,
774773
allow_write: true,
775774
..DenoFlags::default()
776775
}
@@ -782,11 +781,10 @@ mod tests {
782781
#[test]
783782
fn test_flags_from_vec_4() {
784783
let (flags, subcommand, argv) =
785-
flags_from_vec(svec!["deno", "-Dr", "run", "--allow-write", "script.ts"]);
784+
flags_from_vec(svec!["deno", "-r", "run", "--allow-write", "script.ts"]);
786785
assert_eq!(
787786
flags,
788787
DenoFlags {
789-
log_debug: true,
790788
reload: true,
791789
allow_write: true,
792790
..DenoFlags::default()
@@ -1179,7 +1177,6 @@ mod tests {
11791177
let (flags, subcommand, argv) = flags_from_vec(svec![
11801178
"deno",
11811179
"-r",
1182-
"-D",
11831180
"--allow-net",
11841181
"run",
11851182
"--allow-read",
@@ -1189,7 +1186,6 @@ mod tests {
11891186
flags,
11901187
DenoFlags {
11911188
reload: true,
1192-
log_debug: true,
11931189
allow_net: true,
11941190
allow_read: true,
11951191
..DenoFlags::default()
@@ -1381,4 +1377,19 @@ mod tests {
13811377
]
13821378
);
13831379
}
1380+
1381+
#[test]
1382+
fn test_flags_from_vec_31() {
1383+
let (flags, subcommand, argv) =
1384+
flags_from_vec(svec!["deno", "--log-level=debug", "script.ts"]);
1385+
assert_eq!(
1386+
flags,
1387+
DenoFlags {
1388+
log_level: Some(Level::Debug),
1389+
..DenoFlags::default()
1390+
}
1391+
);
1392+
assert_eq!(subcommand, DenoSubcommand::Run);
1393+
assert_eq!(argv, svec!["deno", "script.ts"])
1394+
}
13841395
}

cli/main.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ use flags::DenoSubcommand;
5656
use futures::future;
5757
use futures::lazy;
5858
use futures::Future;
59-
use log::{LevelFilter, Metadata, Record};
59+
use log::Level;
60+
use log::Metadata;
61+
use log::Record;
6062
use std::env;
6163

6264
static LOGGER: Logger = Logger;
@@ -333,11 +335,11 @@ fn main() {
333335
v8_set_flags(v8_flags.clone());
334336
}
335337

336-
log::set_max_level(if flags.log_debug {
337-
LevelFilter::Debug
338-
} else {
339-
LevelFilter::Warn
340-
});
338+
let log_level = match flags.log_level {
339+
Some(level) => level,
340+
None => Level::Warn,
341+
};
342+
log::set_max_level(log_level.to_level_filter());
341343

342344
match subcommand {
343345
DenoSubcommand::Bundle => bundle_command(flags, argv),

cli/ops.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use futures::Sink;
4242
use futures::Stream;
4343
use hyper;
4444
use hyper::rt::Future;
45+
use log;
4546
use rand::{thread_rng, Rng};
4647
use remove_dir_all::remove_dir_all;
4748
use std;
@@ -362,14 +363,19 @@ fn op_start(
362363
.clone()
363364
.map(|m| builder.create_string(&m));
364365

366+
let debug_flag = state
367+
.flags
368+
.log_level
369+
.map_or(false, |l| l == log::Level::Debug);
370+
365371
let inner = msg::StartRes::create(
366372
&mut builder,
367373
&msg::StartResArgs {
368374
cwd: Some(cwd_off),
369375
pid: std::process::id(),
370376
argv: Some(argv_off),
371377
main_module,
372-
debug_flag: state.flags.log_debug,
378+
debug_flag,
373379
version_flag: state.flags.version,
374380
v8_version: Some(v8_version_off),
375381
deno_version: Some(deno_version_off),

0 commit comments

Comments
 (0)