diff options
| author | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-04-22 14:52:59 -0400 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-04-22 14:52:59 -0400 |
| commit | 0952686526e118339b945ccfb84db1dcc69ee2ab (patch) | |
| tree | 983cc5018ba31f88d89c241c951a38a4cae752c8 /src | |
| parent | 25246c26c11f485272689b8c7bd7910f90cc38ed (diff) | |
| download | equity-tracker-0952686526e118339b945ccfb84db1dcc69ee2ab.tar.gz equity-tracker-0952686526e118339b945ccfb84db1dcc69ee2ab.tar.bz2 equity-tracker-0952686526e118339b945ccfb84db1dcc69ee2ab.zip | |
Add logging
Diffstat (limited to 'src')
| -rw-r--r-- | src/app/mod.rs | 9 | ||||
| -rw-r--r-- | src/app/store/sqlite.rs | 2 | ||||
| -rw-r--r-- | src/logging.rs | 54 | ||||
| -rw-r--r-- | src/main.rs | 12 |
4 files changed, 77 insertions, 0 deletions
diff --git a/src/app/mod.rs b/src/app/mod.rs index 15290ef..d51640f 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -34,6 +34,7 @@ impl App { } pub fn exit(&mut self) -> Result<()> { + tracing::event!(tracing::Level::TRACE, exit = true, "Changing exit status."); self.exit = true; Ok(()) } @@ -42,6 +43,7 @@ impl App { let store = self.context.unwrap_store(); let count = store.get_counter()?; + tracing::event!(tracing::Level::TRACE, count, "Decrementing counter."); if count == 0 { Err(OverflowError { _msg: "Cannot count below 0".into(), @@ -57,6 +59,7 @@ impl App { let store = self.context.unwrap_store(); let count = store.get_counter()?; + tracing::event!(tracing::Level::TRACE, count, "Incrementing counter."); if count == u8::MAX { Err(OverflowError { _msg: "Cannot count above u8::MAX".into(), @@ -77,6 +80,12 @@ impl App { } pub fn set_error(&mut self, error: ErrReport) -> Result<()> { + tracing::event!( + tracing::Level::ERROR, + error = error.to_string(), + help = error.help().map_or(String::default(), |h| h.to_string()) + ); + self.error = Some(error); Ok(()) } diff --git a/src/app/store/sqlite.rs b/src/app/store/sqlite.rs index 4f5f8f2..e971ef3 100644 --- a/src/app/store/sqlite.rs +++ b/src/app/store/sqlite.rs @@ -87,6 +87,8 @@ impl Store for SQLiteStore { fn set_counter(&mut self, new_count: u8) -> Result<u8, ContextError> { self.update_counter(i64::from(new_count))?; + tracing::event!(tracing::Level::DEBUG, new_count, "Updated counter."); + Ok(new_count) } } diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..fd81dba --- /dev/null +++ b/src/logging.rs @@ -0,0 +1,54 @@ +use std::{io::Error, path::PathBuf, str::FromStr}; + +use tracing_error::ErrorLayer; +use tracing_subscriber::{self, Layer, layer::SubscriberExt, util::SubscriberInitExt}; + +const LOG_FILE: &'static str = "equity-tracker.log"; + +const LOG_LEVEL: &'static str = "LOG_LEVEL"; +const DEFAULT_LOG_LEVEL: &'static str = "DEBUG"; + +const LOG_DIRECTORY: &'static str = "LOG_DIRECTORY"; +const DEFAULT_LOG_DIRECTORY: &'static str = "./target"; + +/// Either retrieves the environment variable or uses a default value +fn get_env_var(var_name: &'static str, default: &'static str) -> String { + if let Ok(env_var) = std::env::var(var_name) { + env_var + } else { + default.into() + } +} + +/// Provides a PathBuf of the configured log directory, or the target directory +fn get_log_directory() -> std::io::Result<PathBuf> { + let env_dir = get_env_var(LOG_DIRECTORY, DEFAULT_LOG_DIRECTORY); + let path = PathBuf::from_str(&env_dir).map_err(|e| Error::other(e))?; + + Ok(path) +} + +/// Configure the logging through tracing +pub fn initialize_logging() -> std::io::Result<()> { + let directory = get_log_directory()?; + std::fs::create_dir_all(directory.clone())?; + + let log_path = directory.join(LOG_FILE); + let log_file = std::fs::File::create(log_path)?; + + let log_filter = get_env_var(LOG_LEVEL, DEFAULT_LOG_LEVEL); + let file_subscriber = tracing_subscriber::fmt::layer() + .with_file(true) + .with_line_number(true) + .with_target(false) + .with_ansi(false) + .with_writer(log_file) + .with_filter(tracing_subscriber::filter::EnvFilter::builder().parse_lossy(log_filter)); + + tracing_subscriber::registry() + .with(file_subscriber) + .with(ErrorLayer::default()) + .init(); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index bb63c86..d1d4ee4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod app; +mod logging; mod ui; use app::App; @@ -24,9 +25,15 @@ fn main() -> Result<()> { let backend = CrosstermBackend::new(stdout); let mut term = Terminal::new(backend).into_diagnostic()?; + logging::initialize_logging().into_diagnostic()?; + + tracing::event!(tracing::Level::DEBUG, "App initialization started."); + let mut app = App::default(); app.init_context()?; + tracing::event!(tracing::Level::INFO, "App started."); + // could be error, so don't unwrap; let term restore first let run = run_app(&mut term, &mut app); @@ -67,6 +74,11 @@ fn run_app<B: Backend>(term: &mut Terminal<B>, app: &mut App) -> Result<()> { /// Process user input and run associated app functions fn handle_key_event(app: &mut App, key_event: KeyEvent) -> Result<()> { + tracing::event!( + tracing::Level::TRACE, + event = key_event.code.to_string(), + "Received key event." + ); match key_event.code { KeyCode::Char('q') => app.exit()?, KeyCode::Left => app.decrement_counter()?, |
