Skip to content

Commit

Permalink
fix(log): propagate some errors and avoid unwrapping (#1294)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Jul 1, 2024
1 parent 81676da commit 862c42a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions plugins/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl Builder {
LogTarget::Stderr => fern::Output::from(std::io::stderr()),
LogTarget::Folder(path) => {
if !path.exists() {
fs::create_dir_all(path).unwrap();
fs::create_dir_all(path)?;
}

fern::log_file(get_log_file_path(
Expand All @@ -334,9 +334,12 @@ impl Builder {
.into()
}
LogTarget::LogDir => {
let path = app_handle.path_resolver().app_log_dir().unwrap();
let path = app_handle
.path_resolver()
.app_log_dir()
.ok_or("app_log_dir is None")?;
if !path.exists() {
fs::create_dir_all(&path).unwrap();
fs::create_dir_all(&path)?;
}

fern::log_file(get_log_file_path(
Expand All @@ -358,7 +361,7 @@ impl Builder {
};
let app_handle = app_handle.clone();
tauri::async_runtime::spawn(async move {
app_handle.emit_all("log://log", payload).unwrap();
let _ = app_handle.emit_all("log://log", payload);
});
})
}
Expand Down Expand Up @@ -392,21 +395,20 @@ fn get_log_file_path(
log_name,
timezone_strategy
.get_now()
.format(
&time::format_description::parse(
"[year]-[month]-[day]_[hour]-[minute]-[second]"
)
.unwrap()
)
.unwrap(),
.format(&time::format_description::parse(
"[year]-[month]-[day]_[hour]-[minute]-[second]"
)?)?,
));
if to.is_file() {
// designated rotated log file name already exists
// highly unlikely but defensively handle anyway by adding .bak to filename
let mut to_bak = to.clone();
to_bak.set_file_name(format!(
"{}.bak",
to_bak.file_name().unwrap().to_string_lossy()
to_bak
.file_name()
.map(|n| n.to_string_lossy())
.unwrap_or_default()
));
fs::rename(&to, to_bak)?;
}
Expand Down

0 comments on commit 862c42a

Please sign in to comment.