Skip to content

Commit

Permalink
Write logs to filesystem (#522)
Browse files Browse the repository at this point in the history
* Write logs to filesystem

* ansiterm

* clean up

* update tests

* update more tests
  • Loading branch information
sdankel authored Nov 28, 2023
1 parent 4eaa6f5 commit 9e5fa2c
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 192 deletions.
81 changes: 76 additions & 5 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ name = "fuelup"
path = "src/main.rs"

[dependencies]
ansiterm = "0.12.2"
anyhow = "1"
clap = { version = "4.2.4", features = ["cargo", "derive", "env"] }
clap_complete = "4.2"
Expand All @@ -30,12 +31,15 @@ serde_json = "1.0"
sha2 = "0.10"
tar = "0.4"
tempfile = "3"
termcolor = "1"
time = { version = "0.3", features = ["macros", "parsing", "formatting", "serde-well-known"] }
toml_edit = { version = "0.13", features = ["serde", "easy"] }
tracing = "0.1"
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3", features = ["ansi", "env-filter", "json"] }
ureq = "2.4"

[workspace]
members = ["component", "ci/build-channel", "ci/compare-versions"]

[dev-dependencies]
strip-ansi-escapes = "0.2.0"
36 changes: 10 additions & 26 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use ansiterm::Style;
use tracing::info;

use crate::target_triple::TargetTriple;

// In the below functions, we ignore the `Result`s of `set_color` and `reset` to allow `write`
// to work even when those functions fail to set/reset colors, since `StandardStream::stdout` is
// a wrapper over `std::io::stdout`.

pub fn bold<F>(write: F)
where
F: FnOnce(&mut StandardStream) -> std::io::Result<()>,
{
let mut stdout = StandardStream::stdout(ColorChoice::Auto);
let _ = stdout.set_color(ColorSpec::new().set_bold(true));
write(&mut stdout).expect("Unexpected error writing to stdout");
let _ = stdout.reset();
pub fn bold(text: &str) -> String {
let style = Style::new().bold();
format!("{}", style.paint(text))
}

pub fn colored_bold<F>(color: Color, write: F)
where
F: FnOnce(&mut StandardStream) -> std::io::Result<()>,
{
let mut stdout = StandardStream::stdout(ColorChoice::Auto);
let _ = stdout.set_color(ColorSpec::new().set_fg(Some(color)).set_bold(true));
write(&mut stdout).expect("Unexpected error writing to stdout");
let _ = stdout.reset();
pub fn colored_bold(color: ansiterm::Color, text: &str) -> String {
format!("{}", color.bold().paint(text))
}

pub fn print_header(header: &str) {
let mut stdout = StandardStream::stdout(ColorChoice::Auto);
bold(|s| writeln!(s, "{header}"));
writeln!(stdout, "{}", "-".repeat(header.len())).expect("Unexpected error writing to stdout");
let _ = stdout.reset();
info!("");
info!("{}", bold(header));
info!("{}", "-".repeat(header.len()));
}

pub fn format_toolchain_with_target(toolchain: &str) -> String {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod download;
pub mod file;
pub mod fmt;
pub mod fuelup_cli;
pub mod logging;
pub mod ops;
pub mod path;
pub mod proxy_cli;
Expand Down
54 changes: 54 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::path::{fuelup_log_dir, FUELUP_DIR, FUELUP_HOME};
use std::env;
use tracing::{debug, level_filters::LevelFilter};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};

pub fn log_command() {
debug!(
"Command: {}",
env::args().collect::<Vec<String>>().join(" ")
);
}

pub fn log_environment() {
if let Some(val) = env::var_os("PATH") {
if let Some(fuelup_path) =
env::split_paths(&val).find(|p| p.to_string_lossy().contains(FUELUP_DIR))
{
debug!("PATH includes {}", fuelup_path.to_string_lossy());
} else {
debug!("PATH does not include {}", FUELUP_DIR);
}
}
if let Some(val) = env::var_os(FUELUP_HOME) {
debug!("FUELUP_HOME: {}", val.to_string_lossy());
} else {
debug!("FUELUP_HOME is not set");
}
}

pub fn init_tracing() -> WorkerGuard {
let file_appender = tracing_appender::rolling::hourly(fuelup_log_dir(), "fuelup.log");
let (file_writer, guard) = tracing_appender::non_blocking(file_appender);

tracing_subscriber::Registry::default()
.with(
tracing_subscriber::fmt::Layer::default()
.with_writer(file_writer)
.log_internal_errors(false)
.with_target(false)
.with_filter(LevelFilter::DEBUG),
)
.with(
tracing_subscriber::fmt::Layer::default()
.with_writer(std::io::stdout)
.without_time()
.with_level(false)
.with_target(false)
.with_filter(LevelFilter::INFO),
)
.init();

guard
}
21 changes: 10 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use anyhow::Result;
use fuelup::{fuelup_cli, proxy_cli};
use std::panic;
use std::path::PathBuf;
use fuelup::{
fuelup_cli,
logging::{init_tracing, log_command, log_environment},
proxy_cli,
};
use std::{env, panic, path::PathBuf};
use tracing::error;

fn run() -> Result<()> {
let arg0 = std::env::args().next().map(PathBuf::from);
log_command();
log_environment();
let arg0 = env::args().next().map(PathBuf::from);

let process_name = arg0
.as_ref()
Expand All @@ -30,13 +35,7 @@ fn run() -> Result<()> {
}

fn main() {
let format = tracing_subscriber::fmt::format()
.without_time()
.with_level(false)
.with_target(false);

tracing_subscriber::fmt().event_format(format).init();

let _guard = init_tracing();
if run().is_err() {
std::process::exit(1);
}
Expand Down
Loading

0 comments on commit 9e5fa2c

Please sign in to comment.