Skip to content

Commit 99e14e0

Browse files
committed
show running command on env LOG_LEVEL=debug
1 parent 4a6b4a5 commit 99e14e0

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

src/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use color_eyre::{
88
};
99
use directories::ProjectDirs;
1010
use kdl::KdlDocument;
11-
use tracing::debug;
1211

1312
use crate::{Cli, PKG_NAME};
1413

@@ -171,7 +170,7 @@ impl Config {
171170
iptables_prefix,
172171
};
173172

174-
debug!("CProxy config: {r:#?}");
173+
tracing::info!("CProxy config: {r:#?}");
175174
Ok(r)
176175
}
177176
}

src/guards.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl IpRuleGuard {
264264
update_fwmark(&ipv6_cmd, Action::Add, &fwmark, &table);
265265
}
266266
}
267-
if receiver.recv_timeout(Duration::from_secs(1)).is_ok() {
267+
if receiver.recv_timeout(Duration::from_millis(500)).is_ok() {
268268
break;
269269
}
270270
}

src/iproute2.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use strum_macros::{EnumString, IntoStaticStr};
1010

11-
use crate::errors::IPRoute2Error;
11+
use crate::{errors::IPRoute2Error, utils::command_to_string};
1212

1313
pub type Result<T, E = IPRoute2Error> = core::result::Result<T, E>;
1414

@@ -231,11 +231,11 @@ impl IPRoute2 {
231231
I: IntoIterator<Item = S>,
232232
S: AsRef<OsStr>,
233233
{
234-
let output = self
235-
.command()?
236-
.args(args)
237-
.output()
238-
.expect("run ip command error");
234+
let mut cmd = self.command()?;
235+
cmd.args(args);
236+
237+
tracing::debug!("$ {}", &command_to_string(&cmd));
238+
let output = cmd.output()?;
239239
if !output.status.success() {
240240
return Err(IPRoute2Error::from(output));
241241
}

src/iptables.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use lazy_regex::regex;
3030
use nix::fcntl::{flock, FlockArg};
3131
use semver::Version;
3232

33-
use crate::errors::IptablesError;
33+
use crate::{errors::IptablesError, utils::command_to_string};
3434

3535
// List of built-in chains taken from: man 8 iptables
3636
const BUILTIN_CHAINS_FILTER: &[&str] = &["INPUT", "FORWARD", "OUTPUT"];
@@ -479,7 +479,9 @@ impl IPTables {
479479
let output;
480480

481481
if self.has_wait {
482-
output = cmd.arg("--wait").output()?;
482+
cmd.arg("--wait");
483+
tracing::debug!("$ {}", &command_to_string(cmd));
484+
output = cmd.output()?;
483485
} else {
484486
file_lock = Some(File::create("/var/run/xtables_old.lock")?);
485487

@@ -499,6 +501,7 @@ impl IPTables {
499501
}
500502
}
501503
}
504+
tracing::debug!("$ {}", &command_to_string(cmd));
502505
output = cmd.output()?;
503506
}
504507

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod guards;
1111
mod iproute2;
1212
mod iptables;
1313
mod proxy;
14+
mod utils;
1415

1516
pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");
1617

src/utils.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::process::Command;
2+
3+
pub fn command_to_string(cmd: &Command) -> String {
4+
let prog_env = cmd
5+
.get_envs()
6+
.map(|(k, v)| {
7+
if let Some(value) = v {
8+
format!("{}={}", k.to_string_lossy(), value.to_string_lossy())
9+
} else {
10+
format!("{}", k.to_string_lossy())
11+
}
12+
})
13+
.collect::<Vec<String>>()
14+
.join(" ");
15+
let prog_name = cmd.get_program().to_string_lossy();
16+
let prog_args = cmd
17+
.get_args()
18+
.map(|i| {
19+
let arg = i.to_string_lossy();
20+
if arg.chars().any(|it| it.is_ascii_whitespace()) {
21+
format!("\"{}\"", arg.escape_default())
22+
} else {
23+
arg.to_string()
24+
}
25+
})
26+
.collect::<Vec<String>>()
27+
.join(" ");
28+
29+
let mut result = String::new();
30+
if !prog_env.is_empty() {
31+
result.push_str(&prog_env);
32+
result.push(' ');
33+
}
34+
result.push_str(&prog_name);
35+
result.push(' ');
36+
result.push_str(&prog_args);
37+
38+
result
39+
}

0 commit comments

Comments
 (0)