Skip to content

Commit

Permalink
feat: add error messaging (starship#1576)
Browse files Browse the repository at this point in the history
This creates a custom logger for the log crate which logs everything to a file (/tmp/starship/session_$STARSHIP_SESSION_KEY.log) and it logs everything above Warn to stderr, but only if the log file does not contain the line that should be logged resulting in an error or warning to be only logged at the first starship invocation after opening the shell.
  • Loading branch information
Tilmann Meyer authored Sep 28, 2020
1 parent 3c668e6 commit 2233683
Show file tree
Hide file tree
Showing 26 changed files with 188 additions and 97 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {

## 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:

Expand Down
58 changes: 1 addition & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl StarshipConfig {
fn config_from_file() -> Option<Value> {
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
Expand All @@ -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
}
}
Expand All @@ -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
);
Expand Down Expand Up @@ -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
);
Expand Down
6 changes: 3 additions & 3 deletions src/configs/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions src/init/starship.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions src/init/starship.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions src/init/starship.ion
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions src/init/starship.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions src/init/starship.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 2233683

Please sign in to comment.