Skip to content

Commit

Permalink
chore: add tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaYa committed Nov 23, 2024
1 parent 7623f20 commit 525f286
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 41 deletions.
103 changes: 100 additions & 3 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ strum = "0.26.3"
image = "0.25.5"
tauri-plugin-store = "2"
xcap = "0.0.14"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"

[features]
default = ["custom-protocol"]
Expand Down
1 change: 0 additions & 1 deletion src-tauri/src/cmd/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub fn show_preview_window(app: AppHandle, path: String) -> Result<String, Strin

#[tauri::command]
pub fn hide_preview_window(app: AppHandle) -> Result<(), String> {
println!("hide_preview_window");
window::hide_preview_window(&app);
Ok(())
}
Expand Down
9 changes: 5 additions & 4 deletions src-tauri/src/cmd/xcreenshot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use chrono::Local;
use tracing::info;
use std::time::Instant;
use xcap::{Monitor, Window};

Expand Down Expand Up @@ -44,7 +45,7 @@ fn window_capture(path: PathBuf) -> Result<String, String> {
continue;
}

println!(
info!(
"Window: {:?} {:?} {:?}",
window.title(),
(window.x(), window.y(), window.width(), window.height()),
Expand All @@ -62,14 +63,14 @@ fn window_capture(path: PathBuf) -> Result<String, String> {

let output_path = path.join(&filename);

println!("保存图片: {}", &output_path.to_str().unwrap());
info!("保存图片: {}", &output_path.to_str().unwrap());

image.save(output_path).unwrap();

i += 1;
}

println!("运行耗时: {:?}", start.elapsed());
info!("运行耗时: {:?}", start.elapsed());

Ok(filename)
}
Expand All @@ -94,7 +95,7 @@ fn monitor_capture(path: PathBuf) -> Result<String, String> {
image.save(output_path.to_str().unwrap()).unwrap();
}

println!("运行耗时: {:?}", start.elapsed());
info!("运行耗时: {:?}", start.elapsed());

Ok(filename)
}
7 changes: 4 additions & 3 deletions src-tauri/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use tauri::Manager;
use tauri_plugin_store::StoreExt;
use tracing::info;

pub fn get_images_dir(app_handle: &tauri::AppHandle, path: String) -> Result<PathBuf, String> {
let store = app_handle
Expand All @@ -11,7 +12,7 @@ pub fn get_images_dir(app_handle: &tauri::AppHandle, path: String) -> Result<Pat
let screenshot_path = store
.get("screenshot_path")
.ok_or_else(|| "Screenshot path not found in settings".to_string())?;
println!("screenshot_path: {:?}", screenshot_path);
info!("screenshot_path: {:?}", screenshot_path);

// get value from Option<JsonValue>
let screenshot_path = screenshot_path
Expand All @@ -27,7 +28,7 @@ pub fn get_images_dir(app_handle: &tauri::AppHandle, path: String) -> Result<Pat
} else {
PathBuf::from(screenshot_path)
};
println!("app_local_data: {:?}", app_local_data);
info!("app_local_data: {:?}", app_local_data);

// 创建 images 文件夹
// path 如果为空,则使用 app_local_data
Expand All @@ -37,7 +38,7 @@ pub fn get_images_dir(app_handle: &tauri::AppHandle, path: String) -> Result<Pat
app_local_data.join(path)
};

println!("images_dir: {:?}", images_dir);
info!("images_dir: {:?}", images_dir);
std::fs::create_dir_all(&images_dir).map_err(|e| e.to_string())?;

// let filename = format!("screenshot_{}.png", Local::now().format("%Y%m%d_%H%M%S"));
Expand Down
13 changes: 7 additions & 6 deletions src-tauri/src/global_shortcut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{str::FromStr, thread::sleep, time::Duration};
use tauri::{plugin::TauriPlugin, Emitter};
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut, ShortcutState};
use tracing::info;

use crate::{platform, window};

Expand All @@ -9,10 +10,10 @@ const DEFUALT_HOTKEY_S: &str = "CmdOrCtrl+Shift+S";
const DEFUALT_HOTKEY_W: &str = "CmdOrCtrl+Shift+W";

pub fn register_global_shortcut(app: &tauri::App) -> anyhow::Result<()> {
println!("Registering global shortcuts");
info!("Registering global shortcuts");
let shortcuts = app.global_shortcut();
if let Err(error) = shortcuts.unregister_all() {
println!("Unable to unregister shortcuts {}", error.to_string());
info!("Unable to unregister shortcuts {}", error.to_string());
}

// capture_screen: ctrl + shift + A
Expand Down Expand Up @@ -43,7 +44,7 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
if shortcut.id == Shortcut::from_str(DEFUALT_HOTKEY_A).unwrap().id {
match event.state() {
ShortcutState::Pressed => {
println!("Capture Screen Pressed!");
info!("Capture Screen Pressed!");
let filename = tauri::async_runtime::block_on(platform::capture_screen(
&app,
"images".to_string(),
Expand All @@ -56,7 +57,7 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
});
}
ShortcutState::Released => {
println!("Capture Screen Released!");
info!("Capture Screen Released!");
}
}
} else if shortcut.id == Shortcut::from_str(DEFUALT_HOTKEY_S).unwrap().id {
Expand All @@ -74,7 +75,7 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
});
}
ShortcutState::Released => {
println!("Capture Select Released!");
info!("Capture Select Released!");
}
}
} else if shortcut.id == Shortcut::from_str(DEFUALT_HOTKEY_W).unwrap().id {
Expand All @@ -92,7 +93,7 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
});
}
ShortcutState::Released => {
println!("Capture Window Released!");
info!("Capture Window Released!");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod global_shortcut;
mod menu;
mod platform;
mod window;
mod xcap_utils;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
Expand Down
6 changes: 6 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt::Layer, layer::SubscriberExt, util::SubscriberInitExt, Layer as _};

fn main() {
let layer = Layer::new().with_filter(LevelFilter::INFO);
tracing_subscriber::registry().with(layer).init();

ddu_lib::run()
}
Loading

0 comments on commit 525f286

Please sign in to comment.