Skip to content

Commit

Permalink
fix(log): write info to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Nov 9, 2024
1 parent 424b0e3 commit 295d6f7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{

use anyhow::Result;
use serde::{Deserialize, Serialize};
use tracing::error;

use super::{
constant::REGISTRY_PATH,
Expand Down Expand Up @@ -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);
}

Expand Down
34 changes: 32 additions & 2 deletions src/core/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tracing_subscriber::{
fmt::{
self,
format::{FmtSpan, Writer},
FmtContext, FormatEvent, FormatFields,
FmtContext, FormatEvent, FormatFields, MakeWriter,
},
registry::LookupSpan,
};
Expand Down Expand Up @@ -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<dyn std::io::Write + 'a>;

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

Expand Down
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -138,3 +137,9 @@ pub async fn init() -> Result<()> {

Ok(())
}

pub async fn init() {
if let Err(e) = handle_cli().await {
error!("{}", e);
}
}
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#![allow(clippy::needless_return)]

use soar_cli::init;
use tracing::error;

#[tokio::main]
async fn main() {
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}

if let Err(e) = init().await {
error!("{}", e);
}
init().await;
}
16 changes: 8 additions & 8 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(())
}
Expand Down

0 comments on commit 295d6f7

Please sign in to comment.