Updated logging to be able to select output folder.

This commit is contained in:
2025-12-04 16:50:42 +01:00
parent ef3fc12cb8
commit 1ca1c2c7b4
6 changed files with 275 additions and 80 deletions

191
Cargo.lock generated
View File

@@ -190,7 +190,7 @@ dependencies = [
"clipboard-win",
"image",
"log",
"objc2 0.6.1",
"objc2 0.6.3",
"objc2-app-kit 0.3.1",
"objc2-core-foundation",
"objc2-core-graphics",
@@ -228,6 +228,29 @@ dependencies = [
"libloading",
]
[[package]]
name = "ashpd"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cbdf310d77fd3aaee6ea2093db7011dc2d35d2eb3481e5607f1f8d942ed99df"
dependencies = [
"async-fs",
"async-net",
"enumflags2",
"futures-channel",
"futures-util",
"rand 0.9.2",
"raw-window-handle",
"serde",
"serde_repr",
"tokio",
"url",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"zbus",
]
[[package]]
name = "async-broadcast"
version = "0.7.2"
@@ -266,6 +289,17 @@ dependencies = [
"slab",
]
[[package]]
name = "async-fs"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5"
dependencies = [
"async-lock",
"blocking",
"futures-lite",
]
[[package]]
name = "async-io"
version = "2.5.0"
@@ -295,6 +329,17 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "async-net"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7"
dependencies = [
"async-io",
"blocking",
"futures-lite",
]
[[package]]
name = "async-process"
version = "2.4.0"
@@ -466,6 +511,7 @@ dependencies = [
"log",
"postcard-rpc",
"postcard-schema",
"rfd",
"simple_logger",
"tokio",
"tokio-serial",
@@ -526,6 +572,15 @@ dependencies = [
"objc2 0.5.2",
]
[[package]]
name = "block2"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5"
dependencies = [
"objc2 0.6.3",
]
[[package]]
name = "blocking"
version = "1.6.2"
@@ -873,7 +928,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec"
dependencies = [
"bitflags 2.9.4",
"objc2 0.6.1",
"block2 0.6.2",
"libc",
"objc2 0.6.3",
]
[[package]]
@@ -1475,7 +1532,7 @@ dependencies = [
"glutin_glx_sys",
"glutin_wgl_sys",
"libloading",
"objc2 0.6.1",
"objc2 0.6.3",
"objc2-app-kit 0.3.1",
"objc2-core-foundation",
"objc2-foundation 0.3.1",
@@ -2359,9 +2416,9 @@ dependencies = [
[[package]]
name = "objc2"
version = "0.6.1"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88c6597e14493ab2e44ce58f2fdecf095a51f12ca57bec060a11c57332520551"
checksum = "b7c2599ce0ec54857b29ce62166b0ed9b4f6f1a70ccc9a71165b6154caca8c05"
dependencies = [
"objc2-encode",
]
@@ -2373,7 +2430,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"libc",
"objc2 0.5.2",
"objc2-core-data",
@@ -2389,7 +2446,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6f29f568bec459b0ddff777cec4fe3fd8666d82d5a40ebd0ff7e66134f89bcc"
dependencies = [
"bitflags 2.9.4",
"objc2 0.6.1",
"block2 0.6.2",
"objc2 0.6.3",
"objc2-core-foundation",
"objc2-core-graphics",
"objc2-foundation 0.3.1",
@@ -2402,7 +2460,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-core-location",
"objc2-foundation 0.2.2",
@@ -2414,7 +2472,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889"
dependencies = [
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-foundation 0.2.2",
]
@@ -2426,7 +2484,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-foundation 0.2.2",
]
@@ -2439,7 +2497,7 @@ checksum = "1c10c2894a6fed806ade6027bcd50662746363a9589d3ec9d9bef30a4e4bc166"
dependencies = [
"bitflags 2.9.4",
"dispatch2",
"objc2 0.6.1",
"objc2 0.6.3",
]
[[package]]
@@ -2450,7 +2508,7 @@ checksum = "989c6c68c13021b5c2d6b71456ebb0f9dc78d752e86a98da7c716f4f9470f5a4"
dependencies = [
"bitflags 2.9.4",
"dispatch2",
"objc2 0.6.1",
"objc2 0.6.3",
"objc2-core-foundation",
"objc2-io-surface",
]
@@ -2461,7 +2519,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80"
dependencies = [
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-foundation 0.2.2",
"objc2-metal",
@@ -2473,7 +2531,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781"
dependencies = [
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-contacts",
"objc2-foundation 0.2.2",
@@ -2492,7 +2550,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"dispatch",
"libc",
"objc2 0.5.2",
@@ -2505,7 +2563,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900831247d2fe1a09a683278e5384cfb8c80c79fe6b166f9d14bfdde0ea1b03c"
dependencies = [
"bitflags 2.9.4",
"objc2 0.6.1",
"objc2 0.6.3",
"objc2-core-foundation",
]
@@ -2516,7 +2574,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7282e9ac92529fa3457ce90ebb15f4ecbc383e8338060960760fa2cf75420c3c"
dependencies = [
"bitflags 2.9.4",
"objc2 0.6.1",
"objc2 0.6.3",
"objc2-core-foundation",
]
@@ -2526,7 +2584,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398"
dependencies = [
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-app-kit 0.2.2",
"objc2-foundation 0.2.2",
@@ -2539,7 +2597,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-foundation 0.2.2",
]
@@ -2551,7 +2609,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-foundation 0.2.2",
"objc2-metal",
@@ -2574,7 +2632,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-cloud-kit",
"objc2-core-data",
@@ -2594,7 +2652,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe"
dependencies = [
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-foundation 0.2.2",
]
@@ -2606,7 +2664,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3"
dependencies = [
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"objc2 0.5.2",
"objc2-core-location",
"objc2-foundation 0.2.2",
@@ -2728,7 +2786,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
dependencies = [
"phf_shared",
"rand",
"rand 0.8.5",
]
[[package]]
@@ -2831,6 +2889,12 @@ dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "pollster"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3"
[[package]]
name = "portable-atomic"
version = "1.11.1"
@@ -2910,6 +2974,15 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "presser"
version = "0.3.1"
@@ -3015,7 +3088,27 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"rand_core",
"rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
"rand_core 0.9.3",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core 0.9.3",
]
[[package]]
@@ -3024,6 +3117,15 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "range-alloc"
version = "0.1.4"
@@ -3104,6 +3206,30 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832"
[[package]]
name = "rfd"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a15ad77d9e70a92437d8f74c35d99b4e4691128df018833e99f90bcd36152672"
dependencies = [
"ashpd",
"block2 0.6.2",
"dispatch2",
"js-sys",
"log",
"objc2 0.6.3",
"objc2-app-kit 0.3.1",
"objc2-core-foundation",
"objc2-foundation 0.3.1",
"pollster",
"raw-window-handle",
"urlencoding",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"windows-sys 0.60.2",
]
[[package]]
name = "rustc-demangle"
version = "0.1.26"
@@ -3638,9 +3764,11 @@ dependencies = [
"libc",
"mio",
"pin-project-lite",
"signal-hook-registry",
"slab",
"socket2",
"tokio-macros",
"tracing",
"windows-sys 0.59.0",
]
@@ -3826,8 +3954,15 @@ dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
"serde",
]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "utf8_iter"
version = "1.0.4"
@@ -4081,7 +4216,7 @@ dependencies = [
"jni",
"log",
"ndk-context",
"objc2 0.6.1",
"objc2 0.6.3",
"objc2-foundation 0.3.1",
"url",
"web-sys",
@@ -4759,7 +4894,7 @@ dependencies = [
"android-activity",
"atomic-waker",
"bitflags 2.9.4",
"block2",
"block2 0.5.1",
"bytemuck",
"calloop",
"cfg_aliases",
@@ -4942,6 +5077,7 @@ dependencies = [
"ordered-stream",
"serde",
"serde_repr",
"tokio",
"tracing",
"uds_windows",
"windows-sys 0.59.0",
@@ -5098,6 +5234,7 @@ dependencies = [
"endi",
"enumflags2",
"serde",
"url",
"winnow",
"zvariant_derive",
"zvariant_utils",

View File

@@ -13,6 +13,7 @@ log = "0.4.27"
simple_logger = "5.0.0"
atomic_float = "1.1.0"
chrono = { version = "0.4.42" }
rfd = { version = "0.16.0", features = ["tokio"] }
[dependencies.bioz-icd-rs]
path = "../bioz-icd-rs"

View File

@@ -12,11 +12,12 @@ use chrono::Local;
use atomic_float::AtomicF32;
use tokio::{sync::mpsc::{Sender}};
use eframe::egui::{self, Button, CollapsingHeader, Color32, ComboBox, DragValue, Id, Key, Label, Layout, Modifiers, TextEdit, Widget};
use eframe::egui::{self, Button, CollapsingHeader, Color32, ComboBox, DragValue, Id, Key, Label, Layout, Modifiers, RichText, TextEdit, Widget};
use egui_plot::{Corner, GridInput, GridMark, Legend, Line, Plot, PlotPoint, Points};
use egui_dock::{DockArea, DockState, Style};
use egui_extras::{TableBuilder, Column};
use crate::logging::LoggingStates;
use crate::plot::{TimeSeriesPlot, BodePlot};
use crate::signals::{LoggingSignal, StartStopSignal};
@@ -67,7 +68,7 @@ pub struct App {
pub measurement_points: Arc<Mutex<MeasurementPointSet>>,
pub periods_per_dft: Arc<Mutex<Option<f32>>>,
pub periods_per_dft_sweep: Arc<Mutex<(Vec<u32>, Option<Vec<f32>>)>>,
pub gui_logging_enabled: Arc<AtomicBool>,
pub gui_logging_state: Arc<Mutex<LoggingStates>>,
log_filename: String,
}
@@ -540,7 +541,7 @@ impl App {
measurement_points,
periods_per_dft,
periods_per_dft_sweep,
gui_logging_enabled: Arc::new(AtomicBool::new(false)),
gui_logging_state: Arc::new(Mutex::new(LoggingStates::Idle)),
log_filename: format!("log_{}.csv", Local::now().format("%Y%m%d"))
};
@@ -619,23 +620,31 @@ impl eframe::App for App {
ui.separator();
let mut gui_logging_enabled = self.gui_logging_enabled.load(Ordering::Relaxed);
if ui.add(egui::Checkbox::new(&mut gui_logging_enabled, "Logging")).changed() {
self.gui_logging_enabled.store(gui_logging_enabled, Ordering::Relaxed);
if gui_logging_enabled {
if let Err(e) = self.log_tx.try_send(LoggingSignal::StartFileLogging(self.log_filename.clone())) {
error!("Failed to send logging signal: {:?}", e);
}
} else {
if let Err(e) = self.log_tx.try_send(LoggingSignal::StopFileLogging) {
error!("Failed to send logging signal: {:?}", e);
}
}
let gui_logging_state = *self.gui_logging_state.lock().unwrap();
let (color, signal) = match gui_logging_state {
LoggingStates::Idle => (Color32::DARK_RED, LoggingSignal::StartFileLogging(self.log_filename.clone())),
LoggingStates::Starting => (Color32::from_rgb(204, 153, 0), LoggingSignal::StopFileLogging),
LoggingStates::Logging => (Color32::DARK_GREEN, LoggingSignal::StopFileLogging),
};
let button = Button::new(
RichText::new("Logging")
.strong()
.color(Color32::WHITE)
).fill(color)
.corner_radius(5.0)
.frame(true);
if ui.add(button).clicked() {
self.log_tx.try_send(signal).unwrap_or_else(|e| {
error!("Failed to send logging toggle signal: {:?}", e);
});
}
ui.separator();
ui.add_enabled_ui(!gui_logging_enabled, |ui| {
ui.add_enabled_ui(gui_logging_state == LoggingStates::Idle, |ui| {
ui.label("Log filename:");
TextEdit::singleline(&mut self.log_filename).desired_width(125.0).id(Id::new("file_name_field")).ui(ui);
});
@@ -644,7 +653,7 @@ impl eframe::App for App {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.scope(|ui| {
let is_connected = self.connected.load(Ordering::Relaxed);
let color = if is_connected { Color32::GREEN } else { Color32::RED };
let color = if is_connected { Color32::DARK_GREEN } else { Color32::DARK_RED };
let tooltip = if is_connected { "Connected" } else { "Disconnected" };
// Allocate a fixed-size rectangle for the LED
@@ -739,17 +748,22 @@ impl eframe::App for App {
// Enable/disable GUI logging
if ctx.input(|i| i.key_pressed(egui::Key::L)) {
let mut gui_logging_enabled = self.gui_logging_enabled.load(Ordering::Relaxed);
gui_logging_enabled = !gui_logging_enabled;
self.gui_logging_enabled.store(gui_logging_enabled, Ordering::Relaxed);
if gui_logging_enabled {
if let Err(e) = self.log_tx.try_send(LoggingSignal::StartFileLogging(self.log_filename.clone())) {
error!("Failed to send logging signal: {:?}", e);
}
} else {
if let Err(e) = self.log_tx.try_send(LoggingSignal::StopFileLogging) {
error!("Failed to send logging signal: {:?}", e);
}
let gui_logging_enabled = *self.gui_logging_state.lock().unwrap();
match gui_logging_enabled {
LoggingStates::Starting => {
// If currently starting, do nothing
return;
},
LoggingStates::Logging => {
if let Err(e) = self.log_tx.try_send(LoggingSignal::StopFileLogging) {
error!("Failed to send logging signal: {:?}", e);
}
},
LoggingStates::Idle => {
if let Err(e) = self.log_tx.try_send(LoggingSignal::StartFileLogging(self.log_filename.clone())) {
error!("Failed to send logging signal: {:?}", e);
}
},
}
}

View File

@@ -43,10 +43,11 @@ async fn main() {
let periods_per_dft = app.periods_per_dft.clone();
let periods_per_dft_sweep = app.periods_per_dft_sweep.clone();
let gui_logging_enabled = app.gui_logging_enabled.clone();
let gui_logging_state_1 = app.gui_logging_state.clone();
let gui_logging_state_2 = app.gui_logging_state.clone();
// Log thread
tokio::spawn(async move { log_data(log_rx).await });
tokio::spawn(async move { log_data(log_rx, gui_logging_state_1).await });
// Execute the runtime in its own thread.
std::thread::spawn(move || {
@@ -62,7 +63,7 @@ async fn main() {
data_frequency_clone,
periods_per_dft,
periods_per_dft_sweep,
gui_logging_enabled,
gui_logging_state_2,
log_tx_clone,
));
});

View File

@@ -15,6 +15,7 @@ use bioz_icd_rs::MeasurementPointSet;
use crate::icd;
use crate::client::WorkbookClient;
use crate::logging::LoggingStates;
use crate::plot::{TimeSeriesPlot, BodePlot};
use crate::signals::{LoggingSignal, StartStopSignal};
@@ -31,7 +32,7 @@ pub async fn communicate_with_hardware(
data_frequency: Arc<AtomicF32>,
periods_per_dft: Arc<Mutex<Option<f32>>>,
periods_per_dft_sweep: Arc<Mutex<(Vec<u32>, Option<Vec<f32>>)>>,
gui_logging_enabled: Arc<AtomicBool>,
gui_logging_state: Arc<Mutex<LoggingStates>>,
log_tx: Sender<LoggingSignal>,
) {
let data_counter = Arc::new(AtomicU32::new(0));
@@ -78,7 +79,7 @@ pub async fn communicate_with_hardware(
let data_counter_clone_single = data_counter_clone.clone();
// Clone log_tx for the task
let gui_logging_enabled_clone = gui_logging_enabled.clone();
let gui_logging_state_clone = gui_logging_state.clone();
let log_tx_clone = log_tx.clone();
let settings_clone = settings.clone();
@@ -91,7 +92,7 @@ pub async fn communicate_with_hardware(
let mut mag_val = data.2.lock().unwrap();
let mut phase_val = data.3.lock().unwrap();
*mag_val = val.magnitude;
*mag_val = val.magnitude;
*phase_val = val.phase;
mag_plot.add(val.magnitude as f64);
@@ -100,11 +101,11 @@ pub async fn communicate_with_hardware(
data_counter_clone_single.fetch_add(1, Ordering::Relaxed);
}
// Send logging signal
if gui_logging_enabled_clone.load(Ordering::Relaxed) {
if *gui_logging_state_clone.lock().unwrap() == LoggingStates::Logging {
let settings = *settings_clone.lock().unwrap();
match settings.frequency {
Some(StartStopSignal::StartSingle(freq, _, _)) => {
if let Err(e) = log_tx_clone.send(LoggingSignal::SingleImpedance(SystemTime::now(), freq, val.magnitude, val.phase)).await {
if let Err(e) = log_tx_clone.try_send(LoggingSignal::SingleImpedance(SystemTime::now(), freq, val.magnitude, val.phase)) {
error!("Failed to send logging signal: {:?}", e);
}
},
@@ -131,7 +132,7 @@ pub async fn communicate_with_hardware(
let data_counter_clone_sweep = data_counter_clone.clone();
// Clone log_tx for the task
let gui_logging_enabled_clone = gui_logging_enabled.clone();
let gui_logging_state_clone = gui_logging_state.clone();
let log_tx_clone = log_tx.clone();
tokio::spawn(async move {
@@ -147,7 +148,7 @@ pub async fn communicate_with_hardware(
bode_plot.update_magnitudes(MeasurementPointSet::Eight, magnitudes.clone());
bode_plot.update_phases(MeasurementPointSet::Eight, phases.clone());
}
if gui_logging_enabled_clone.load(Ordering::Relaxed) {
if *gui_logging_state_clone.lock().unwrap() == LoggingStates::Logging {
if let Err(e) = log_tx_clone.send(LoggingSignal::SweepImpedance(SystemTime::now(), MeasurementPointSet::Eight.values().to_vec(), magnitudes.clone(), phases.clone())).await {
error!("Failed to send logging signal: {:?}", e);
}
@@ -161,7 +162,7 @@ pub async fn communicate_with_hardware(
bode_plot.update_magnitudes(MeasurementPointSet::Eighteen, magnitudes.clone());
bode_plot.update_phases(MeasurementPointSet::Eighteen, phases.clone());
}
if gui_logging_enabled_clone.load(Ordering::Relaxed) {
if *gui_logging_state_clone.lock().unwrap() == LoggingStates::Logging {
if let Err(e) = log_tx_clone.send(LoggingSignal::SweepImpedance(SystemTime::now(), MeasurementPointSet::Eighteen.values().to_vec(), magnitudes.clone(), phases.clone())).await {
error!("Failed to send logging signal: {:?}", e);
}

View File

@@ -1,14 +1,27 @@
use log::info;
use std::sync::{Arc, Mutex};
use std::time::UNIX_EPOCH;
use tokio::sync::mpsc::Receiver;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use std::path::Path;
use std::time::UNIX_EPOCH;
use rfd::AsyncFileDialog;
use crate::signals::LoggingSignal;
pub async fn log_data(mut data: Receiver<LoggingSignal>) {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoggingStates {
Idle,
Starting,
Logging,
}
pub async fn log_data(
mut data: Receiver<LoggingSignal>,
gui_logging_state: Arc<Mutex<LoggingStates>>,
) {
let mut file: Option<File> = None;
loop {
@@ -38,36 +51,64 @@ pub async fn log_data(mut data: Receiver<LoggingSignal>) {
}
}
LoggingSignal::StartFileLogging(filename) => {
let path = Path::new(&filename);
let base = path.file_stem().and_then(|s| s.to_str()).unwrap_or("log");
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
// Update global logging state
*gui_logging_state.lock().unwrap() = LoggingStates::Starting;
// Open a folder picker for the user
let folder = match AsyncFileDialog::new()
.set_title("Select Folder for Logs")
.pick_folder()
.await {
Some(f) => f,
None => {
info!("File logging cancelled by user");
*gui_logging_state.lock().unwrap() = LoggingStates::Idle;
continue;
}
};
let log_dir = folder.path();
// Ensure the directory exists
std::fs::create_dir_all(&log_dir).ok();
// Build initial file path
let mut path = log_dir.join(&filename);
let base = path.file_stem().and_then(|s| s.to_str()).unwrap_or("log").to_string();
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("").to_string();
// Avoid overwriting existing files
let mut counter = 0;
let mut new_filename = filename.clone();
while Path::new(&new_filename).exists() {
while path.exists() {
counter += 1;
new_filename = if ext.is_empty() {
format!("{}_{}", base, counter)
path = if ext.is_empty() {
log_dir.join(format!("{}_{}", base, counter))
} else {
format!("{}_{}.{}", base, counter, ext)
log_dir.join(format!("{}_{}.{}", base, counter, ext))
};
}
match File::create(&new_filename).await {
// Create the file asynchronously
match File::create(&path).await {
Ok(f) => {
info!("Started file logging to: {}", new_filename);
info!("Started file logging to: {}", path.display());
file = Some(f);
}
Err(e) => {
info!("Failed to create log file '{}': {}", new_filename, e);
info!("Failed to create log file '{}': {}", path.display(), e);
}
}
// Update global logging state
*gui_logging_state.lock().unwrap() = LoggingStates::Logging;
}
LoggingSignal::StopFileLogging => {
if file.is_some() {
info!("Stopped file logging");
file = None;
}
// Update global logging state
*gui_logging_state.lock().unwrap() = LoggingStates::Idle;
}
}
}