From 295d6f7801af0a7714bf7b7409c602586a6885b9 Mon Sep 17 00:00:00 2001 From: Rabindra Dhakal Date: Sat, 9 Nov 2024 15:47:58 +0545 Subject: [PATCH] fix(log): write info to stdout --- src/core/config.rs | 3 ++- src/core/log.rs | 34 ++++++++++++++++++++++++++++++++-- src/lib.rs | 11 ++++++++--- src/main.rs | 7 +------ src/registry/mod.rs | 16 ++++++++-------- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/core/config.rs b/src/core/config.rs index 290402e..a0207ff 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -8,6 +8,7 @@ use std::{ use anyhow::Result; use serde::{Deserialize, Serialize}; +use tracing::error; use super::{ constant::REGISTRY_PATH, @@ -129,7 +130,7 @@ pub fn generate_default_config() -> Result<()> { let config_path = PathBuf::from(home_config).join("soar").join("config.json"); if config_path.exists() { - eprintln!("Default config already exists. Not overriding it."); + error!("Default config already exists. Not overriding it."); std::process::exit(1); } diff --git a/src/core/log.rs b/src/core/log.rs index f7e2a18..98cf207 100644 --- a/src/core/log.rs +++ b/src/core/log.rs @@ -3,7 +3,7 @@ use tracing_subscriber::{ fmt::{ self, format::{FmtSpan, Writer}, - FmtContext, FormatEvent, FormatFields, + FmtContext, FormatEvent, FormatFields, MakeWriter, }, registry::LookupSpan, }; @@ -68,6 +68,36 @@ where } } +struct WriterBuilder { + stdout: std::io::Stdout, + stderr: std::io::Stderr, +} + +impl WriterBuilder { + fn new() -> Self { + Self { + stdout: std::io::stdout(), + stderr: std::io::stderr(), + } + } +} + +impl<'a> MakeWriter<'a> for WriterBuilder { + type Writer = Box; + + fn make_writer(&'a self) -> Self::Writer { + Box::new(self.stdout.lock()) + } + + fn make_writer_for(&'a self, meta: &tracing::Metadata<'_>) -> Self::Writer { + if meta.level() == &tracing::Level::INFO { + Box::new(self.stdout.lock()) + } else { + Box::new(self.stderr.lock()) + } + } +} + pub fn setup_logging(args: &Args) { let filter_level = if args.quiet { Level::ERROR @@ -87,7 +117,7 @@ pub fn setup_logging(args: &Args) { .with_file(false) .with_line_number(false) .with_span_events(FmtSpan::NONE) - .with_writer(std::io::stderr) + .with_writer(WriterBuilder::new()) .compact() .without_time(); diff --git a/src/lib.rs b/src/lib.rs index 925c67c..f7bd140 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,7 @@ mod misc; mod package; mod registry; -pub async fn init() -> Result<()> { +async fn handle_cli() -> Result<()> { let args = Args::parse(); setup_logging(&args); @@ -79,8 +79,7 @@ pub async fn init() -> Result<()> { .await?; } Commands::Sync => { - // nothing to do here - // it can be used to force sync without doing any other operation + registry.await?; } Commands::Remove { packages, exact } => { registry.await?.remove_packages(&packages, exact).await?; @@ -138,3 +137,9 @@ pub async fn init() -> Result<()> { Ok(()) } + +pub async fn init() { + if let Err(e) = handle_cli().await { + error!("{}", e); + } +} diff --git a/src/main.rs b/src/main.rs index 9e171ec..6bef831 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,4 @@ -#![allow(clippy::needless_return)] - use soar_cli::init; -use tracing::error; #[tokio::main] async fn main() { @@ -9,7 +6,5 @@ async fn main() { libc::signal(libc::SIGPIPE, libc::SIG_DFL); } - if let Err(e) = init().await { - error!("{}", e); - } + init().await; } diff --git a/src/registry/mod.rs b/src/registry/mod.rs index 4631d5e..73631a6 100644 --- a/src/registry/mod.rs +++ b/src/registry/mod.rs @@ -238,12 +238,14 @@ impl PackageRegistry { let pkg_image = get_package_image_string(&pkg).await; - let mut printable = Vec::new(); let indent = 32; - printable.extend(pkg_image.as_bytes()); - printable.extend(cursor::Up(15).to_string().as_bytes()); - printable.extend(cursor::Right(indent).to_string().as_bytes()); + info!( + "{}{}{}", + pkg_image, + cursor::Up(15), + cursor::Right(indent).to_string() + ); data.iter().for_each(|(k, v)| { let value = strip_ansi_escapes::strip_str(v); @@ -256,13 +258,11 @@ impl PackageRegistry { indent, ); - printable.extend(format!("{}\n", line).as_bytes()); - printable.extend(cursor::Right(indent).to_string().as_bytes()); + info!("{}{}", cursor::Right(indent).to_string(), line); } }); - printable.extend(cursor::Down(1).to_string().as_bytes()); - info!("{}", String::from_utf8(printable).unwrap()); + info!("{}", cursor::Down(1).to_string()); } Ok(()) }