Skip to content

Commit

Permalink
Add --logfile FILENAME option
Browse files Browse the repository at this point in the history
  • Loading branch information
kiron1 committed Jan 19, 2025
1 parent 8d473b3 commit 432567c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
5 changes: 1 addition & 4 deletions paclib/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ impl<'a> Engine<'a> {
)
.expect("register_global_property");
}
Self {
js,
my_ip_addr,
}
Self { js, my_ip_addr }
}

pub fn with_pac_script(pac_script: &str) -> Result<Self, PacScriptError> {
Expand Down
25 changes: 12 additions & 13 deletions proxydetox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use tracing_subscriber::filter::EnvFilter;
pub extern "C" fn main() {
let config = Options::load_without_rcfile();

setup_tracing(&config.log_level);

if let Err(error) = run(config) {
tracing::error!(%error, "fatal error");
write_error(&mut std::io::stderr(), error).ok();
Expand All @@ -37,16 +35,14 @@ pub extern "C" fn main() {
fn main() {
let config = Options::load();

setup_tracing(&config.log_level);

if let Err(error) = run(config) {
tracing::error!(%error, "fatal error");
write_error(&mut std::io::stderr(), error).ok();
std::process::exit(1);
}
}

fn setup_tracing(log_level: &tracing::level_filters::LevelFilter) {
fn setup_tracing(log_level: &tracing::level_filters::LevelFilter, logfile: Option<File>) {
let env_name = format!("{}_LOG", env!("CARGO_PKG_NAME").to_uppercase());

let filter = if let Ok(filter) = EnvFilter::try_from_env(&env_name) {
Expand Down Expand Up @@ -78,23 +74,23 @@ fn setup_tracing(log_level: &tracing::level_filters::LevelFilter) {
.parse()
.expect("directive"),
)
.add_directive(
format!("paclib={0}", log_level)
.parse()
.expect("directive"),
)
.add_directive(format!("paclib={0}", log_level).parse().expect("directive"))
.add_directive(
format!("proxy_client={0}", log_level)
.parse()
.expect("directive"),
)
};

tracing_subscriber::fmt()
let fmt = tracing_subscriber::fmt()
.compact()
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_env_filter(filter)
.init();
.with_env_filter(filter);
if let Some(f) = logfile {
fmt.with_writer(f).init();
} else {
fmt.with_writer(std::io::stderr).init();
}
}

fn write_error<W, E>(writer: &mut W, error: E) -> std::io::Result<()>
Expand All @@ -114,6 +110,9 @@ where

#[tokio::main]
async fn run(config: Arc<Options>) -> Result<(), proxydetoxlib::Error> {
let logfile = config.log_filepath.as_ref().map(File::create).transpose()?;

setup_tracing(&config.log_level, logfile);
let auth = match &config.authorization {
#[cfg(feature = "negotiate")]
Authorization::Negotiate(ref negotiate) => {
Expand Down
10 changes: 10 additions & 0 deletions proxydetox/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Authorization {
#[derive(Debug)]
pub struct Options {
pub log_level: LevelFilter,
pub log_filepath: Option<PathBuf>,
pub pac_file: Option<PathOrUri>,
pub my_ip_address: Option<IpAddr>,
pub authorization: Authorization,
Expand Down Expand Up @@ -147,6 +148,14 @@ impl Options {
.action(ArgAction::Count)
.help("Decreases verbosity level"),
)
.arg(
Arg::new("log_filepath")
.long("logfile")
.value_name("FILEPATH")
.help("Log to file instead of stderr")
.value_parser(clap::value_parser!(PathBuf))
.action(clap::ArgAction::Set)
)
.arg(
Arg::new("activate_socket")
.long("activate-socket")
Expand Down Expand Up @@ -346,6 +355,7 @@ impl From<ArgMatches> for Options {

Self {
log_level,
log_filepath: m.get_one("log_filepath").cloned(),
pac_file: m
.get_one::<PathOrUri>("pac_file")
.cloned()
Expand Down

0 comments on commit 432567c

Please sign in to comment.