|
1 | | -use crate::{set_last_error, FFIErrorCode}; |
2 | 1 | use std::ffi::CStr; |
3 | 2 | use std::os::raw::c_char; |
| 3 | +use std::path::PathBuf; |
| 4 | +use std::sync::OnceLock; |
| 5 | + |
| 6 | +use crate::{set_last_error, FFIErrorCode}; |
| 7 | +use dash_spv::{LogFileConfig, LoggingConfig}; |
| 8 | + |
| 9 | +/// Static storage for the logging guard to keep it alive for the FFI lifetime. |
| 10 | +/// The guard must remain alive for log flushing to work correctly. |
| 11 | +static LOGGING_GUARD: OnceLock<dash_spv::LoggingGuard> = OnceLock::new(); |
4 | 12 |
|
5 | 13 | /// Initialize logging for the SPV library. |
6 | 14 | /// |
| 15 | +/// # Arguments |
| 16 | +/// - `level`: Log level string (null uses RUST_LOG env var or defaults to INFO). |
| 17 | +/// Valid values: "error", "warn", "info", "debug", "trace" |
| 18 | +/// - `enable_console`: Whether to output logs to console (stderr) |
| 19 | +/// - `log_dir`: Directory for log files (null to disable file logging) |
| 20 | +/// - `max_files`: Maximum archived log files to retain (ignored if log_dir is null) |
| 21 | +/// |
7 | 22 | /// # Safety |
8 | | -/// - `level` may be null or point to a valid, NUL-terminated C string. |
9 | | -/// - If non-null, the pointer must remain valid for the duration of this call. |
| 23 | +/// - `level` and `log_dir` may be null or point to valid, NUL-terminated C strings. |
10 | 24 | #[no_mangle] |
11 | | -pub unsafe extern "C" fn dash_spv_ffi_init_logging(level: *const c_char) -> i32 { |
12 | | - let level_str = if level.is_null() { |
13 | | - "info" |
| 25 | +pub unsafe extern "C" fn dash_spv_ffi_init_logging( |
| 26 | + level: *const c_char, |
| 27 | + enable_console: bool, |
| 28 | + log_dir: *const c_char, |
| 29 | + max_files: usize, |
| 30 | +) -> i32 { |
| 31 | + let level_filter = if level.is_null() { |
| 32 | + None |
14 | 33 | } else { |
15 | 34 | match CStr::from_ptr(level).to_str() { |
16 | | - Ok(s) => s, |
| 35 | + Ok(s) => match s.parse() { |
| 36 | + Ok(lf) => Some(lf), |
| 37 | + Err(_) => { |
| 38 | + set_last_error(&format!( |
| 39 | + "Invalid log level '{}'. Valid: error, warn, info, debug, trace", |
| 40 | + s |
| 41 | + )); |
| 42 | + return FFIErrorCode::InvalidArgument as i32; |
| 43 | + } |
| 44 | + }, |
17 | 45 | Err(e) => { |
18 | 46 | set_last_error(&format!("Invalid UTF-8 in log level: {}", e)); |
19 | 47 | return FFIErrorCode::InvalidArgument as i32; |
20 | 48 | } |
21 | 49 | } |
22 | 50 | }; |
23 | 51 |
|
24 | | - match dash_spv::init_logging(level_str) { |
25 | | - Ok(()) => FFIErrorCode::Success as i32, |
| 52 | + let file_config = if log_dir.is_null() { |
| 53 | + None |
| 54 | + } else { |
| 55 | + match CStr::from_ptr(log_dir).to_str() { |
| 56 | + Ok(s) => Some(LogFileConfig { |
| 57 | + log_dir: PathBuf::from(s), |
| 58 | + max_files, |
| 59 | + }), |
| 60 | + Err(e) => { |
| 61 | + set_last_error(&format!("Invalid UTF-8 in log directory: {}", e)); |
| 62 | + return FFIErrorCode::InvalidArgument as i32; |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + let config = LoggingConfig { |
| 68 | + level: level_filter, |
| 69 | + console: enable_console, |
| 70 | + file: file_config, |
| 71 | + }; |
| 72 | + |
| 73 | + match dash_spv::init_logging(config) { |
| 74 | + Ok(guard) => { |
| 75 | + // Store guard in static to keep it alive for log flushing. |
| 76 | + // OnceLock::set returns Err if already set (first init wins). |
| 77 | + if LOGGING_GUARD.set(guard).is_err() { |
| 78 | + tracing::warn!("Logging already initialized, ignoring subsequent init"); |
| 79 | + } |
| 80 | + FFIErrorCode::Success as i32 |
| 81 | + } |
26 | 82 | Err(e) => { |
27 | 83 | set_last_error(&format!("Failed to initialize logging: {}", e)); |
28 | 84 | FFIErrorCode::RuntimeError as i32 |
|
0 commit comments