From eb187f97d520fc202a791aaac3d1e6ae53ca2b57 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sat, 1 Feb 2025 22:26:57 -0600 Subject: [PATCH] Add log files --- Cargo.lock | 58 ++++++++++++++++------------------------- harbor-ui/Cargo.toml | 2 +- harbor-ui/src/bridge.rs | 49 +++++++++++++++++++++++++++------- harbor-ui/src/main.rs | 19 ++++++-------- 4 files changed, 72 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74bfae2..970dd4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2066,19 +2066,6 @@ dependencies = [ "syn 2.0.95", ] -[[package]] -name = "env_logger" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -3370,9 +3357,9 @@ dependencies = [ "log", "lyon_algorithms", "palette", - "pretty_env_logger", "serde", "serde_json", + "simplelog", "tokio", "uuid", "webbrowser", @@ -4213,17 +4200,6 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" -[[package]] -name = "is-terminal" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" -dependencies = [ - "hermit-abi 0.4.0", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -5022,6 +4998,15 @@ dependencies = [ "syn 2.0.95", ] +[[package]] +name = "num_threads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" +dependencies = [ + "libc", +] + [[package]] name = "objc" version = "0.2.7" @@ -5737,16 +5722,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa" -[[package]] -name = "pretty_env_logger" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" -dependencies = [ - "env_logger", - "log", -] - [[package]] name = "primeorder" version = "0.13.6" @@ -6921,6 +6896,17 @@ dependencies = [ "log", ] +[[package]] +name = "simplelog" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0" +dependencies = [ + "log", + "termcolor", + "time", +] + [[package]] name = "siphasher" version = "0.3.11" @@ -7426,7 +7412,9 @@ checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", + "libc", "num-conv", + "num_threads", "powerfmt", "serde", "time-core", diff --git a/harbor-ui/Cargo.toml b/harbor-ui/Cargo.toml index b710670..4d00308 100644 --- a/harbor-ui/Cargo.toml +++ b/harbor-ui/Cargo.toml @@ -12,7 +12,7 @@ disable-tor = ["harbor-client/disable-tor"] harbor-client = { version = "0.1.0", path = "../harbor-client" } log = "0.4" -pretty_env_logger = "0.5" # todo swap to a file logger +simplelog = "0.12" iced = { version = "0.13.1", features = ["debug", "tokio", "svg", "qr_code", "advanced"] } lyon_algorithms = "1.0" tokio = { version = "1", features = ["full"] } diff --git a/harbor-ui/src/bridge.rs b/harbor-ui/src/bridge.rs index 0b33526..00145bb 100644 --- a/harbor-ui/src/bridge.rs +++ b/harbor-ui/src/bridge.rs @@ -7,8 +7,11 @@ use harbor_client::fedimint_client::{FederationInviteOrId, FedimintClient}; use harbor_client::{data_dir, CoreUIMsg, CoreUIMsgPacket, HarborCore, UICoreMsg, UICoreMsgPacket}; use iced::futures::channel::mpsc::Sender; use iced::futures::{SinkExt, Stream, StreamExt}; -use log::{error, warn}; +use log::{error, warn, LevelFilter}; +use simplelog::WriteLogger; +use simplelog::{CombinedLogger, TermLogger, TerminalMode}; use std::collections::HashMap; +use std::fs::File; use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::atomic::AtomicBool; @@ -19,6 +22,7 @@ use tokio::task::spawn_blocking; use uuid::Uuid; pub const HARBOR_FILE_NAME: &str = "harbor.sqlite"; +pub const LOG_FILE_NAME: &str = "harbor.log"; #[derive(Debug)] pub struct UIHandle { @@ -152,6 +156,41 @@ async fn try_auto_unlock( pub fn run_core() -> impl Stream { iced::stream::channel(100, |mut tx: Sender| async move { + let config = read_config().expect("could not read config"); + let network = config.network; + + // Create the network-specific datadir if it doesn't exist + let path = PathBuf::from(&data_dir(Some(network))); + std::fs::create_dir_all(&path).expect("Could not create datadir"); + log::info!("Using datadir: {path:?}"); + + let log_file_path = path.join(LOG_FILE_NAME); + let log_file = if log_file_path.exists() { + File::open(log_file_path).expect("Could not open log file") + } else { + File::create(log_file_path).expect("Could not create log file") + }; + + let log_config = simplelog::ConfigBuilder::new() + // ignore spammy UI logs + .add_filter_ignore_str("wgpu_hal") + .add_filter_ignore_str("wgpu_core") + .add_filter_ignore_str("iced") + .add_filter_ignore_str("naga") + .add_filter_ignore_str("cosmic_text") + .add_filter_ignore_str("rustls") + .build(); + CombinedLogger::init(vec![ + TermLogger::new( + LevelFilter::Info, + log_config.clone(), + TerminalMode::Mixed, + simplelog::ColorChoice::Auto, + ), + WriteLogger::new(LevelFilter::Debug, log_config, log_file), + ]) + .expect("Could not initialize logger"); + // Setup UI Handle let (ui_handle, mut core_handle) = create_handles(); let arc_ui_handle = Arc::new(ui_handle); @@ -159,18 +198,10 @@ pub fn run_core() -> impl Stream { .await .expect("should send"); - let config = read_config().expect("could not read config"); - let network = config.network; - tx.send(Message::ConfigLoaded(config)) .await .expect("should send"); - // Create the network-specific datadir if it doesn't exist - let path = PathBuf::from(&data_dir(Some(network))); - std::fs::create_dir_all(&path).expect("Could not create datadir"); - log::info!("Using datadir: {path:?}"); - // FIXME: Artificial sleep because it loads too fast tokio::time::sleep(Duration::from_secs(1)).await; diff --git a/harbor-ui/src/main.rs b/harbor-ui/src/main.rs index 4c084b5..48958b5 100644 --- a/harbor-ui/src/main.rs +++ b/harbor-ui/src/main.rs @@ -18,7 +18,7 @@ use iced::Subscription; use iced::Task; use iced::{clipboard, Color}; use iced::{window, Element}; -use log::{error, info}; +use log::{debug, error, info, trace}; use routes::Route; use std::str::FromStr; use std::sync::Arc; @@ -32,8 +32,6 @@ pub mod routes; // This starts the program. Importantly, it registers the update and view methods, along with a subscription. // We can also run logic during load if we need to. pub fn main() -> iced::Result { - pretty_env_logger::init(); - #[cfg(target_os = "macos")] let window_settings = window::Settings { platform_specific: window::settings::PlatformSpecific { @@ -325,7 +323,6 @@ impl HarborWallet { // Setup Message::UIHandlerLoaded(ui_handle) => { self.ui_handle = Some(ui_handle); - println!("Core loaded"); Task::none() } Message::ConfigLoaded(config) => { @@ -527,7 +524,7 @@ impl HarborWallet { match self.send_amount_input_str.parse::() { Ok(amount) => Some(amount), Err(e) => { - eprintln!("Error parsing amount: {e}"); + error!("Error parsing amount: {e}"); self.send_failure_reason = Some(e.to_string()); return Task::none(); } @@ -619,7 +616,7 @@ impl HarborWallet { } Err(e) => { self.receive_amount_str = String::new(); - eprintln!("Error parsing amount: {e}"); + error!("Error parsing amount: {e}"); Task::perform(async {}, move |_| { Message::AddToast(Toast { title: "Failed to generate invoice".to_string(), @@ -672,7 +669,7 @@ impl HarborWallet { } Err(e) => { self.receive_amount_str = String::new(); - eprintln!("Error parsing amount: {e}"); + error!("Error parsing amount: {e}"); Task::none() } }, @@ -882,7 +879,7 @@ impl HarborWallet { Task::none() } CoreUIMsg::FederationBalanceUpdated { id, balance } => { - println!( + debug!( "Balance update received - ID: {:?}, Balance: {:?}", id, balance ); @@ -900,7 +897,7 @@ impl HarborWallet { } CoreUIMsg::ReceiveInvoiceGenerated(invoice) => { self.receive_status = ReceiveStatus::WaitingToReceive; - println!("Received invoice: {invoice}"); + debug!("Received invoice: {invoice}"); self.receive_qr_data = Some( Data::with_error_correction( format!("lightning:{invoice}"), @@ -998,7 +995,7 @@ impl HarborWallet { }) } CoreUIMsg::FederationListUpdated(list) => { - println!("Updated federation list: {:#?}", list); + trace!("Updated federation list: {:#?}", list); // if we don't have an active federation, set it to the first one if self.active_federation_id.is_none() { @@ -1013,7 +1010,7 @@ impl HarborWallet { } CoreUIMsg::ReceiveAddressGenerated(address) => { self.receive_status = ReceiveStatus::WaitingToReceive; - println!("Received address: {address}"); + debug!("Received address: {address}"); self.receive_qr_data = Some( Data::with_error_correction( format!("bitcoin:{address}"),