Skip to main content

tauri_plugin_debug_tools/
lib.rs

1use std::sync::Arc;
2use tauri::{
3    plugin::{Builder, TauriPlugin},
4    Manager, Runtime,
5};
6
7mod adapters;
8mod application;
9mod commands;
10mod config;
11mod domain;
12
13pub use config::DebugToolsConfig;
14pub use domain::{ConsoleLogEntry, DebugSnapshot, DomSnapshotResult, WebViewState};
15
16use adapters::{init_tracing, FileSystemRepository};
17use application::{AppendConsoleLogsUseCase, CaptureDebugSnapshotUseCase, SaveDomSnapshotUseCase};
18use config::ConfigError;
19
20pub struct DebugToolsState {
21    pub config: Arc<DebugToolsConfig>,
22    pub repository: Arc<FileSystemRepository>,
23    pub append_logs_use_case: Arc<AppendConsoleLogsUseCase<FileSystemRepository>>,
24    pub save_dom_use_case: Arc<SaveDomSnapshotUseCase<FileSystemRepository>>,
25    pub capture_snapshot_use_case: Arc<CaptureDebugSnapshotUseCase<FileSystemRepository>>,
26    #[allow(dead_code)]
27    tracing_guard: adapters::logging::TracingGuard,
28}
29
30pub fn init<R: Runtime>() -> TauriPlugin<R> {
31    Builder::new("debug-tools")
32        .setup(|app, _api| {
33            let config = Arc::new(
34                DebugToolsConfig::from_app_handle(app.app_handle())
35                    .map_err(|e: ConfigError| e.to_string())?,
36            );
37
38            config.ensure_subdirectories().map_err(|e| e.to_string())?;
39
40            let tracing_guard = init_tracing(config.clone()).map_err(|e| e.to_string())?;
41
42            tracing::info!(
43                log_dir = %config.log_dir.display(),
44                "Debug tools plugin initialized"
45            );
46
47            let app_name = app.package_info().name.clone();
48            let repository = Arc::new(FileSystemRepository::new(config.clone(), app_name));
49
50            let append_logs_use_case = Arc::new(AppendConsoleLogsUseCase::new(repository.clone()));
51            let save_dom_use_case = Arc::new(SaveDomSnapshotUseCase::new(repository.clone()));
52            let capture_snapshot_use_case =
53                Arc::new(CaptureDebugSnapshotUseCase::new(repository.clone()));
54
55            let state = DebugToolsState {
56                config,
57                repository,
58                append_logs_use_case,
59                save_dom_use_case,
60                capture_snapshot_use_case,
61                tracing_guard,
62            };
63
64            app.manage(state);
65
66            let screenshots_plugin = tauri_plugin_screenshots::init();
67            let handle = app.app_handle().clone();
68            std::thread::spawn(move || {
69                if let Err(e) = handle.plugin(screenshots_plugin) {
70                    tracing::error!(error = %e, "Failed to initialize screenshots plugin");
71                }
72            });
73
74            Ok(())
75        })
76        .invoke_handler(tauri::generate_handler![
77            commands::capture_webview_state,
78            commands::get_console_logs,
79            commands::send_debug_command,
80            commands::append_debug_logs,
81            commands::reset_debug_logs,
82            commands::clear_debug_log_files_command,
83            commands::copy_screenshot_to_debug_dir,
84            commands::write_debug_snapshot,
85            commands::capture_dom_snapshot,
86            commands::capture_full_debug_state,
87            commands::get_log_directory,
88        ])
89        .build()
90}