Skip to content

Commit a80a929

Browse files
committed
Don't panic on Logger setup failure
1 parent eae4d06 commit a80a929

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ enum BuildError {
121121
"IOWriteFailed",
122122
"StoragePathAccessFailed",
123123
"WalletSetupFailed",
124+
"LoggerSetupFailed",
124125
};
125126

126127
[Enum]

src/builder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ pub enum BuildError {
8484
StoragePathAccessFailed,
8585
/// We failed to setup the onchain wallet.
8686
WalletSetupFailed,
87+
/// We failed to setup the logger.
88+
LoggerSetupFailed,
8789
}
8890

8991
impl fmt::Display for BuildError {
@@ -97,6 +99,7 @@ impl fmt::Display for BuildError {
9799
Self::IOWriteFailed => write!(f, "Failed to write to store."),
98100
Self::StoragePathAccessFailed => write!(f, "Failed to access the given storage path."),
99101
Self::WalletSetupFailed => write!(f, "Failed to setup onchain wallet."),
102+
Self::LoggerSetupFailed => write!(f, "Failed to setup the logger."),
100103
}
101104
}
102105
}
@@ -377,7 +380,10 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
377380
config.storage_dir_path,
378381
chrono::offset::Local::now().format("%Y_%m_%d")
379382
);
380-
let logger = Arc::new(FilesystemLogger::new(log_file_path.clone(), config.log_level));
383+
let logger = Arc::new(
384+
FilesystemLogger::new(log_file_path.clone(), config.log_level)
385+
.map_err(|_| BuildError::LoggerSetupFailed)?,
386+
);
381387

382388
// Initialize the on-chain wallet and chain access
383389
let seed_bytes = match entropy_source_config {

src/logger.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) struct FilesystemLogger {
1616
}
1717

1818
impl FilesystemLogger {
19-
pub(crate) fn new(file_path: String, level: Level) -> Self {
19+
pub(crate) fn new(file_path: String, level: Level) -> Result<Self, ()> {
2020
if let Some(parent_dir) = Path::new(&file_path).parent() {
2121
fs::create_dir_all(parent_dir).expect("Failed to create log parent directory");
2222

@@ -25,21 +25,17 @@ impl FilesystemLogger {
2525
.create(true)
2626
.append(true)
2727
.open(file_path.clone())
28-
.expect("Failed to open log file");
28+
.map_err(|_| ())?;
2929

3030
// Create a symlink to the current log file, with prior cleanup
3131
let log_file_symlink = parent_dir.join("ldk_node_latest.log");
3232
if log_file_symlink.as_path().exists() && log_file_symlink.as_path().is_symlink() {
33-
fs::remove_file(&log_file_symlink)
34-
.expect("Failed to remove an old symlink for the log file");
33+
fs::remove_file(&log_file_symlink).map_err(|_| ())?;
3534
}
36-
symlink(&file_path, &log_file_symlink).expect(&format!(
37-
"Failed to create symlink for the log file: {:?}",
38-
log_file_symlink
39-
));
35+
symlink(&file_path, &log_file_symlink).map_err(|_| ())?;
4036
}
4137

42-
Self { file_path, level }
38+
Ok(Self { file_path, level })
4339
}
4440
}
4541
impl Logger for FilesystemLogger {

0 commit comments

Comments
 (0)