Skip to content

Commit

Permalink
logging: Make output bare of level/target/fields (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
srid authored Sep 13, 2023
1 parent b07afac commit d62e240
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ reqwest = { version = "0.11", features = ["blocking", "json"] }
try-guard = "0.2.0"
clap = { version = "4.4", features = ["derive"] }
urlencoding = "2.1.3"
nix_rs = { version = "0.1.7", features = ["ssr"] }
nix_rs = { version = "0.1.8", features = ["ssr"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracing = "0.1.37"

Expand Down
23 changes: 3 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
pub mod cli;
pub mod config;
pub mod github;
pub mod logging;
pub mod nix;

use std::{collections::HashSet, io};
use std::collections::HashSet;

use cli::CliArgs;
use colored::Colorize;
use nix::{
devour_flake::{DevourFlakeOutput, DrvOut},
url::FlakeUrl,
};
use tracing::{instrument, Level};
use tracing::instrument;

/// Run nixci on the given [CliArgs], returning the built outputs in sorted order.
#[instrument(name = "nixci", skip(args))]
Expand Down Expand Up @@ -51,21 +52,3 @@ async fn nixci_subflake(
}
Ok(outs)
}

pub fn setup_logging(verbose: bool) {
let env_filter = if verbose {
"nixci=debug,nix_rs=debug"
} else {
"nixci=info,nix_rs=info"
};
let builder = tracing_subscriber::fmt()
.with_writer(io::stderr)
.with_max_level(Level::INFO)
.with_env_filter(env_filter)
.compact();
if !verbose {
builder.without_time().init()
} else {
builder.init()
}
}
53 changes: 53 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::fmt;
use std::io;

use tracing::{Event, Level, Subscriber};
use tracing_subscriber::fmt::format;
use tracing_subscriber::{
fmt::{FmtContext, FormatEvent, FormatFields},
registry::LookupSpan,
};

/// A [tracing_subscriber] event formatter that suppresses everything but the
/// log message.
///
/// Level/target are displayed for non-INFO messages.
struct BareFormatter;

impl<S, N> FormatEvent<S, N> for BareFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: format::Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
let metadata = event.metadata();
if metadata.level() != &Level::INFO {
write!(&mut writer, "{} {}: ", metadata.level(), metadata.target())?;
}
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}

pub fn setup_logging(verbose: bool) {
let env_filter = if verbose {
"nixci=debug,nix_rs=debug"
} else {
"nixci=info,nix_rs=info"
};
let builder = tracing_subscriber::fmt()
.with_writer(io::stderr)
.with_max_level(Level::INFO)
.with_env_filter(env_filter);

if !verbose {
builder.event_format(BareFormatter).init();
} else {
builder.init()
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nixci::cli;
#[tokio::main]
async fn main() -> Result<()> {
let args = cli::CliArgs::parse();
nixci::setup_logging(args.verbose);
nixci::logging::setup_logging(args.verbose);
let _outs = nixci::nixci(args).await?;
Ok(())
}
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod integration_test {

#[ctor::ctor]
fn init() {
nixci::setup_logging(true);
nixci::logging::setup_logging(true);
}

#[tokio::test]
Expand Down

0 comments on commit d62e240

Please sign in to comment.