Skip to content

Commit

Permalink
deps: add tracing feature to remove tracing dep if wanted
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed May 31, 2024
1 parent adfcce4 commit 9f64922
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 38 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rust-version = "1.75.0"
futures = { version = "0.3.30", optional = true }
indexmap = "2.2.6"
tokio = { version = "1.37.0", features = ["io-util", "macros", "process", "rt"], optional = true }
tracing = "0.1.40"
tracing = { version = "0.1.40", optional = true }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", default-features = false, features = ["fs", "poll", "signal"], optional = true }
Expand All @@ -33,7 +33,10 @@ remoteprocess = "0.4.13"
tokio = { version = "1.37.0", features = ["io-util", "macros", "process", "rt", "rt-multi-thread", "time"] }

[features]
default = ["creation-flags", "job-object", "kill-on-drop", "process-group", "process-session"]
default = ["creation-flags", "job-object", "kill-on-drop", "process-group", "process-session", "tracing"]

## Enable internal tracing logs
tracing = ["dep:tracing"]

## Frontend: StdCommandWrap
std = ["dep:nix"]
Expand Down
3 changes: 3 additions & 0 deletions src/generic_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ macro_rules! Wrap {
wrappers: &mut ::indexmap::IndexMap<::std::any::TypeId, Box<dyn $wrapper>>,
) -> ::std::io::Result<Box<dyn $childer>> {
for (id, wrapper) in wrappers.iter_mut() {
#[cfg(feature = "tracing")]
::tracing::debug!(?id, "pre_spawn");
wrapper.pre_spawn(command, self)?;
}

let mut child = command.spawn()?;
for (id, wrapper) in wrappers.iter_mut() {
#[cfg(feature = "tracing")]
::tracing::debug!(?id, "post_spawn");
wrapper.post_spawn(&mut child, self)?;
}
Expand All @@ -98,6 +100,7 @@ macro_rules! Wrap {
) as Box<dyn $childer>;

for (id, wrapper) in wrappers.iter_mut() {
#[cfg(feature = "tracing")]
::tracing::debug!(?id, "wrap_child");
child = wrapper.wrap_child(child, self)?;
}
Expand Down
13 changes: 7 additions & 6 deletions src/std/job_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
time::Duration,
};

#[cfg(feature = "tracing")]
use tracing::{debug, instrument};
use windows::Win32::{
Foundation::{CloseHandle, HANDLE},
Expand Down Expand Up @@ -33,7 +34,7 @@ use super::{StdChildWrapper, StdCommandWrap, StdCommandWrapper};
pub struct JobObject;

impl StdCommandWrapper for JobObject {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn pre_spawn(&mut self, command: &mut Command, core: &StdCommandWrap) -> Result<()> {
let mut flags = CREATE_SUSPENDED;
#[cfg(feature = "creation-flags")]
Expand All @@ -45,7 +46,7 @@ impl StdCommandWrapper for JobObject {
Ok(())
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wrap_child(
&mut self,
inner: Box<dyn StdChildWrapper>,
Expand Down Expand Up @@ -82,7 +83,7 @@ pub struct JobObjectChild {
}

impl JobObjectChild {
#[instrument(level = "debug", skip(job_port))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(job_port)))]
pub(crate) fn new(inner: Box<dyn StdChildWrapper>, job_port: JobPort) -> Self {
Self {
inner,
Expand All @@ -109,12 +110,12 @@ impl StdChildWrapper for JobObjectChild {
self.inner.into_inner()
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn start_kill(&mut self) -> Result<()> {
terminate_job(self.job_port.job, 1)
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wait(&mut self) -> Result<ExitStatus> {
if let ChildExitStatus::Exited(status) = &self.exit_status {
return Ok(*status);
Expand All @@ -133,7 +134,7 @@ impl StdChildWrapper for JobObjectChild {
Ok(status)
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn try_wait(&mut self) -> Result<Option<ExitStatus>> {
wait_on_job(self.job_port.completion_port, Some(Duration::ZERO))?;
self.inner.try_wait()
Expand Down
17 changes: 9 additions & 8 deletions src/std/process_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use nix::{
},
unistd::{setpgid, Pid},
};
#[cfg(feature = "tracing")]
use tracing::instrument;

use crate::ChildExitStatus;
Expand Down Expand Up @@ -61,7 +62,7 @@ pub struct ProcessGroupChild {
}

impl ProcessGroupChild {
#[instrument(level = "debug")]
#[cfg_attr(feature = "tracing", instrument(level = "debug"))]
pub(crate) fn new(inner: Box<dyn StdChildWrapper>, pgid: Pid) -> Self {
Self {
inner,
Expand All @@ -79,7 +80,7 @@ impl ProcessGroupChild {
}

impl StdCommandWrapper for ProcessGroup {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn pre_spawn(&mut self, command: &mut Command, _core: &StdCommandWrap) -> Result<()> {
#[cfg(Std_unstable)]
{
Expand All @@ -99,7 +100,7 @@ impl StdCommandWrapper for ProcessGroup {
Ok(())
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wrap_child(
&mut self,
inner: Box<dyn StdChildWrapper>,
Expand All @@ -112,12 +113,12 @@ impl StdCommandWrapper for ProcessGroup {
}

impl ProcessGroupChild {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn signal_imp(&self, sig: Signal) -> Result<()> {
killpg(self.pgid, sig).map_err(Error::from)
}

#[instrument(level = "debug")]
#[cfg_attr(feature = "tracing", instrument(level = "debug"))]
fn wait_imp(pgid: Pid, flag: WaitPidFlag) -> Result<ControlFlow<Option<ExitStatus>>> {
// wait for processes in a loop until every process in this group has
// exited (this ensures that we reap any zombies that may have been
Expand Down Expand Up @@ -173,12 +174,12 @@ impl StdChildWrapper for ProcessGroupChild {
self.inner.into_inner()
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn start_kill(&mut self) -> Result<()> {
self.signal_imp(Signal::SIGKILL)
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wait(&mut self) -> Result<ExitStatus> {
if let ChildExitStatus::Exited(status) = &self.exit_status {
return Ok(*status);
Expand All @@ -194,7 +195,7 @@ impl StdChildWrapper for ProcessGroupChild {
Ok(status)
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn try_wait(&mut self) -> Result<Option<ExitStatus>> {
if let ChildExitStatus::Exited(status) = &self.exit_status {
return Ok(Some(*status));
Expand Down
5 changes: 3 additions & 2 deletions src/std/process_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use nix::unistd::{setsid, Pid};
#[cfg(feature = "tracing")]
use tracing::instrument;

use super::{StdCommandWrap, StdCommandWrapper};
Expand All @@ -25,7 +26,7 @@ use super::{StdCommandWrap, StdCommandWrapper};
pub struct ProcessSession;

impl StdCommandWrapper for ProcessSession {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn pre_spawn(&mut self, command: &mut Command, _core: &StdCommandWrap) -> Result<()> {
unsafe {
command.pre_exec(move || setsid().map_err(Error::from).map(|_| ()));
Expand All @@ -34,7 +35,7 @@ impl StdCommandWrapper for ProcessSession {
Ok(())
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wrap_child(
&mut self,
inner: Box<dyn super::core::StdChildWrapper>,
Expand Down
6 changes: 6 additions & 0 deletions src/std/reset_sigmask.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{io::Result, os::unix::process::CommandExt, process::Command};

use nix::sys::signal::{sigprocmask, SigSet, SigmaskHow};
#[cfg(feature = "tracing")]
use tracing::trace;

use super::{StdCommandWrap, StdCommandWrapper};
Expand All @@ -18,8 +19,13 @@ impl StdCommandWrapper for ResetSigmask {
command.pre_exec(|| {
let mut oldset = SigSet::empty();
let newset = SigSet::all();

#[cfg(feature = "tracing")]
trace!(unblocking=?newset, "resetting process sigmask");

sigprocmask(SigmaskHow::SIG_UNBLOCK, Some(&newset), Some(&mut oldset))?;

#[cfg(feature = "tracing")]
trace!(?oldset, "sigmask reset");
Ok(())
});
Expand Down
14 changes: 8 additions & 6 deletions src/tokio/job_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tokio::{
process::{Child, Command},
task::spawn_blocking,
};
#[cfg(feature = "tracing")]
use tracing::{debug, instrument};
use windows::Win32::{
Foundation::{CloseHandle, HANDLE},
Expand Down Expand Up @@ -34,7 +35,7 @@ use super::{TokioChildWrapper, TokioCommandWrap, TokioCommandWrapper};
pub struct JobObject;

impl TokioCommandWrapper for JobObject {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn pre_spawn(&mut self, command: &mut Command, core: &TokioCommandWrap) -> Result<()> {
let mut flags = CREATE_SUSPENDED;
#[cfg(feature = "creation-flags")]
Expand All @@ -46,7 +47,7 @@ impl TokioCommandWrapper for JobObject {
Ok(())
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wrap_child(
&mut self,
inner: Box<dyn TokioChildWrapper>,
Expand All @@ -64,6 +65,7 @@ impl TokioCommandWrapper for JobObject {
#[cfg(not(feature = "creation-flags"))]
let create_suspended = false;

#[cfg(feature = "tracing")]
debug!(
?kill_on_drop,
?create_suspended,
Expand Down Expand Up @@ -97,7 +99,7 @@ pub struct JobObjectChild {
}

impl JobObjectChild {
#[instrument(level = "debug", skip(job_port))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(job_port)))]
pub(crate) fn new(inner: Box<dyn TokioChildWrapper>, job_port: JobPort) -> Self {
Self {
inner,
Expand All @@ -124,12 +126,12 @@ impl TokioChildWrapper for JobObjectChild {
self.inner.into_inner()
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn start_kill(&mut self) -> Result<()> {
terminate_job(self.job_port.job, 1)
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wait(&mut self) -> Box<dyn Future<Output = Result<ExitStatus>> + Send + '_> {
Box::new(async {
if let ChildExitStatus::Exited(status) = &self.exit_status {
Expand Down Expand Up @@ -160,7 +162,7 @@ impl TokioChildWrapper for JobObjectChild {
})
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn try_wait(&mut self) -> Result<Option<ExitStatus>> {
wait_on_job(self.job_port.completion_port, Some(Duration::ZERO))?;
self.inner.try_wait()
Expand Down
17 changes: 9 additions & 8 deletions src/tokio/process_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tokio::{
process::{Child, Command},
task::spawn_blocking,
};
#[cfg(feature = "tracing")]
use tracing::instrument;

use crate::ChildExitStatus;
Expand Down Expand Up @@ -66,7 +67,7 @@ pub struct ProcessGroupChild {
}

impl ProcessGroupChild {
#[instrument(level = "debug")]
#[cfg_attr(feature = "tracing", instrument(level = "debug"))]
pub(crate) fn new(inner: Box<dyn TokioChildWrapper>, pgid: Pid) -> Self {
Self {
inner,
Expand All @@ -84,7 +85,7 @@ impl ProcessGroupChild {
}

impl TokioCommandWrapper for ProcessGroup {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn pre_spawn(&mut self, command: &mut Command, _core: &TokioCommandWrap) -> Result<()> {
#[cfg(tokio_unstable)]
{
Expand All @@ -104,7 +105,7 @@ impl TokioCommandWrapper for ProcessGroup {
Ok(())
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wrap_child(
&mut self,
inner: Box<dyn TokioChildWrapper>,
Expand All @@ -124,12 +125,12 @@ impl TokioCommandWrapper for ProcessGroup {
}

impl ProcessGroupChild {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn signal_imp(&self, sig: Signal) -> Result<()> {
killpg(self.pgid, sig).map_err(Error::from)
}

#[instrument(level = "debug")]
#[cfg_attr(feature = "tracing", instrument(level = "debug"))]
fn wait_imp(pgid: Pid, flag: WaitPidFlag) -> Result<ControlFlow<Option<ExitStatus>>> {
// wait for processes in a loop until every process in this group has
// exited (this ensures that we reap any zombies that may have been
Expand Down Expand Up @@ -185,12 +186,12 @@ impl TokioChildWrapper for ProcessGroupChild {
self.inner.into_inner()
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn start_kill(&mut self) -> Result<()> {
self.signal_imp(Signal::SIGKILL)
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wait(&mut self) -> Box<dyn Future<Output = Result<ExitStatus>> + Send + '_> {
Box::new(async {
if let ChildExitStatus::Exited(status) = &self.exit_status {
Expand Down Expand Up @@ -219,7 +220,7 @@ impl TokioChildWrapper for ProcessGroupChild {
})
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn try_wait(&mut self) -> Result<Option<ExitStatus>> {
if let ChildExitStatus::Exited(status) = &self.exit_status {
return Ok(Some(*status));
Expand Down
5 changes: 3 additions & 2 deletions src/tokio/process_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io::{Error, Result};

use nix::unistd::{setsid, Pid};
use tokio::process::Command;
#[cfg(feature = "tracing")]
use tracing::instrument;

use super::{TokioCommandWrap, TokioCommandWrapper};
Expand All @@ -22,7 +23,7 @@ use super::{TokioCommandWrap, TokioCommandWrapper};
pub struct ProcessSession;

impl TokioCommandWrapper for ProcessSession {
#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn pre_spawn(&mut self, command: &mut Command, _core: &TokioCommandWrap) -> Result<()> {
unsafe {
command.pre_exec(move || setsid().map_err(Error::from).map(|_| ()));
Expand All @@ -31,7 +32,7 @@ impl TokioCommandWrapper for ProcessSession {
Ok(())
}

#[instrument(level = "debug", skip(self))]
#[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
fn wrap_child(
&mut self,
inner: Box<dyn super::core::TokioChildWrapper>,
Expand Down
Loading

0 comments on commit 9f64922

Please sign in to comment.