My personal-knowledge-system, with deeply integrated task tracking and long term goal planning capabilities.

feat/tui: init logging #7

closed opened by suri.codes targeting main from tui
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:nxdjlcnomw44wvso6myag2e6/sh.tangled.repo.pull/3mgl5nipbln22
+118 -25
Diff #0
+3
.harper-dictionary.txt
··· 1 1 Implementers 2 2 Multi-Device 3 + OS 3 4 PKM 4 5 clippy 6 + dir 7 + env 5 8 eyre
+38 -1
Cargo.lock
··· 512 512 "directories", 513 513 "futures", 514 514 "human-panic", 515 - "lazy_static", 516 515 "ratatui", 517 516 "serde", 518 517 "signal-hook 0.4.3", ··· 520 519 "tokio", 521 520 "tokio-util", 522 521 "tracing", 522 + "tracing-error", 523 + "tracing-subscriber", 523 524 ] 524 525 525 526 [[package]] ··· 933 934 "winapi", 934 935 ] 935 936 937 + [[package]] 938 + name = "matchers" 939 + version = "0.2.0" 940 + source = "registry+https://github.com/rust-lang/crates.io-index" 941 + checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" 942 + dependencies = [ 943 + "regex-automata", 944 + ] 945 + 936 946 [[package]] 937 947 name = "memchr" 938 948 version = "2.8.0" ··· 1013 1023 "winapi", 1014 1024 ] 1015 1025 1026 + [[package]] 1027 + name = "nu-ansi-term" 1028 + version = "0.50.3" 1029 + source = "registry+https://github.com/rust-lang/crates.io-index" 1030 + checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" 1031 + dependencies = [ 1032 + "windows-sys 0.59.0", 1033 + ] 1034 + 1016 1035 [[package]] 1017 1036 name = "num-conv" 1018 1037 version = "0.2.0" ··· 1960 1979 "tracing-subscriber", 1961 1980 ] 1962 1981 1982 + [[package]] 1983 + name = "tracing-log" 1984 + version = "0.2.0" 1985 + source = "registry+https://github.com/rust-lang/crates.io-index" 1986 + checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" 1987 + dependencies = [ 1988 + "log", 1989 + "once_cell", 1990 + "tracing-core", 1991 + ] 1992 + 1963 1993 [[package]] 1964 1994 name = "tracing-subscriber" 1965 1995 version = "0.3.22" 1966 1996 source = "registry+https://github.com/rust-lang/crates.io-index" 1967 1997 checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" 1968 1998 dependencies = [ 1999 + "matchers", 2000 + "nu-ansi-term", 2001 + "once_cell", 2002 + "regex-automata", 1969 2003 "sharded-slab", 2004 + "smallvec", 1970 2005 "thread_local", 2006 + "tracing", 1971 2007 "tracing-core", 2008 + "tracing-log", 1972 2009 ] 1973 2010 1974 2011 [[package]]
+2 -1
Cargo.toml
··· 56 56 tokio-util = "0.7.18" 57 57 signal-hook = "0.4.3" 58 58 strum = { version = "0.28.0", features = ["derive"] } 59 - lazy_static = "1.5.0" 60 59 directories = "6.0.0" 60 + tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } 61 + tracing-error = "0.2.1"
+21 -23
src/config.rs
··· 1 1 use directories::ProjectDirs; 2 - use lazy_static::lazy_static; 3 2 use serde::Deserialize; 4 3 use std::{env, path::PathBuf, sync::LazyLock}; 5 4 6 - // #[expect(dead_code)] 7 - static PROJECT_NAME: LazyLock<String> = LazyLock::new(|| env!("CARGO_CRATE_NAME").to_uppercase()); 5 + /// Project Name: Filaments 6 + pub static PROJECT_NAME: LazyLock<String> = 7 + LazyLock::new(|| env!("CARGO_CRATE_NAME").to_uppercase()); 8 8 9 - #[expect(dead_code)] 10 - static DATA_FOLDER: LazyLock<Option<PathBuf>> = LazyLock::new(|| { 9 + /// The OS-agnostic data directory for the project. 10 + pub static DATA_DIRECTORY: LazyLock<Option<PathBuf>> = LazyLock::new(|| { 11 11 env::var(format!("{}_DATA", PROJECT_NAME.clone())) 12 12 .ok() 13 13 .map(PathBuf::from) 14 14 }); 15 - #[expect(dead_code)] 16 - static CONFIG_FOLDER: LazyLock<Option<PathBuf>> = LazyLock::new(|| { 15 + 16 + /// The OS-agnostic config directory for the project. 17 + pub static CONFIG_DIRECTORY: LazyLock<Option<PathBuf>> = LazyLock::new(|| { 17 18 env::var(format!("{}_CONFIG", PROJECT_NAME.clone())) 18 19 .ok() 19 20 .map(PathBuf::from) ··· 47 48 48 49 /// Returns the path to the OS-agnostic data directory. 49 50 pub fn get_data_dir() -> PathBuf { 50 - let directory = if let Some(s) = DATA_FOLDER.clone() { 51 - s 52 - } else if let Some(proj_dirs) = project_directory() { 53 - proj_dirs.data_local_dir().to_path_buf() 54 - } else { 55 - PathBuf::from(".").join(".data") 56 - }; 57 - directory 51 + DATA_DIRECTORY.clone().unwrap_or_else(|| { 52 + project_directory().map_or_else( 53 + || PathBuf::from(".").join(".data"), 54 + |proj_dirs| proj_dirs.data_local_dir().to_path_buf(), 55 + ) 56 + }) 58 57 } 59 58 60 59 /// Returns the path to the OS-agnostic config directory. 60 + #[expect(dead_code)] 61 61 pub fn get_config_dir() -> PathBuf { 62 - let directory = if let Some(s) = CONFIG_FOLDER.clone() { 63 - s 64 - } else if let Some(proj_dirs) = project_directory() { 65 - proj_dirs.config_local_dir().to_path_buf() 66 - } else { 67 - PathBuf::from(".").join(".config") 68 - }; 69 - directory 62 + CONFIG_DIRECTORY.clone().unwrap_or_else(|| { 63 + project_directory().map_or_else( 64 + || PathBuf::from(".").join(".config"), 65 + |proj_dirs| proj_dirs.config_local_dir().to_path_buf(), 66 + ) 67 + }) 70 68 } 71 69 72 70 fn project_directory() -> Option<ProjectDirs> {
+52
src/logging.rs
··· 1 + use std::{ 2 + fs::{File, create_dir_all}, 3 + sync::LazyLock, 4 + }; 5 + 6 + use color_eyre::eyre::Result; 7 + use tracing::Level; 8 + use tracing_error::ErrorLayer; 9 + use tracing_subscriber::{EnvFilter, Layer, fmt, layer::SubscriberExt, util::SubscriberInitExt}; 10 + 11 + use crate::config; 12 + 13 + /// The user-set log level if it exists. 14 + pub static LOG_LEVEL_ENV: LazyLock<String> = 15 + LazyLock::new(|| format!("{}_LOG_LEVEL", config::PROJECT_NAME.clone())); 16 + 17 + /// The logfile name set by our package name. 18 + pub static LOG_FILE: LazyLock<String> = LazyLock::new(|| format!("{}.log", env!("CARGO_PKG_NAME"))); 19 + 20 + /// Initializes the logger, which writes logs to a `log_file` in the data dir. 21 + /// 22 + /// NOTE: log level is configurable via the `RUST_LOG` env var or the 23 + /// `FILAMENTS_LOG_LEVEL` env var 24 + pub fn init() -> Result<()> { 25 + let directory = config::get_data_dir(); 26 + 27 + create_dir_all(&directory)?; 28 + 29 + let log_path = directory.join(LOG_FILE.clone()); 30 + let log_file = File::create(log_path)?; 31 + 32 + let env_filter = EnvFilter::builder().with_default_directive(Level::INFO.into()); 33 + 34 + // If `RUST_LOG` is set, use that as default,, or use value of `LOG_ENV` variable. 35 + let env_filter = env_filter 36 + .try_from_env() 37 + .or_else(|_| env_filter.with_env_var(LOG_LEVEL_ENV.clone()).from_env())?; 38 + 39 + let file_subscriber = fmt::layer() 40 + .with_file(true) 41 + .with_line_number(true) 42 + .with_writer(log_file) 43 + .with_target(false) 44 + .with_ansi(false) 45 + .with_filter(env_filter); 46 + tracing_subscriber::registry() 47 + .with(file_subscriber) 48 + .with(ErrorLayer::default()) 49 + .try_init()?; 50 + 51 + Ok(()) 52 + }
+2
src/main.rs
··· 5 5 mod components; 6 6 mod config; 7 7 mod errors; 8 + mod logging; 8 9 mod signal; 9 10 mod tui; 10 11 11 12 fn main() -> color_eyre::Result<()> { 12 13 errors::init()?; 14 + logging::init()?; 13 15 14 16 Ok(()) 15 17 }

History

1 round 0 comments
sign up or login to add to the discussion
suri.codes submitted #0
1 commit
expand
feat/tui: init logging
expand 0 comments
closed without merging