Skip to content

Commit 862c42a

Browse files
authored
fix(log): propagate some errors and avoid unwrapping (#1294)
1 parent 81676da commit 862c42a

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

plugins/log/src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl Builder {
321321
LogTarget::Stderr => fern::Output::from(std::io::stderr()),
322322
LogTarget::Folder(path) => {
323323
if !path.exists() {
324-
fs::create_dir_all(path).unwrap();
324+
fs::create_dir_all(path)?;
325325
}
326326

327327
fern::log_file(get_log_file_path(
@@ -334,9 +334,12 @@ impl Builder {
334334
.into()
335335
}
336336
LogTarget::LogDir => {
337-
let path = app_handle.path_resolver().app_log_dir().unwrap();
337+
let path = app_handle
338+
.path_resolver()
339+
.app_log_dir()
340+
.ok_or("app_log_dir is None")?;
338341
if !path.exists() {
339-
fs::create_dir_all(&path).unwrap();
342+
fs::create_dir_all(&path)?;
340343
}
341344

342345
fern::log_file(get_log_file_path(
@@ -358,7 +361,7 @@ impl Builder {
358361
};
359362
let app_handle = app_handle.clone();
360363
tauri::async_runtime::spawn(async move {
361-
app_handle.emit_all("log://log", payload).unwrap();
364+
let _ = app_handle.emit_all("log://log", payload);
362365
});
363366
})
364367
}
@@ -392,21 +395,20 @@ fn get_log_file_path(
392395
log_name,
393396
timezone_strategy
394397
.get_now()
395-
.format(
396-
&time::format_description::parse(
397-
"[year]-[month]-[day]_[hour]-[minute]-[second]"
398-
)
399-
.unwrap()
400-
)
401-
.unwrap(),
398+
.format(&time::format_description::parse(
399+
"[year]-[month]-[day]_[hour]-[minute]-[second]"
400+
)?)?,
402401
));
403402
if to.is_file() {
404403
// designated rotated log file name already exists
405404
// highly unlikely but defensively handle anyway by adding .bak to filename
406405
let mut to_bak = to.clone();
407406
to_bak.set_file_name(format!(
408407
"{}.bak",
409-
to_bak.file_name().unwrap().to_string_lossy()
408+
to_bak
409+
.file_name()
410+
.map(|n| n.to_string_lossy())
411+
.unwrap_or_default()
410412
));
411413
fs::rename(&to, to_bak)?;
412414
}

0 commit comments

Comments
 (0)