diff --git a/Cargo.lock b/Cargo.lock index 08d3eb73..8004943c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -917,6 +917,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-deque" version = "0.8.5" @@ -2568,6 +2577,7 @@ dependencies = [ "tempfile", "tokio", "tracing", + "tracing-appender", "tracing-subscriber", "tracing-wasm", "uuid", @@ -4427,6 +4437,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf" +dependencies = [ + "crossbeam-channel", + "thiserror", + "time", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.27" diff --git a/Cargo.toml b/Cargo.toml index 8213a36c..18f107f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ bitflags = "2.5.0" uuid = { version = "1.10.0", features = ["v4"] } indexmap = "2.6.0" dirs = "5.0.1" +tracing-appender = "0.2.3" [dev-dependencies] tempfile = "3.13.0" diff --git a/src/bin/notedeck.rs b/src/bin/notedeck.rs index 199272b4..ec77bd6d 100644 --- a/src/bin/notedeck.rs +++ b/src/bin/notedeck.rs @@ -11,8 +11,57 @@ use notedeck::Damus; #[cfg(not(target_arch = "wasm32"))] #[tokio::main] async fn main() { + #[allow(unused_variables)] // need guard to live for lifetime of program + let (maybe_non_blocking, maybe_guard) = + if let Ok(log_path) = notedeck::DataPaths::Log.get_path() { + // Setup logging to file + use std::panic; + + use tracing::error; + use tracing_appender::{ + non_blocking, + rolling::{RollingFileAppender, Rotation}, + }; + + let file_appender = RollingFileAppender::new( + Rotation::DAILY, + log_path, + format!("notedeck-{}.log", env!("CARGO_PKG_VERSION")), + ); + panic::set_hook(Box::new(|panic_info| { + error!("Notedeck panicked: {:?}", panic_info); + })); + + let (non_blocking, _guard) = non_blocking(file_appender); + + (Some(non_blocking), Some(_guard)) + } else { + (None, None) + }; + // Log to stdout (if you run with `RUST_LOG=debug`). - tracing_subscriber::fmt::init(); + if let Some(non_blocking_writer) = maybe_non_blocking { + use tracing::Level; + use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt}; + + let console_layer = fmt::layer().with_target(true).with_writer(std::io::stdout); + + // Create the file layer (writes to the file) + let file_layer = fmt::layer() + .with_ansi(false) + .with_writer(non_blocking_writer); + + // Set up the subscriber to combine both layers + tracing_subscriber::registry() + .with(console_layer) + .with(file_layer) + .with(tracing_subscriber::filter::LevelFilter::from_level( + Level::INFO, + )) // Set log level + .init(); + } else { + tracing_subscriber::fmt::init(); + } let _res = eframe::run_native( "Damus NoteDeck", diff --git a/src/lib.rs b/src/lib.rs index 6d35e2a3..a887d1e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ mod abbrev; pub mod account_manager; mod actionbar; pub mod app_creation; +mod app_size_handler; mod app_style; mod args; mod colors;