|
| 1 | +//! Tauri shell side of file-based logging. |
| 2 | +//! |
| 3 | +//! Resolves the OpenHuman data directory the same way the core does |
| 4 | +//! (`~/.openhuman` or `OPENHUMAN_WORKSPACE` override) and hands it to |
| 5 | +//! [`openhuman_core::core::logging::init_for_embedded`], which installs a |
| 6 | +//! daily-rotated file appender so packaged GUI builds — where stderr is |
| 7 | +//! invisible — still produce a log users can share for support. |
| 8 | +//! |
| 9 | +//! Both the shell's `log::*` calls (via the `tracing_log::LogTracer` bridge) |
| 10 | +//! and the embedded core's `tracing::*` events funnel into the same file. |
| 11 | +
|
| 12 | +use std::path::PathBuf; |
| 13 | + |
| 14 | +use openhuman_core::core::logging::{self, log_directory}; |
| 15 | + |
| 16 | +/// Initialize logging for the Tauri shell + embedded core. Idempotent and |
| 17 | +/// safe to call from any startup position; the underlying `Once` guard means |
| 18 | +/// the first caller's data dir wins. |
| 19 | +/// |
| 20 | +/// Verbosity defaults to `info` (or `debug` when `OPENHUMAN_VERBOSE=1`); the |
| 21 | +/// `RUST_LOG` env var continues to override both. |
| 22 | +pub fn init() { |
| 23 | + let data_dir = resolve_data_dir(); |
| 24 | + let verbose = std::env::var("OPENHUMAN_VERBOSE") |
| 25 | + .map(|v| v == "1" || v.eq_ignore_ascii_case("true")) |
| 26 | + .unwrap_or(false); |
| 27 | + logging::init_for_embedded(&data_dir, verbose); |
| 28 | +} |
| 29 | + |
| 30 | +/// Resolve the directory used to host `<data_dir>/logs/`. Mirrors the core's |
| 31 | +/// own resolution so log files sit next to `active_user.toml`, the per-user |
| 32 | +/// `users/` tree, and the CEF caches a support engineer would also need. |
| 33 | +/// |
| 34 | +/// If `default_root_openhuman_dir` fails (very unusual — it requires |
| 35 | +/// `dirs::home_dir` to return `None`), falls back to `<temp>/openhuman` |
| 36 | +/// rather than a relative `.openhuman` whose final location depends on the |
| 37 | +/// shell's CWD at launch time. |
| 38 | +fn resolve_data_dir() -> PathBuf { |
| 39 | + if let Ok(workspace) = std::env::var("OPENHUMAN_WORKSPACE") { |
| 40 | + if !workspace.is_empty() { |
| 41 | + return PathBuf::from(workspace); |
| 42 | + } |
| 43 | + } |
| 44 | + openhuman_core::openhuman::config::default_root_openhuman_dir().unwrap_or_else(|err| { |
| 45 | + eprintln!( |
| 46 | + "[file_logging] default_root_openhuman_dir failed ({err}); falling back to temp dir" |
| 47 | + ); |
| 48 | + std::env::temp_dir().join("openhuman") |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(test)] |
| 53 | +mod tests { |
| 54 | + use super::*; |
| 55 | + |
| 56 | + /// Lock around env-var mutation. Cargo runs unit tests in parallel |
| 57 | + /// threads in the same process, so concurrent `set_var` / `remove_var` |
| 58 | + /// can race; the lock keeps the env stable for each test's duration. |
| 59 | + static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn resolve_data_dir_honors_workspace_override() { |
| 63 | + let _guard = ENV_LOCK.lock().unwrap(); |
| 64 | + let prior = std::env::var("OPENHUMAN_WORKSPACE").ok(); |
| 65 | + std::env::set_var("OPENHUMAN_WORKSPACE", "/tmp/openhuman-test-override"); |
| 66 | + let dir = resolve_data_dir(); |
| 67 | + assert_eq!(dir, PathBuf::from("/tmp/openhuman-test-override")); |
| 68 | + match prior { |
| 69 | + Some(v) => std::env::set_var("OPENHUMAN_WORKSPACE", v), |
| 70 | + None => std::env::remove_var("OPENHUMAN_WORKSPACE"), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn resolve_data_dir_ignores_empty_workspace() { |
| 76 | + let _guard = ENV_LOCK.lock().unwrap(); |
| 77 | + let prior = std::env::var("OPENHUMAN_WORKSPACE").ok(); |
| 78 | + std::env::set_var("OPENHUMAN_WORKSPACE", ""); |
| 79 | + // Empty string must NOT short-circuit — fall through to the |
| 80 | + // default resolver so the user's real `~/.openhuman` is used. |
| 81 | + let dir = resolve_data_dir(); |
| 82 | + assert_ne!(dir, PathBuf::from("")); |
| 83 | + assert!(dir.is_absolute(), "expected absolute fallback, got {dir:?}"); |
| 84 | + match prior { |
| 85 | + Some(v) => std::env::set_var("OPENHUMAN_WORKSPACE", v), |
| 86 | + None => std::env::remove_var("OPENHUMAN_WORKSPACE"), |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn logs_folder_path_returns_none_pre_init() { |
| 92 | + // `init()` is `Once`-guarded across the whole process, so in unit |
| 93 | + // tests where the embedded subscriber hasn't been installed, |
| 94 | + // `logs_folder_path` should return `None` rather than a stale path. |
| 95 | + // (When run alongside a test that *did* call `init`, the function |
| 96 | + // is allowed to return Some — assert the type signature only.) |
| 97 | + let result = logs_folder_path(); |
| 98 | + let _: Option<String> = result; |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn reveal_logs_folder_errors_when_uninitialized() { |
| 103 | + // If logging hasn't been initialized, the command must surface a |
| 104 | + // typed error so the UI can show it instead of silently launching |
| 105 | + // an `open` against an empty path. |
| 106 | + if openhuman_core::core::logging::log_directory().is_none() { |
| 107 | + let err = reveal_logs_folder().expect_err("must error pre-init"); |
| 108 | + assert!(err.contains("not initialized"), "unexpected error: {err}"); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// Tauri command — return the absolute path to the active log directory, or |
| 114 | +/// `None` if logging hasn't been initialized in embedded mode (shouldn't |
| 115 | +/// happen at runtime; guard for tests). |
| 116 | +#[tauri::command] |
| 117 | +pub fn logs_folder_path() -> Option<String> { |
| 118 | + log_directory().map(|p| p.display().to_string()) |
| 119 | +} |
| 120 | + |
| 121 | +/// Tauri command — open the platform file manager at the log directory so a |
| 122 | +/// user can grab today's log file and send it to support. |
| 123 | +#[tauri::command] |
| 124 | +pub fn reveal_logs_folder() -> Result<(), String> { |
| 125 | + let dir = log_directory().ok_or_else(|| "log directory not initialized".to_string())?; |
| 126 | + |
| 127 | + #[cfg(target_os = "macos")] |
| 128 | + let result = std::process::Command::new("open").arg(dir).spawn(); |
| 129 | + |
| 130 | + #[cfg(target_os = "windows")] |
| 131 | + let result = std::process::Command::new("explorer").arg(dir).spawn(); |
| 132 | + |
| 133 | + #[cfg(target_os = "linux")] |
| 134 | + let result = std::process::Command::new("xdg-open").arg(dir).spawn(); |
| 135 | + |
| 136 | + result |
| 137 | + .map(|_| ()) |
| 138 | + .map_err(|e| format!("failed to open log directory {}: {e}", dir.display())) |
| 139 | +} |
0 commit comments