Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log files #158

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion harbor-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
49 changes: 40 additions & 9 deletions harbor-ui/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -152,25 +156,52 @@ async fn try_auto_unlock(

pub fn run_core() -> impl Stream<Item = Message> {
iced::stream::channel(100, |mut tx: Sender<Message>| 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);
tx.send(Message::UIHandlerLoaded(arc_ui_handle))
.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;

Expand Down
19 changes: 8 additions & 11 deletions harbor-ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -527,7 +524,7 @@ impl HarborWallet {
match self.send_amount_input_str.parse::<u64>() {
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();
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
}
},
Expand Down Expand Up @@ -882,7 +879,7 @@ impl HarborWallet {
Task::none()
}
CoreUIMsg::FederationBalanceUpdated { id, balance } => {
println!(
debug!(
"Balance update received - ID: {:?}, Balance: {:?}",
id, balance
);
Expand All @@ -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}"),
Expand Down Expand Up @@ -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() {
Expand All @@ -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}"),
Expand Down
Loading