add tracing
This commit is contained in:
parent
9ae0630db4
commit
a06790a2af
7 changed files with 413 additions and 17 deletions
|
|
@ -57,18 +57,20 @@ pub struct AppendCmd {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, clap::Subcommand)]
|
||||
#[command(flatten_help = true)]
|
||||
pub enum Subcommand {
|
||||
Append(AppendCmd),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, clap::Parser)]
|
||||
#[command(version, about, author, arg_required_else_help(true))]
|
||||
#[command(version, about, author)]
|
||||
#[command(arg_required_else_help(true), args_override_self(true))]
|
||||
#[command(propagate_version = true)]
|
||||
pub struct Args {
|
||||
#[arg(long, default_value = "auto")]
|
||||
#[arg(long, global(true), default_value = "auto")]
|
||||
pub color: ColorChoice,
|
||||
|
||||
#[arg(long)]
|
||||
#[arg(long, global(true), default_value = "./configuration.nix")]
|
||||
pub file: Arc<OsStr>,
|
||||
|
||||
#[command(subcommand)]
|
||||
|
|
|
|||
15
src/color.rs
15
src/color.rs
|
|
@ -6,10 +6,14 @@ use std::{
|
|||
#[allow(unused_imports)]
|
||||
use crate::prelude::*;
|
||||
|
||||
pub static CLI_ENABLE_COLOR: OnceLock<bool> = OnceLock::new();
|
||||
/// The actual, final value for whether color should be used, based on CLI and environment values.
|
||||
pub static SHOULD_COLOR: LazyLock<bool> = LazyLock::new(|| is_clicolor_forced() || is_color_reqd());
|
||||
|
||||
/// Initialized from the `--color` value from the CLI, along with `io::stdin().is_terminal()`.
|
||||
pub static _CLI_ENABLE_COLOR: OnceLock<bool> = OnceLock::new();
|
||||
|
||||
fn is_color_reqd() -> bool {
|
||||
CLI_ENABLE_COLOR.get().copied().unwrap_or(false)
|
||||
_CLI_ENABLE_COLOR.get().copied().unwrap_or(false)
|
||||
}
|
||||
|
||||
fn is_clicolor_forced() -> bool {
|
||||
|
|
@ -24,8 +28,6 @@ fn is_clicolor_forced() -> bool {
|
|||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub static SHOULD_COLOR: LazyLock<bool> = LazyLock::new(|| is_clicolor_forced() || is_color_reqd());
|
||||
|
||||
/// Silly wrapper around LazyLock<&'static str> to impl Display.
|
||||
pub(crate) struct _LazyLockDisplay(LazyLock<&'static str>);
|
||||
impl Display for _LazyLockDisplay {
|
||||
|
|
@ -47,8 +49,5 @@ pub(crate) const ANSI_CYAN: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(||
|
|||
}));
|
||||
|
||||
pub(crate) const ANSI_RESET: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
||||
SHOULD_COLOR
|
||||
// C'mon rustfmt, just format it to match ^.
|
||||
.then_some("\x1b[0m")
|
||||
.unwrap_or_default()
|
||||
SHOULD_COLOR.then_some("\x1b[0m").unwrap_or_default()
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ pub(crate) mod prelude {
|
|||
pub use fs_err::os::unix::fs::{FileExt, OpenOptionsExt};
|
||||
|
||||
pub use tap::{Pipe, Tap};
|
||||
|
||||
pub use tracing::{Level, debug, error, info, trace, warn};
|
||||
}
|
||||
|
||||
use prelude::*;
|
||||
|
|
@ -34,7 +36,7 @@ use prelude::*;
|
|||
pub mod args;
|
||||
pub use args::{AppendCmd, Args};
|
||||
mod color;
|
||||
pub use color::{CLI_ENABLE_COLOR, SHOULD_COLOR};
|
||||
pub use color::{_CLI_ENABLE_COLOR, SHOULD_COLOR};
|
||||
pub mod line;
|
||||
mod nixcmd;
|
||||
pub use line::Line;
|
||||
|
|
@ -47,6 +49,7 @@ use crate::source::SourceFile;
|
|||
|
||||
pub const ASCII_WHITESPACE: &[char] = &['\t', '\n', '\x0C', '\r', ' '];
|
||||
|
||||
#[tracing::instrument(level = "debug")]
|
||||
pub fn do_append(args: Arc<Args>, append_args: AppendCmd) -> Result<(), BoxDynError> {
|
||||
let filepath = Path::new(&args.file);
|
||||
let filepath: PathBuf = if filepath.is_relative() && !filepath.starts_with("./") {
|
||||
|
|
|
|||
14
src/main.rs
14
src/main.rs
|
|
@ -3,13 +3,14 @@ use std::process::ExitCode;
|
|||
use std::{error::Error as StdError, sync::Arc};
|
||||
|
||||
use clap::{ColorChoice, Parser as _};
|
||||
use tracing_human_layer::HumanLayer;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use tracing_subscriber::{EnvFilter, layer::SubscriberExt};
|
||||
|
||||
fn main_wrapped() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
|
||||
let args = Arc::new(dynix::Args::parse());
|
||||
|
||||
dbg!(&args);
|
||||
|
||||
let success = dynix::CLI_ENABLE_COLOR.set(match args.color {
|
||||
let success = dynix::_CLI_ENABLE_COLOR.set(match args.color {
|
||||
ColorChoice::Always => true,
|
||||
ColorChoice::Auto => io::stdin().is_terminal(),
|
||||
ColorChoice::Never => false,
|
||||
|
|
@ -18,6 +19,13 @@ fn main_wrapped() -> Result<(), Box<dyn StdError + Send + Sync + 'static>> {
|
|||
success.expect("logic error in CLI_ENABLE_COLOR");
|
||||
}
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(HumanLayer::new().with_color_output(*dynix::SHOULD_COLOR))
|
||||
.with(EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
tracing::debug!("Parsed command-line arguments: {args:?}");
|
||||
|
||||
use dynix::args::Subcommand::*;
|
||||
match &args.subcommand {
|
||||
Append(append_args) => dynix::do_append(args.clone(), append_args.clone())?,
|
||||
|
|
|
|||
|
|
@ -102,6 +102,11 @@ pub struct SourceFile {
|
|||
impl SourceFile {
|
||||
/// Panics if `path` is a directory path instead of a file path.
|
||||
pub fn open_from(path: Arc<Path>, options: OpenOptions) -> Result<Self, IoError> {
|
||||
trace!(
|
||||
"SourceFile::open_from(path={:?}, options={:?})",
|
||||
path,
|
||||
options.options(),
|
||||
);
|
||||
assert!(path.file_name().is_some());
|
||||
|
||||
let file = Arc::new(Mutex::new(options.open(&*path)?));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue