diff --git a/Cargo.lock b/Cargo.lock index 9cc8cbc..0d0cd6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -153,12 +153,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "anyhow" -version = "1.0.81" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" - [[package]] name = "ariadne" version = "0.2.0" @@ -346,17 +340,6 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.2.0" @@ -722,6 +705,33 @@ dependencies = [ "roff", ] +[[package]] +name = "color-eyre" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -1218,8 +1228,7 @@ dependencies = [ [[package]] name = "email-lib" version = "0.24.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28526bad9d46fcfb2c6385961aeb4b684546c965d18a12e5552ca4af7cf1f35e" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "advisory-lock", "async-trait", @@ -1268,8 +1277,7 @@ dependencies = [ [[package]] name = "email-macros" version = "0.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f24a09fd651027f8764f8a12c12358715cb9bab622ab3125ede3dd6ae047c95" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "quote", "syn 2.0.58", @@ -1332,19 +1340,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "env_logger" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -1429,6 +1424,16 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -1793,15 +1798,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.3.9" @@ -1869,24 +1865,22 @@ dependencies = [ name = "himalaya" version = "1.0.0-beta.4" dependencies = [ - "anyhow", "ariadne", "async-trait", "chrono", "clap", "clap_complete", "clap_mangen", + "color-eyre", "console", "coredump", "dialoguer", "dirs 4.0.0", "email-lib", "email_address", - "env_logger", "erased-serde", "indicatif", "keyring-lib", - "log", "mail-builder", "md5", "mml-lib", @@ -1905,6 +1899,9 @@ dependencies = [ "tokio", "toml", "toml_edit 0.22.9", + "tracing", + "tracing-error", + "tracing-subscriber", "unicode-width", "url", "uuid", @@ -1982,12 +1979,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "0.14.28" @@ -2112,6 +2103,12 @@ dependencies = [ "nom", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "2.2.6" @@ -2179,7 +2176,7 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ - "hermit-abi 0.3.9", + "hermit-abi", "libc", "windows-sys 0.48.0", ] @@ -2258,8 +2255,7 @@ dependencies = [ [[package]] name = "keyring-lib" version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2e6d01e57f1b382ba5a1c40ef866855e8bab15f7caa4033480db80a5b9b9aff" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "keyring", "log", @@ -2482,6 +2478,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "md-5" version = "0.10.6" @@ -2558,8 +2563,7 @@ dependencies = [ [[package]] name = "mml-lib" version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3c6ac41c419e7857fdf7104a2f750c0d575969fa32f50a9d370ea3a3bcab1bf" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "async-recursion", "chumsky", @@ -2645,6 +2649,16 @@ dependencies = [ "libc", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num" version = "0.4.1" @@ -2762,7 +2776,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi 0.3.9", + "hermit-abi", "libc", ] @@ -2775,8 +2789,7 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" [[package]] name = "oauth-lib" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd81f50b3bdf9656f511a776e5a76c249e00d7ce5c0c9071d0b923177d9248d" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "log", "oauth2", @@ -2935,6 +2948,18 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "p256" version = "0.13.2" @@ -3220,7 +3245,7 @@ checksum = "e0c976a60b2d7e99d6f229e414670a9b85d13ac305cc6d1e9c134de58c5aaaf6" dependencies = [ "cfg-if", "concurrent-queue", - "hermit-abi 0.3.9", + "hermit-abi", "pin-project-lite", "rustix 0.38.32", "tracing", @@ -3313,8 +3338,7 @@ dependencies = [ [[package]] name = "process-lib" version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0600893efc62c84ded38fec2b022ada4d4d80e48a8df5bceb070c865bf8a33a9" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "log", "serde", @@ -3446,6 +3470,15 @@ dependencies = [ "regex-syntax 0.8.3", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + [[package]] name = "regex-automata" version = "0.3.9" @@ -3468,6 +3501,12 @@ dependencies = [ "regex-syntax 0.8.3", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.7.5" @@ -3815,8 +3854,7 @@ dependencies = [ [[package]] name = "secret-lib" version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d99d0df4b8cb27c13c953abaf74bd089922a783fad99cd2b08b369cf7fabcbc9" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "keyring-lib", "log", @@ -3999,6 +4037,15 @@ dependencies = [ "keccak", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shell-words" version = "1.1.0" @@ -4019,8 +4066,7 @@ dependencies = [ [[package]] name = "shellexpand-utils" version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c15a8f3693529b38d1be40ec6016bb6317ebee4a0208a813a75e3538f3e439f5" +source = "git+https://git.sr.ht/~soywod/pimalaya#7d56cfda13fe13a9aa48d9a117573e3dfbbcf8a2" dependencies = [ "log", "shellexpand", @@ -4284,6 +4330,16 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.34" @@ -4463,6 +4519,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -4594,6 +4690,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version-compare" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 14cecb7..9dd8577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,23 +49,21 @@ pgp-native = ["email-lib/pgp-native", "mml-lib/pgp-native", "pgp"] tempfile = "3.3" [dependencies] -anyhow = "1" ariadne = "0.2" async-trait = "0.1" chrono = "0.4.24" clap = { version = "4.4", features = ["derive"] } clap_complete = "4.4" clap_mangen = "0.2" +color-eyre = "0.6.3" console = "0.15.2" dialoguer = "0.10.2" dirs = "4" email-lib = { version = "=0.24.0", default-features = false, features = ["derive"] } email_address = "0.2.4" -env_logger = "0.8" erased-serde = "0.3" indicatif = "0.17" keyring-lib = { version = "=0.4.2", features = ["derive"] } -log = "0.4" mail-builder = "0.3" md5 = "0.7" mml-lib = { version = "=1.0.12", default-features = false, features = ["derive"] } @@ -83,6 +81,9 @@ terminal_size = "0.1" tokio = { version = "1.23", default-features = false, features = ["macros", "rt-multi-thread"] } toml = "0.8" toml_edit = "0.22" +tracing = "0.1.40" +tracing-error = "0.2.0" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } unicode-width = "0.1" url = "2.2" uuid = { version = "0.8", features = ["v4"] } diff --git a/src/account/command/check_up.rs b/src/account/command/check_up.rs index 51fc764..9e94839 100644 --- a/src/account/command/check_up.rs +++ b/src/account/command/check_up.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::context::BackendContextBuilder; -use log::info; +use tracing::info; use crate::{ account::arg::name::OptionalAccountNameArg, backend, config::TomlConfig, printer::Printer, diff --git a/src/account/command/configure.rs b/src/account/command/configure.rs index a7e171f..69be407 100644 --- a/src/account/command/configure.rs +++ b/src/account/command/configure.rs @@ -1,12 +1,12 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; #[cfg(feature = "imap")] use email::imap::config::ImapAuthConfig; #[cfg(feature = "smtp")] use email::smtp::config::SmtpAuthConfig; -use log::info; +use tracing::info; #[cfg(any(feature = "imap", feature = "smtp"))] -use log::{debug, warn}; +use tracing::{debug, warn}; #[cfg(any(feature = "imap", feature = "smtp", feature = "pgp"))] use crate::ui::prompt; diff --git a/src/account/command/list.rs b/src/account/command/list.rs index 720680a..c135ad9 100644 --- a/src/account/command/list.rs +++ b/src/account/command/list.rs @@ -1,6 +1,6 @@ -use anyhow::Result; use clap::Parser; -use log::info; +use color_eyre::Result; +use tracing::info; use crate::{ account::Accounts, diff --git a/src/account/command/mod.rs b/src/account/command/mod.rs index 7698214..5f2de44 100644 --- a/src/account/command/mod.rs +++ b/src/account/command/mod.rs @@ -4,7 +4,7 @@ mod list; #[cfg(feature = "account-sync")] mod sync; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/account/command/sync.rs b/src/account/command/sync.rs index 18ae59c..929dd78 100644 --- a/src/account/command/sync.rs +++ b/src/account/command/sync.rs @@ -1,5 +1,5 @@ -use anyhow::{anyhow, bail, Result}; use clap::{ArgAction, Parser}; +use color_eyre::{eyre::bail, eyre::eyre, Result}; use email::backend::context::BackendContextBuilder; #[cfg(feature = "imap")] use email::imap::ImapContextBuilder; @@ -10,12 +10,12 @@ use email::{ sync::{hash::SyncHash, SyncEvent}, }; use indicatif::{MultiProgress, ProgressBar, ProgressFinish, ProgressStyle}; -use log::info; use once_cell::sync::Lazy; use std::{ collections::{BTreeSet, HashMap}, sync::{Arc, Mutex}, }; +use tracing::info; use crate::{ account::arg::name::OptionalAccountNameArg, backend::BackendKind, config::TomlConfig, @@ -99,7 +99,7 @@ impl AccountSyncCommand { .as_ref() .map(Clone::clone) .map(Arc::new) - .ok_or_else(|| anyhow!("imap config not found"))?; + .ok_or_else(|| eyre!("imap config not found"))?; let imap_ctx = ImapContextBuilder::new(account_config.clone(), imap_config) .with_prebuilt_credentials() .await?; diff --git a/src/account/mod.rs b/src/account/mod.rs index 62bed18..6b741ff 100644 --- a/src/account/mod.rs +++ b/src/account/mod.rs @@ -3,7 +3,7 @@ pub mod command; pub mod config; pub(crate) mod wizard; -use anyhow::Result; +use color_eyre::Result; use serde::Serialize; use std::{collections::hash_map::Iter, fmt, ops::Deref}; diff --git a/src/account/wizard.rs b/src/account/wizard.rs index 722be03..047e3c2 100644 --- a/src/account/wizard.rs +++ b/src/account/wizard.rs @@ -1,6 +1,6 @@ #[cfg(feature = "account-sync")] use crate::account::config::SyncConfig; -use anyhow::{bail, Result}; +use color_eyre::{eyre::bail, Result}; #[cfg(feature = "account-sync")] use dialoguer::Confirm; use dialoguer::Input; diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 227f257..81e5bad 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,7 +1,7 @@ pub mod config; pub(crate) mod wizard; -use anyhow::Result; +use color_eyre::Result; use async_trait::async_trait; use std::{ops::Deref, sync::Arc}; diff --git a/src/backend/wizard.rs b/src/backend/wizard.rs index b37484a..ccd0583 100644 --- a/src/backend/wizard.rs +++ b/src/backend/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::Select; #[cfg(feature = "account-discovery")] use email::account::discover::config::AutoConfig; diff --git a/src/cache/mod.rs b/src/cache/mod.rs index fce7241..5a111e6 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -1,11 +1,11 @@ pub mod arg; -use anyhow::{anyhow, Context, Result}; +use color_eyre::{eyre::eyre, eyre::Context, Result}; use dirs::data_dir; use email::account::config::AccountConfig; -use log::debug; use sled::{Config, Db}; use std::collections::HashSet; +use tracing::debug; #[derive(Debug)] pub enum IdMapper { @@ -17,7 +17,7 @@ impl IdMapper { pub fn new(account_config: &AccountConfig, folder: &str) -> Result { let digest = md5::compute(account_config.name.clone() + folder); let db_path = data_dir() - .ok_or(anyhow!("cannot get XDG data directory"))? + .ok_or(eyre!("cannot get XDG data directory"))? .join("himalaya") .join(".id-mappers") .join(format!("{digest:x}")); @@ -108,7 +108,7 @@ impl IdMapper { None } }) - .ok_or_else(|| anyhow!("cannot get id from alias {alias}"))?; + .ok_or_else(|| eyre!("cannot get id from alias {alias}"))?; debug!("found id {id} from alias {alias}"); Ok(id) diff --git a/src/cli.rs b/src/cli.rs index d957bf6..440f08b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,5 @@ -use anyhow::Result; use clap::{Parser, Subcommand}; +use color_eyre::Result; use std::path::PathBuf; use crate::{ @@ -75,6 +75,20 @@ pub struct Cli { #[arg(long, short = 'C', global = true)] #[arg(value_name = "MODE", value_enum, default_value_t = Default::default())] pub color: ColorFmt, + + /// Enable logs with spantrace. + /// + /// This is the same as running the command with `RUST_LOG=debug` + /// environment variable. + #[arg(long, global = true, conflicts_with = "trace")] + pub debug: bool, + + /// Enable verbose logs with backtrace. + /// + /// This is the same as running the command with `RUST_LOG=trace` + /// and `RUST_BACKTRACE=1` environment variables. + #[arg(long, global = true, conflicts_with = "debug")] + pub trace: bool, } #[derive(Subcommand, Debug)] diff --git a/src/completion/command.rs b/src/completion/command.rs index 7d11d3d..11bc542 100644 --- a/src/completion/command.rs +++ b/src/completion/command.rs @@ -1,8 +1,8 @@ -use anyhow::Result; use clap::{value_parser, CommandFactory, Parser}; use clap_complete::Shell; -use log::info; +use color_eyre::Result; use std::io; +use tracing::info; use crate::cli::Cli; diff --git a/src/config/mod.rs b/src/config/mod.rs index 6e4d3eb..997147b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,17 +1,20 @@ pub mod wizard; -use anyhow::{anyhow, bail, Context, Result}; +use color_eyre::{ + eyre::{bail, eyre, Context}, + Result, +}; use dirs::{config_dir, home_dir}; use email::{ account::config::AccountConfig, config::Config, envelope::config::EnvelopeConfig, flag::config::FlagConfig, folder::config::FolderConfig, message::config::MessageConfig, }; -use log::debug; use serde::{Deserialize, Serialize}; use serde_toml_merge::merge; use shellexpand_utils::{canonicalize, expand}; use std::{collections::HashMap, fs, path::PathBuf, sync::Arc}; use toml::{self, Value}; +use tracing::debug; #[cfg(feature = "account-sync")] use crate::backend::BackendKind; @@ -136,7 +139,7 @@ impl TomlConfig { /// found. pub fn default_path() -> Result { Ok(config_dir() - .ok_or(anyhow!("cannot get XDG config directory"))? + .ok_or(eyre!("cannot get XDG config directory"))? .join("himalaya") .join("config.toml")) } @@ -175,12 +178,12 @@ impl TomlConfig { .filter(|default| *default) .map(|_| (name.to_owned(), account.clone())) }) - .ok_or_else(|| anyhow!("cannot find default account")), + .ok_or_else(|| eyre!("cannot find default account")), Some(name) => self .accounts .get(name) .map(|account| (name.to_owned(), account.clone())) - .ok_or_else(|| anyhow!("cannot find account {name}")), + .ok_or_else(|| eyre!("cannot find account {name}")), }?; #[cfg(feature = "imap")] diff --git a/src/config/wizard.rs b/src/config/wizard.rs index 796e5fc..79e59e3 100644 --- a/src/config/wizard.rs +++ b/src/config/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::{Confirm, Input, Select}; use shellexpand_utils::expand; use std::{fs, path::PathBuf, process}; diff --git a/src/email/envelope/command/list.rs b/src/email/envelope/command/list.rs index 7b440e8..2a0cbf4 100644 --- a/src/email/envelope/command/list.rs +++ b/src/email/envelope/command/list.rs @@ -1,12 +1,12 @@ -use anyhow::Result; use ariadne::{Color, Label, Report, ReportKind, Source}; use clap::Parser; +use color_eyre::Result; use email::{ backend::feature::BackendFeatureSource, email::search_query, envelope::list::ListEnvelopesOptions, search_query::SearchEmailsQuery, }; -use log::info; use std::process::exit; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/envelope/command/mod.rs b/src/email/envelope/command/mod.rs index 4b05c6c..3de79a6 100644 --- a/src/email/envelope/command/mod.rs +++ b/src/email/envelope/command/mod.rs @@ -1,7 +1,7 @@ pub mod list; pub mod watch; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/email/envelope/command/watch.rs b/src/email/envelope/command/watch.rs index 0323571..1f93458 100644 --- a/src/email/envelope/command/watch.rs +++ b/src/email/envelope/command/watch.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/envelope/flag/arg/ids_and_flags.rs b/src/email/envelope/flag/arg/ids_and_flags.rs index a8f9e70..8827a81 100644 --- a/src/email/envelope/flag/arg/ids_and_flags.rs +++ b/src/email/envelope/flag/arg/ids_and_flags.rs @@ -1,6 +1,6 @@ use clap::Parser; use email::flag::{Flag, Flags}; -use log::debug; +use tracing::debug; /// The ids and/or flags arguments parser. #[derive(Debug, Parser)] diff --git a/src/email/envelope/flag/command/add.rs b/src/email/envelope/flag/command/add.rs index a0520e5..dcfc458 100644 --- a/src/email/envelope/flag/command/add.rs +++ b/src/email/envelope/flag/command/add.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/envelope/flag/command/mod.rs b/src/email/envelope/flag/command/mod.rs index 8e87783..da9ec04 100644 --- a/src/email/envelope/flag/command/mod.rs +++ b/src/email/envelope/flag/command/mod.rs @@ -2,7 +2,7 @@ mod add; mod remove; mod set; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/email/envelope/flag/command/remove.rs b/src/email/envelope/flag/command/remove.rs index 74fba55..1ef7c23 100644 --- a/src/email/envelope/flag/command/remove.rs +++ b/src/email/envelope/flag/command/remove.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/envelope/flag/command/set.rs b/src/email/envelope/flag/command/set.rs index f98852b..3cdd9f7 100644 --- a/src/email/envelope/flag/command/set.rs +++ b/src/email/envelope/flag/command/set.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/envelope/mod.rs b/src/email/envelope/mod.rs index 18ba071..a842521 100644 --- a/src/email/envelope/mod.rs +++ b/src/email/envelope/mod.rs @@ -3,7 +3,7 @@ pub mod command; pub mod config; pub mod flag; -use anyhow::Result; +use color_eyre::Result; use email::account::config::AccountConfig; use serde::Serialize; use std::ops; diff --git a/src/email/message/attachment/command/download.rs b/src/email/message/attachment/command/download.rs index 8b4d798..4c73edc 100644 --- a/src/email/message/attachment/command/download.rs +++ b/src/email/message/attachment/command/download.rs @@ -1,8 +1,8 @@ -use anyhow::{Context, Result}; use clap::Parser; +use color_eyre::{eyre::Context, Result}; use email::backend::feature::BackendFeatureSource; -use log::info; use std::{fs, path::PathBuf}; +use tracing::info; use uuid::Uuid; #[cfg(feature = "account-sync")] diff --git a/src/email/message/attachment/command/mod.rs b/src/email/message/attachment/command/mod.rs index f7b6970..634fa36 100644 --- a/src/email/message/attachment/command/mod.rs +++ b/src/email/message/attachment/command/mod.rs @@ -1,6 +1,6 @@ mod download; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/email/message/command/copy.rs b/src/email/message/command/copy.rs index 70329d0..171e31f 100644 --- a/src/email/message/command/copy.rs +++ b/src/email/message/command/copy.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/command/delete.rs b/src/email/message/command/delete.rs index de6189e..7965c5a 100644 --- a/src/email/message/command/delete.rs +++ b/src/email/message/command/delete.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/command/forward.rs b/src/email/message/command/forward.rs index c586561..f64c230 100644 --- a/src/email/message/command/forward.rs +++ b/src/email/message/command/forward.rs @@ -1,7 +1,7 @@ -use anyhow::{anyhow, Result}; use clap::Parser; +use color_eyre::{eyre::eyre, Result}; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; @@ -75,7 +75,7 @@ impl MessageForwardCommand { .get_messages(folder, &[id]) .await? .first() - .ok_or(anyhow!("cannot find message"))? + .ok_or(eyre!("cannot find message"))? .to_forward_tpl_builder(account_config.clone()) .with_headers(self.headers.raw) .with_body(self.body.raw()) diff --git a/src/email/message/command/mailto.rs b/src/email/message/command/mailto.rs index 20b3e6e..e855a73 100644 --- a/src/email/message/command/mailto.rs +++ b/src/email/message/command/mailto.rs @@ -1,8 +1,8 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; use mail_builder::MessageBuilder; +use tracing::info; use url::Url; #[cfg(feature = "account-sync")] diff --git a/src/email/message/command/mod.rs b/src/email/message/command/mod.rs index b5065c9..dd99709 100644 --- a/src/email/message/command/mod.rs +++ b/src/email/message/command/mod.rs @@ -9,7 +9,7 @@ pub mod save; pub mod send; pub mod write; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/email/message/command/move.rs b/src/email/message/command/move.rs index 39ed208..3c63ea3 100644 --- a/src/email/message/command/move.rs +++ b/src/email/message/command/move.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/command/read.rs b/src/email/message/command/read.rs index 7ef27be..1747b1f 100644 --- a/src/email/message/command/read.rs +++ b/src/email/message/command/read.rs @@ -1,8 +1,8 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; use mml::message::FilterParts; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/command/reply.rs b/src/email/message/command/reply.rs index 33830cd..a9c1cc1 100644 --- a/src/email/message/command/reply.rs +++ b/src/email/message/command/reply.rs @@ -1,7 +1,7 @@ -use anyhow::{anyhow, Result}; use clap::Parser; +use color_eyre::{eyre::eyre, Result}; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; @@ -77,7 +77,7 @@ impl MessageReplyCommand { .get_messages(folder, &[id]) .await? .first() - .ok_or(anyhow!("cannot find message {id}"))? + .ok_or(eyre!("cannot find message {id}"))? .to_reply_tpl_builder(account_config.clone()) .with_headers(self.headers.raw) .with_body(self.body.raw()) diff --git a/src/email/message/command/save.rs b/src/email/message/command/save.rs index 3efc76b..6bfebde 100644 --- a/src/email/message/command/save.rs +++ b/src/email/message/command/save.rs @@ -1,8 +1,8 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; use std::io::{self, BufRead, IsTerminal}; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/command/send.rs b/src/email/message/command/send.rs index 98fa278..000bfad 100644 --- a/src/email/message/command/send.rs +++ b/src/email/message/command/send.rs @@ -1,8 +1,8 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; use std::io::{self, BufRead, IsTerminal}; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/command/write.rs b/src/email/message/command/write.rs index 0746fcc..519431c 100644 --- a/src/email/message/command/write.rs +++ b/src/email/message/command/write.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::{backend::feature::BackendFeatureSource, message::Message}; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/template/command/forward.rs b/src/email/message/template/command/forward.rs index 086205c..35ef478 100644 --- a/src/email/message/template/command/forward.rs +++ b/src/email/message/template/command/forward.rs @@ -1,7 +1,7 @@ -use anyhow::{anyhow, Result}; use clap::Parser; +use color_eyre::{eyre::eyre, Result}; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; @@ -69,7 +69,7 @@ impl TemplateForwardCommand { .get_messages(folder, &[id]) .await? .first() - .ok_or(anyhow!("cannot find message {id}"))? + .ok_or(eyre!("cannot find message {id}"))? .to_forward_tpl_builder(account_config) .with_headers(self.headers.raw) .with_body(self.body.raw()) diff --git a/src/email/message/template/command/mod.rs b/src/email/message/template/command/mod.rs index 6e1162e..4bd5a8e 100644 --- a/src/email/message/template/command/mod.rs +++ b/src/email/message/template/command/mod.rs @@ -4,7 +4,7 @@ mod save; mod send; mod write; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/email/message/template/command/reply.rs b/src/email/message/template/command/reply.rs index c7f40fa..940759a 100644 --- a/src/email/message/template/command/reply.rs +++ b/src/email/message/template/command/reply.rs @@ -1,7 +1,7 @@ -use anyhow::{anyhow, Result}; use clap::Parser; +use color_eyre::{eyre::eyre, Result}; use email::backend::feature::BackendFeatureSource; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; @@ -73,7 +73,7 @@ impl TemplateReplyCommand { .get_messages(folder, &[id]) .await? .first() - .ok_or(anyhow!("cannot find message {id}"))? + .ok_or(eyre!("cannot find message {id}"))? .to_reply_tpl_builder(account_config) .with_headers(self.headers.raw) .with_body(self.body.raw()) diff --git a/src/email/message/template/command/save.rs b/src/email/message/template/command/save.rs index c9a8945..4c4a718 100644 --- a/src/email/message/template/command/save.rs +++ b/src/email/message/template/command/save.rs @@ -1,9 +1,9 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; use mml::MmlCompilerBuilder; use std::io::{self, BufRead, IsTerminal}; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/template/command/send.rs b/src/email/message/template/command/send.rs index ea40a33..4c5a988 100644 --- a/src/email/message/template/command/send.rs +++ b/src/email/message/template/command/send.rs @@ -1,9 +1,9 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::backend::feature::BackendFeatureSource; -use log::info; use mml::MmlCompilerBuilder; use std::io::{self, BufRead, IsTerminal}; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/template/command/write.rs b/src/email/message/template/command/write.rs index a3accc1..8aa9c93 100644 --- a/src/email/message/template/command/write.rs +++ b/src/email/message/template/command/write.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::message::Message; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/email/message/template/mod.rs b/src/email/message/template/mod.rs index 2272a00..94bf7e4 100644 --- a/src/email/message/template/mod.rs +++ b/src/email/message/template/mod.rs @@ -1,7 +1,7 @@ pub mod arg; pub mod command; -use anyhow::Result; +use color_eyre::Result; use email::template::Template; use crate::printer::{Print, WriteColor}; diff --git a/src/folder/command/add.rs b/src/folder/command/add.rs index be3f26e..bf30278 100644 --- a/src/folder/command/add.rs +++ b/src/folder/command/add.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::{backend::feature::BackendFeatureSource, folder::add::AddFolder}; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/folder/command/delete.rs b/src/folder/command/delete.rs index 552d815..38f568e 100644 --- a/src/folder/command/delete.rs +++ b/src/folder/command/delete.rs @@ -1,9 +1,9 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use dialoguer::Confirm; use email::{backend::feature::BackendFeatureSource, folder::delete::DeleteFolder}; -use log::info; use std::process; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/folder/command/expunge.rs b/src/folder/command/expunge.rs index ba10a0f..d28b17f 100644 --- a/src/folder/command/expunge.rs +++ b/src/folder/command/expunge.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::{backend::feature::BackendFeatureSource, folder::expunge::ExpungeFolder}; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/folder/command/list.rs b/src/folder/command/list.rs index 3cf2860..324776f 100644 --- a/src/folder/command/list.rs +++ b/src/folder/command/list.rs @@ -1,7 +1,7 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use email::{backend::feature::BackendFeatureSource, folder::list::ListFolders}; -use log::info; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/folder/command/mod.rs b/src/folder/command/mod.rs index 8131574..7affc54 100644 --- a/src/folder/command/mod.rs +++ b/src/folder/command/mod.rs @@ -4,7 +4,7 @@ mod expunge; mod list; mod purge; -use anyhow::Result; +use color_eyre::Result; use clap::Subcommand; use crate::{config::TomlConfig, printer::Printer}; diff --git a/src/folder/command/purge.rs b/src/folder/command/purge.rs index ec40bec..b36c02f 100644 --- a/src/folder/command/purge.rs +++ b/src/folder/command/purge.rs @@ -1,9 +1,9 @@ -use anyhow::Result; use clap::Parser; +use color_eyre::Result; use dialoguer::Confirm; use email::{backend::feature::BackendFeatureSource, folder::purge::PurgeFolder}; -use log::info; use std::process; +use tracing::info; #[cfg(feature = "account-sync")] use crate::cache::arg::disable::CacheDisableFlag; diff --git a/src/folder/mod.rs b/src/folder/mod.rs index 577abab..db35ec0 100644 --- a/src/folder/mod.rs +++ b/src/folder/mod.rs @@ -2,7 +2,7 @@ pub mod arg; pub mod command; pub mod config; -use anyhow::Result; +use color_eyre::Result; use serde::Serialize; use std::ops; diff --git a/src/imap/wizard.rs b/src/imap/wizard.rs index 0d7df13..53296b4 100644 --- a/src/imap/wizard.rs +++ b/src/imap/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::{Confirm, Input, Password, Select}; #[cfg(feature = "account-discovery")] use email::account::discover::config::{AuthenticationType, AutoConfig, SecurityType, ServerType}; diff --git a/src/lib.rs b/src/lib.rs index fcc7d23..429283e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ pub mod printer; pub mod sendmail; #[cfg(feature = "smtp")] pub mod smtp; +pub mod tracing; pub mod ui; #[doc(inline)] diff --git a/src/maildir/wizard.rs b/src/maildir/wizard.rs index 2ba3af4..20c0a26 100644 --- a/src/maildir/wizard.rs +++ b/src/maildir/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::Input; use dirs::home_dir; use email::maildir::config::MaildirConfig; diff --git a/src/main.rs b/src/main.rs index ab43025..15689ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,35 @@ -use anyhow::Result; +use std::env; + use clap::Parser; -use env_logger::{Builder as LoggerBuilder, Env, DEFAULT_FILTER_ENV}; +use color_eyre::{Result, Section}; use himalaya::{ cli::Cli, config::TomlConfig, envelope::command::list::ListEnvelopesCommand, message::command::mailto::MessageMailtoCommand, printer::StdoutPrinter, }; -use log::{debug, trace}; +use tracing::{debug, level_filters::LevelFilter, trace}; #[tokio::main] async fn main() -> Result<()> { + if env::var("RUST_LOG").is_err() { + if std::env::args().any(|arg| arg == "--debug") { + env::set_var("RUST_LOG", "debug"); + } + if std::env::args().any(|arg| arg == "--trace") { + env::set_var("RUST_LOG", "trace"); + } + } + let cli = Cli::parse(); + + let filter = himalaya::tracing::install()?; + + let mut printer = StdoutPrinter::new(cli.output, cli.color); + #[cfg(not(target_os = "windows"))] if let Err((_, err)) = coredump::register_panic_handler() { - debug!("cannot register coredump panic handler: {err}"); - trace!("{err:?}"); + trace!("cannot register coredump panic handler: {err}"); + debug!("{err:?}"); } - LoggerBuilder::new() - .parse_env(Env::new().filter_or(DEFAULT_FILTER_ENV, "warn")) - .format_timestamp(None) - .init(); - // if the first argument starts by "mailto:", execute straight the // mailto message command let mailto = std::env::args() @@ -35,10 +45,7 @@ async fn main() -> Result<()> { .await; } - let cli = Cli::parse(); - let mut printer = StdoutPrinter::new(cli.output, cli.color); - - match cli.command { + let mut res = match cli.command { Some(cmd) => cmd.execute(&mut printer, cli.config_paths.as_ref()).await, None => { let config = TomlConfig::from_paths_or_default(cli.config_paths.as_ref()).await?; @@ -46,5 +53,15 @@ async fn main() -> Result<()> { .execute(&mut printer, &config) .await } - } + }; + + if filter < LevelFilter::DEBUG { + res = res.note("Run with --debug to enable logs with spantrace."); + }; + + if filter < LevelFilter::TRACE { + res = res.note("Run with --trace to enable verbose logs with backtrace.") + }; + + res } diff --git a/src/manual/command.rs b/src/manual/command.rs index 637982d..bcbafba 100644 --- a/src/manual/command.rs +++ b/src/manual/command.rs @@ -1,9 +1,9 @@ -use anyhow::Result; use clap::{CommandFactory, Parser}; use clap_mangen::Man; -use log::info; +use color_eyre::Result; use shellexpand_utils::{canonicalize, expand}; use std::{fs, path::PathBuf}; +use tracing::info; use crate::{cli::Cli, printer::Printer}; diff --git a/src/notmuch/wizard.rs b/src/notmuch/wizard.rs index 4f7d2aa..ba0505f 100644 --- a/src/notmuch/wizard.rs +++ b/src/notmuch/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::Input; use email::notmuch::config::NotmuchConfig; diff --git a/src/output/output.rs b/src/output/output.rs index 9fbc1ff..3ff1383 100644 --- a/src/output/output.rs +++ b/src/output/output.rs @@ -1,5 +1,5 @@ -use anyhow::{anyhow, Error, Result}; use clap::ValueEnum; +use color_eyre::{eyre::eyre, eyre::Error, Result}; use serde::Serialize; use std::{ fmt, @@ -23,7 +23,7 @@ impl FromStr for OutputFmt { match fmt { fmt if fmt.eq_ignore_ascii_case("json") => Ok(Self::Json), fmt if fmt.eq_ignore_ascii_case("plain") => Ok(Self::Plain), - unknown => Err(anyhow!("cannot parse output format {}", unknown)), + unknown => Err(eyre!("cannot parse output format {}", unknown)), } } } @@ -69,7 +69,7 @@ impl FromStr for ColorFmt { fmt if fmt.eq_ignore_ascii_case("always") => Ok(Self::Always), fmt if fmt.eq_ignore_ascii_case("ansi") => Ok(Self::Ansi), fmt if fmt.eq_ignore_ascii_case("auto") => Ok(Self::Auto), - unknown => Err(anyhow!("cannot parse color format {}", unknown)), + unknown => Err(eyre!("cannot parse color format {}", unknown)), } } } diff --git a/src/printer/print.rs b/src/printer/print.rs index 54bbde7..81360f2 100644 --- a/src/printer/print.rs +++ b/src/printer/print.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use color_eyre::{eyre::Context, Result}; use crate::printer::WriteColor; diff --git a/src/printer/print_table.rs b/src/printer/print_table.rs index e371d9b..5b58379 100644 --- a/src/printer/print_table.rs +++ b/src/printer/print_table.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use email::email::config::EmailTextPlainFormat; use std::io; use termcolor::{self, StandardStream}; diff --git a/src/printer/printer.rs b/src/printer/printer.rs index ef00dd0..13cf90e 100644 --- a/src/printer/printer.rs +++ b/src/printer/printer.rs @@ -1,5 +1,5 @@ -use anyhow::{Context, Error, Result}; use clap::ArgMatches; +use color_eyre::{eyre::Context, Report, Result}; use std::fmt::{self, Debug}; use termcolor::StandardStream; @@ -87,7 +87,7 @@ impl From for StdoutPrinter { } impl TryFrom<&ArgMatches> for StdoutPrinter { - type Error = Error; + type Error = Report; fn try_from(m: &ArgMatches) -> Result { let fmt: OutputFmt = m diff --git a/src/sendmail/wizard.rs b/src/sendmail/wizard.rs index edb366f..e6418df 100644 --- a/src/sendmail/wizard.rs +++ b/src/sendmail/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::Input; use email::sendmail::config::SendmailConfig; diff --git a/src/smtp/wizard.rs b/src/smtp/wizard.rs index 6084c46..f22605f 100644 --- a/src/smtp/wizard.rs +++ b/src/smtp/wizard.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::{Confirm, Input, Password, Select}; #[cfg(feature = "account-discovery")] use email::account::discover::config::{AuthenticationType, AutoConfig, SecurityType, ServerType}; diff --git a/src/tracing.rs b/src/tracing.rs new file mode 100644 index 0000000..4a61a7f --- /dev/null +++ b/src/tracing.rs @@ -0,0 +1,40 @@ +use color_eyre::eyre::Result; +use std::env; +use tracing_error::ErrorLayer; +use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*, EnvFilter}; + +pub fn install() -> Result { + let fmt_layer = fmt::layer().with_target(false); + + let (filter_layer, current_filter) = match EnvFilter::try_from_default_env() { + Err(_) => (EnvFilter::try_new("warn").unwrap(), LevelFilter::OFF), + Ok(layer) => { + let level = layer.max_level_hint().unwrap_or(LevelFilter::OFF); + (layer, level) + } + }; + + let registry = tracing_subscriber::registry() + .with(filter_layer) + .with(ErrorLayer::default()); + + if current_filter == LevelFilter::OFF { + registry.with(fmt_layer.without_time()).init() + } else { + registry.with(fmt_layer).init() + } + + if env::var("RUST_BACKTRACE").is_err() && current_filter == LevelFilter::TRACE { + env::set_var("RUST_BACKTRACE", "1"); + } + + let debug = current_filter >= LevelFilter::DEBUG; + + color_eyre::config::HookBuilder::new() + .capture_span_trace_by_default(debug) + .display_location_section(debug) + .display_env_section(false) + .install()?; + + Ok(current_filter) +} diff --git a/src/ui/choice.rs b/src/ui/choice.rs index 99d9728..9d56e62 100644 --- a/src/ui/choice.rs +++ b/src/ui/choice.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::Result; use dialoguer::Select; use super::THEME; diff --git a/src/ui/editor.rs b/src/ui/editor.rs index 1553e9a..e8a3164 100644 --- a/src/ui/editor.rs +++ b/src/ui/editor.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use color_eyre::{eyre::Context, Result}; use email::{ account::config::AccountConfig, email::utils::{local_draft_path, remove_local_draft}, @@ -6,10 +6,10 @@ use email::{ folder::DRAFTS, template::Template, }; -use log::debug; use mml::MmlCompilerBuilder; use process::SingleCommand; use std::{env, fs, sync::Arc}; +use tracing::debug; use crate::{ backend::Backend, diff --git a/src/ui/table/table.rs b/src/ui/table/table.rs index 7cdaac5..775c4ee 100644 --- a/src/ui/table/table.rs +++ b/src/ui/table/table.rs @@ -4,11 +4,11 @@ //! //! [builder design pattern]: https://refactoring.guru/design-patterns/builder -use anyhow::{Context, Result}; +use color_eyre::{eyre::Context, Result}; use email::email::config::EmailTextPlainFormat; -use log::trace; use termcolor::{Color, ColorSpec}; use terminal_size::terminal_size; +use tracing::trace; use unicode_width::UnicodeWidthStr; use crate::printer::{Print, PrintTableOpts, WriteColor};