diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1583064c..8277afe8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -72,7 +72,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { ## Logging -Debug logging in starship is done with [pretty_env_logger](https://crates.io/crates/pretty_env_logger). +Debug logging in starship is done with our custom logger implementation. To run starship with debug logs, set the `STARSHIP_LOG` environment variable to the log level needed. For example, to enable the trace logs, run the following: diff --git a/Cargo.lock b/Cargo.lock index 1dd7b4f1..cac5cc1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -351,19 +351,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f" -[[package]] -name = "env_logger" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "fake-simd" version = "0.1.2" @@ -463,15 +450,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "idna" version = "0.2.0" @@ -887,16 +865,6 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" -[[package]] -name = "pretty_env_logger" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" -dependencies = [ - "env_logger", - "log", -] - [[package]] name = "proc-macro2" version = "1.0.19" @@ -906,12 +874,6 @@ dependencies = [ "unicode-xid 0.2.1", ] -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quick-xml" version = "0.19.0" @@ -1187,8 +1149,8 @@ dependencies = [ "path-slash", "pest", "pest_derive", - "pretty_env_logger", "quick-xml", + "rand", "rayon", "regex", "serde_json", @@ -1306,15 +1268,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "termcolor" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" -dependencies = [ - "winapi-util", -] - [[package]] name = "textwrap" version = "0.11.0" @@ -1480,15 +1433,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index f3fe8fc5..aa5a6ffe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,8 +35,7 @@ git2 = { version = "0.13.11", default-features = false } toml = { version = "0.5.6", features = ["preserve_order"] } serde_json = "1.0.57" rayon = "1.4.0" -pretty_env_logger = "0.4.0" -log = "0.4.11" +log = { version = "0.4.11", features = ["std"] } # battery is optional (on by default) because the crate doesn't currently build for Termux # see: https://github.com/svartalf/rust-battery/issues/33 battery = { version = "0.7.6", optional = true } @@ -58,6 +57,7 @@ open = "1.4.0" unicode-width = "0.1.8" term_size = "0.3.2" quick-xml = "0.19.0" +rand = "0.7.3" notify-rust = { version = "4.0.0", optional = true } # Optional/http: diff --git a/docs/config/README.md b/docs/config/README.md index ba18aeb9..a48dbc3c 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -33,6 +33,21 @@ Equivalently in PowerShell (Windows) would be adding this line to your `$PROFILE $ENV:STARSHIP_CONFIG = "$HOME\.starship" ``` +### Logging + +By default starship logs warnings and errors into a file named `~/.cache/starship/session_${STARSHIP_SESSION_KEY}.log`, where the session key is corresponding to a instance of your terminal. +This, however can be changed using the `STARSHIP_CACHE` environment variable: + +```sh +export STARSHIP_CACHE=~/.starship/cache +``` + +Equivalently in PowerShell (Windows) would be adding this line to your `$PROFILE`: + +```ps1 +$ENV:STARSHIP_CACHE = "$HOME\AppData\Local\Temp" +``` + ### Terminology **Module**: A component in the prompt giving information based on contextual information from your OS. For example, the "nodejs" module shows the version of NodeJS that is currently installed on your computer, if your current directory is a NodeJS project. diff --git a/src/config.rs b/src/config.rs index 9099011a..e6d3a4d4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -199,7 +199,7 @@ impl StarshipConfig { fn config_from_file() -> Option { let file_path = if let Ok(path) = env::var("STARSHIP_CONFIG") { // Use $STARSHIP_CONFIG as the config path if available - log::debug!("STARSHIP_CONFIG is set: \n{}", &path); + log::debug!("STARSHIP_CONFIG is set: {}", &path); path } else { // Default to using ~/.config/starship.toml @@ -212,22 +212,22 @@ impl StarshipConfig { let toml_content = match utils::read_file(&file_path) { Ok(content) => { - log::trace!("Config file content: \n{}", &content); + log::trace!("Config file content: \"\n{}\"", &content); Some(content) } Err(e) => { - log::debug!("Unable to read config file content: \n{}", &e); + log::debug!("Unable to read config file content: {}", &e); None } }?; match toml::from_str(&toml_content) { Ok(parsed) => { - log::debug!("Config parsed: \n{:?}", &parsed); + log::debug!("Config parsed: {:?}", &parsed); Some(parsed) } Err(error) => { - log::debug!("Unable to parse the config file: {}", error); + log::error!("Unable to parse the config file: {}", error); None } } @@ -238,7 +238,7 @@ impl StarshipConfig { let module_config = self.get_config(&[module_name]); if module_config.is_some() { log::debug!( - "Config found for \"{}\": \n{:?}", + "Config found for \"{}\": {:?}", &module_name, &module_config ); @@ -302,7 +302,7 @@ impl StarshipConfig { let module_config = self.get_config(&["custom", module_name]); if module_config.is_some() { log::debug!( - "Custom config found for \"{}\": \n{:?}", + "Custom config found for \"{}\": {:?}", &module_name, &module_config ); diff --git a/src/configs/custom.rs b/src/configs/custom.rs index 31317f1c..315542b2 100644 --- a/src/configs/custom.rs +++ b/src/configs/custom.rs @@ -52,7 +52,7 @@ impl<'a> ModuleConfig<'a> for Files<'a> { if let Some(file) = item.as_str() { files.push(file); } else { - log::debug!("Unexpected file {:?}", item); + log::warn!("Unexpected file {:?}", item); } } @@ -68,7 +68,7 @@ impl<'a> ModuleConfig<'a> for Extensions<'a> { if let Some(file) = item.as_str() { extensions.push(file); } else { - log::debug!("Unexpected extension {:?}", item); + log::warn!("Unexpected extension {:?}", item); } } @@ -84,7 +84,7 @@ impl<'a> ModuleConfig<'a> for Directories<'a> { if let Some(file) = item.as_str() { directories.push(file); } else { - log::debug!("Unexpected directory {:?}", item); + log::warn!("Unexpected directory {:?}", item); } } diff --git a/src/configure.rs b/src/configure.rs index efd9007d..ac9b93e3 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -80,7 +80,6 @@ pub fn edit_configuration() { environment variables correctly?", editor_cmd ); - eprintln!("Full error: {:?}", error); std::process::exit(1) } other_error => panic!("failed to open file: {:?}", other_error), diff --git a/src/init/starship.bash b/src/init/starship.bash index 822a3b0b..f7bd2d9d 100644 --- a/src/init/starship.bash +++ b/src/init/starship.bash @@ -84,3 +84,6 @@ fi # Set up the start time and STARSHIP_SHELL, which controls shell-specific sequences STARSHIP_START_TIME=$(::STARSHIP:: time) export STARSHIP_SHELL="bash" + +# Set up the session key that will be used to store logs +export STARSHIP_SESSION_KEY=$(::STARSHIP:: session) diff --git a/src/init/starship.fish b/src/init/starship.fish index 6daa473d..8236f548 100644 --- a/src/init/starship.fish +++ b/src/init/starship.fish @@ -16,3 +16,6 @@ set VIRTUAL_ENV_DISABLE_PROMPT 1 function fish_mode_prompt; end export STARSHIP_SHELL="fish" + +# Set up the session key that will be used to store logs +export STARSHIP_SESSION_KEY=(::STARSHIP:: session) diff --git a/src/init/starship.ion b/src/init/starship.ion index 0d979df2..5a03ab2e 100644 --- a/src/init/starship.ion +++ b/src/init/starship.ion @@ -15,3 +15,6 @@ end # Export the correct name of the shell export STARSHIP_SHELL="ion" + +# Set up the session key that will be used to store logs +export STARSHIP_SESSION_KEY=$(::STARSHIP:: session) diff --git a/src/init/starship.ps1 b/src/init/starship.ps1 index 558d9c5c..74c8f952 100644 --- a/src/init/starship.ps1 +++ b/src/init/starship.ps1 @@ -54,3 +54,6 @@ function global:prompt { } $ENV:STARSHIP_SHELL = "powershell" + +# Set up the session key that will be used to store logs +$ENV:STARSHIP_SESSION_KEY = $(::STARSHIP:: session) diff --git a/src/init/starship.zsh b/src/init/starship.zsh index ed8452de..5ecb33d1 100644 --- a/src/init/starship.zsh +++ b/src/init/starship.zsh @@ -62,3 +62,6 @@ zle-keymap-select() { STARSHIP_START_TIME=$(::STARSHIP:: time) zle -N zle-keymap-select export STARSHIP_SHELL="zsh" + +# Set up the session key that will be used to store logs +export STARSHIP_SESSION_KEY=$(::STARSHIP:: session) diff --git a/src/lib.rs b/src/lib.rs index 0b98721b..15a38074 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod config; pub mod configs; pub mod context; pub mod formatter; +pub mod logger; pub mod module; pub mod modules; pub mod print; diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 00000000..e17a368c --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,112 @@ +use ansi_term::Color; +use log::{Level, LevelFilter, Metadata, Record}; +use std::{ + collections::HashSet, + env, + fs::{self, File, OpenOptions}, + io::Write, + path::PathBuf, + sync::{Arc, Mutex}, +}; + +pub struct StarshipLogger { + log_file: Arc>, + log_file_content: Arc>, + log_level: Level, +} + +impl StarshipLogger { + fn new() -> Self { + let log_dir = env::var_os("STARSHIP_CACHE") + .map(PathBuf::from) + .unwrap_or_else(|| { + dirs_next::home_dir() + .expect("Unable to find home directory") + .join(".cache/starship") + }); + + fs::create_dir_all(&log_dir).expect("Unable to create log dir!"); + let session_log_file = log_dir.join(format!( + "session_{}.log", + env::var("STARSHIP_SESSION_KEY").unwrap_or_default() + )); + + Self { + log_file_content: Arc::new( + fs::read_to_string(&session_log_file) + .unwrap_or_default() + .lines() + .map(|line| line.to_string()) + .collect(), + ), + log_file: Arc::new(Mutex::new( + OpenOptions::new() + .create(true) + .append(true) + .open(session_log_file) + .unwrap(), + )), + log_level: env::var("STARSHIP_LOG") + .map(|level| match level.to_lowercase().as_str() { + "trace" => Level::Trace, + "debug" => Level::Debug, + "info" => Level::Info, + "warn" => Level::Warn, + "error" => Level::Error, + _ => Level::Warn, + }) + .unwrap_or_else(|_| Level::Warn), + } + } +} + +impl log::Log for StarshipLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= self.log_level + } + + fn log(&self, record: &Record) { + let to_print = format!( + "[{}] - ({}): {}", + record.level(), + record.module_path().unwrap_or_default(), + record.args() + ); + + if record.metadata().level() <= Level::Warn { + self.log_file + .lock() + .map(|mut file| writeln!(file, "{}", to_print)) + .expect("Log file writer mutex was poisoned!") + .expect("Unable to write to the log file!"); + } + + if self.enabled(record.metadata()) && !self.log_file_content.contains(to_print.as_str()) { + eprintln!( + "[{}] - ({}): {}", + match record.level() { + Level::Trace => Color::Black.dimmed().paint(format!("{}", record.level())), + Level::Debug => Color::Black.paint(format!("{}", record.level())), + Level::Info => Color::White.paint(format!("{}", record.level())), + Level::Warn => Color::Yellow.paint(format!("{}", record.level())), + Level::Error => Color::Red.paint(format!("{}", record.level())), + }, + record.module_path().unwrap_or_default(), + record.args() + ); + } + } + + fn flush(&self) { + self.log_file + .lock() + .map(|mut writer| writer.flush()) + .expect("Log file writer mutex was poisoned!") + .expect("Unable to flush the log file!"); + } +} + +pub fn init() { + log::set_boxed_logger(Box::new(StarshipLogger::new())).unwrap(); + log::set_max_level(LevelFilter::Trace); +} diff --git a/src/main.rs b/src/main.rs index 3afcf6b3..46dc86e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod configure; mod context; mod formatter; mod init; +mod logger; mod module; mod modules; mod print; @@ -20,9 +21,11 @@ mod test; use crate::module::ALL_MODULES; use clap::{App, AppSettings, Arg, Shell, SubCommand}; +use rand::distributions::Alphanumeric; +use rand::Rng; fn main() { - pretty_env_logger::init_custom_env("STARSHIP_LOG"); + logger::init(); let status_code_arg = Arg::with_name("status_code") .short("s") @@ -153,7 +156,8 @@ fn main() { .required(true) .env("STARSHIP_SHELL"), ), - ); + ) + .subcommand(SubCommand::with_name("session").about("Generate random session key")); let matches = app.clone().get_matches(); @@ -209,6 +213,13 @@ fn main() { app.gen_completions_to("starship", shell, &mut io::stdout().lock()); } + ("session", _) => println!( + "{}", + rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(16) + .collect::() + ), (command, _) => unreachable!("Invalid subcommand: {}", command), } } diff --git a/src/modules/aws.rs b/src/modules/aws.rs index b404a2a2..a73dc756 100644 --- a/src/modules/aws.rs +++ b/src/modules/aws.rs @@ -108,7 +108,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { module.set_segments(match parsed { Ok(segments) => segments, Err(error) => { - log::error!("Error in module `aws`: \n{}", error); + log::warn!("Error in module `aws`: \n{}", error); return None; } }); diff --git a/src/modules/battery.rs b/src/modules/battery.rs index 0ce96d8c..2b773bf2 100644 --- a/src/modules/battery.rs +++ b/src/modules/battery.rs @@ -85,7 +85,7 @@ fn get_battery_status() -> Option { }) } Err(e) => { - log::debug!("Unable to access battery information:\n{}", &e); + log::warn!("Unable to access battery information:\n{}", &e); None } }) diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index 8684b0aa..03731084 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -11,11 +11,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("cmd_duration"); let config: CmdDurationConfig = CmdDurationConfig::try_load(module.config); - /* TODO: Once error handling is implemented, warn the user if their config - min time is nonsensical */ if config.min_time < 0 { - log::debug!( - "[WARN]: min_time in [cmd_duration] ({}) was less than zero", + log::warn!( + "min_time in [cmd_duration] ({}) was less than zero", config.min_time ); return None; diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 3b977ff0..39048024 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -136,7 +136,7 @@ fn is_readonly_dir(path: &Path) -> bool { Ok(res) => !res, Err(e) => { log::debug!( - "Failed to detemine read only status of directory '{:?}': {}", + "Failed to determine read only status of directory '{:?}': {}", path, e ); diff --git a/src/modules/dotnet.rs b/src/modules/dotnet.rs index 0e0ad52b..71e58748 100644 --- a/src/modules/dotnet.rs +++ b/src/modules/dotnet.rs @@ -321,7 +321,7 @@ fn get_latest_sdk_from_cli() -> Option { None => { // Older versions of the dotnet cli do not support the --list-sdks command // So, if the status code indicates failure, fall back to `dotnet --version` - log::warn!( + log::debug!( "Received a non-success exit code from `dotnet --list-sdks`. \ Falling back to `dotnet --version`.", ); diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index bcebd83e..0610b530 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -14,8 +14,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { let truncation_symbol = get_first_grapheme(config.truncation_symbol); - // TODO: Once error handling is implemented, warn the user if their config - // truncation length is nonsensical let len = if config.truncation_length <= 0 { log::warn!( "\"truncation_length\" should be a positive value, found {}", diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index 5e3ecb8a..9daf94c0 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -144,7 +144,7 @@ impl<'a> GitStatusInfo<'a> { return match result.as_ref() { Ok(ahead_behind) => Some(*ahead_behind), Err(error) => { - log::warn!("Warn: get_ahead_behind: {}", error); + log::debug!("get_ahead_behind: {}", error); None } }; @@ -159,7 +159,7 @@ impl<'a> GitStatusInfo<'a> { match data.as_ref().unwrap() { Ok(ahead_behind) => Some(*ahead_behind), Err(error) => { - log::warn!("Warn: get_ahead_behind: {}", error); + log::debug!("get_ahead_behind: {}", error); None } } @@ -173,7 +173,7 @@ impl<'a> GitStatusInfo<'a> { return match result.as_ref() { Ok(repo_status) => Some(*repo_status), Err(error) => { - log::warn!("Warn: get_repo_status: {}", error); + log::debug!("get_repo_status: {}", error); None } }; @@ -187,7 +187,7 @@ impl<'a> GitStatusInfo<'a> { match data.as_ref().unwrap() { Ok(repo_status) => Some(*repo_status), Err(error) => { - log::warn!("Warn: get_repo_status: {}", error); + log::debug!(" get_repo_status: {}", error); None } } @@ -201,7 +201,7 @@ impl<'a> GitStatusInfo<'a> { return match result.as_ref() { Ok(stashed_count) => Some(*stashed_count), Err(error) => { - log::warn!("Warn: get_stashed_count: {}", error); + log::debug!("get_stashed_count: {}", error); None } }; @@ -215,7 +215,7 @@ impl<'a> GitStatusInfo<'a> { match data.as_ref().unwrap() { Ok(stashed_count) => Some(*stashed_count), Err(error) => { - log::warn!("Warn: get_stashed_count: {}", error); + log::debug!("get_stashed_count: {}", error); None } } @@ -356,7 +356,7 @@ where .parse(None) .ok() } else { - log::error!("Error parsing format string `{}`", &config_path); + log::warn!("Error parsing format string `{}`", &config_path); None } } diff --git a/src/modules/hg_branch.rs b/src/modules/hg_branch.rs index 7d29d622..9b6c211a 100644 --- a/src/modules/hg_branch.rs +++ b/src/modules/hg_branch.rs @@ -24,8 +24,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; }; - // TODO: Once error handling is implemented, warn the user if their config - // truncation length is nonsensical let len = if config.truncation_length <= 0 { log::warn!( "\"truncation_length\" should be a positive value, found {}", diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index cacbaff2..feefbf5d 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -24,7 +24,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let host = match os_hostname.into_string() { Ok(host) => host, Err(bad) => { - log::debug!("hostname is not valid UTF!\n{:?}", bad); + log::warn!("hostname is not valid UTF!\n{:?}", bad); return None; } }; diff --git a/src/utils.rs b/src/utils.rs index 122dd63c..0c8a4013 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -253,7 +253,7 @@ fn internal_exec_cmd(cmd: &str, args: &[&str]) -> Option { }) } Err(error) => { - log::trace!("Executing command {:?} failed by: {:?}", cmd, error); + log::warn!("Executing command {:?} failed by: {:?}", cmd, error); None } } diff --git a/starship_module_config_derive/src/lib.rs b/starship_module_config_derive/src/lib.rs index db2e275e..b6d11326 100644 --- a/starship_module_config_derive/src/lib.rs +++ b/starship_module_config_derive/src/lib.rs @@ -1,6 +1,3 @@ -extern crate proc_macro; -extern crate proc_macro2; - use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, DeriveInput}; @@ -50,6 +47,9 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream { fn load_config(&self, config: &'a toml::Value) -> Self { let mut new_module_config = self.clone(); if let toml::Value::Table(config) = config { + if config.get("prefix").is_some() || config.get("suffix").is_some() { + log::warn!("You're using the outdated config format! Migrate your config here: https://starship.rs/migrating-to-0.45.0/") + } #load_tokens } new_module_config