Skip to content
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

Make logging to file opt-in #1946

Merged
merged 3 commits into from
Dec 10, 2023
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
23 changes: 23 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions changelog/changed-log-to-file-opt-in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Logging to file is now off by default. Use `--log-to-folder` or `--log-file` to enable.
2 changes: 2 additions & 0 deletions probe-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ cli = [
"dep:terminal_size",
"dep:termtree",
"dep:time",
"dep:tracing-appender",
"dep:tracing-subscriber",
"dep:git-version",
"dep:serde_json",
Expand Down Expand Up @@ -184,6 +185,7 @@ tracing-subscriber = { version = "0.3.18", features = [
"env-filter",
"json",
], optional = true }
tracing-appender = { version = "0.2.3", optional = true }
ratatui = { version = "0.24.0", default-features = false, features = [
"crossterm",
], optional = true }
Expand Down
13 changes: 9 additions & 4 deletions probe-rs/src/architecture/riscv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,15 @@ impl<'probe> CoreInterface for Riscv32<'probe> {
// write 1 to the haltreq register, which is part
// of the dmcontrol register

tracing::debug!(
"Before requesting halt, the Dmcontrol register value was: {:?}",
self.interface.read_dm_register::<Dmcontrol>()?
);
if tracing::enabled!(tracing::Level::DEBUG) {
MabezDev marked this conversation as resolved.
Show resolved Hide resolved
// For some reason inlining this read into the log macro causes it to be printed
// even if debug prints are disabled.
let dmc = self.interface.read_dm_register::<Dmcontrol>()?;
tracing::debug!(
"Before requesting halt, the Dmcontrol register value was: {:?}",
dmc
);
}

let mut dmcontrol = Dmcontrol(0);

Expand Down
62 changes: 43 additions & 19 deletions probe-rs/src/bin/probe-rs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ const MAX_LOG_FILES: usize = 20;
struct Cli {
/// Location for log file
///
/// If no location is specified, the log file will be stored in a default directory.
/// If no location is specified, the behaviour depends on `--log-to-folder`.
#[clap(long, global = true)]
log_file: Option<PathBuf>,
/// Enable logging to the default folder. This option is ignored if `--log-file` is specified.
#[clap(long, global = true)]
log_to_folder: bool,
#[clap(subcommand)]
subcommand: Subcommand,
}
Expand Down Expand Up @@ -269,27 +272,20 @@ fn main() -> Result<()> {
}

let log_path = if let Some(location) = matches.log_file {
location
} else {
Some(location)
} else if matches.log_to_folder {
let location =
default_logfile_location().context("Unable to determine default log file location.")?;
prune_logs(
location
.parent()
.expect("A file parent directory. Please report this as a bug."),
)?;
location
Some(location)
} else {
None
};

let log_file = File::create(&log_path)?;

let file_subscriber = tracing_subscriber::fmt::layer()
.json()
.with_file(true)
.with_line_number(true)
.with_span_events(FmtSpan::FULL)
.with_writer(log_file);

let stdout_subscriber = tracing_subscriber::fmt::layer()
.compact()
.without_time()
Expand All @@ -299,12 +295,38 @@ fn main() -> Result<()> {
.from_env_lossy(),
);

tracing_subscriber::registry()
.with(stdout_subscriber)
.with(file_subscriber)
.init();
let _append_guard = if let Some(ref log_path) = log_path {
let log_file = File::create(log_path)?;

let (file_appender, guard) = tracing_appender::non_blocking::NonBlockingBuilder::default()
.lossy(false)
.buffered_lines_limit(128 * 1024)
.finish(log_file);

let file_subscriber = tracing_subscriber::fmt::layer()
.json()
.with_file(true)
.with_line_number(true)
.with_span_events(FmtSpan::FULL)
.with_writer(file_appender);

tracing::info!("Writing log to {:?}", log_path);
tracing_subscriber::registry()
.with(stdout_subscriber)
.with(file_subscriber)
.init();

Some(guard)
} else {
tracing_subscriber::registry()
.with(stdout_subscriber)
.init();

None
};

if let Some(ref log_path) = log_path {
tracing::info!("Writing log to {:?}", log_path);
}

let result = match matches.subcommand {
Subcommand::DapServer { .. } => unreachable!(), // handled above.
Expand All @@ -326,7 +348,9 @@ fn main() -> Result<()> {
Subcommand::Write(cmd) => cmd.run(&lister),
};

tracing::info!("Wrote log to {:?}", log_path);
if let Some(ref log_path) = log_path {
tracing::info!("Wrote log to {:?}", log_path);
}

result
}