From aaac00f20862f4ba3b97207ccc8d8079f6209e2e Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Thu, 25 Nov 2021 16:15:49 +0800 Subject: [PATCH] SS_LOG_VERBOSE_LEVEL and SS_LOG_WITHOUT_TIME environment variable ref #691 --- README.md | 5 +++++ bin/common/config.rs | 3 ++- bin/common/logging/mod.rs | 22 +++++++++++++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7e46c07e..dd353946 100644 --- a/README.md +++ b/README.md @@ -579,6 +579,11 @@ Example configuration: } ``` +### Environment Variables + +- `SS_LOG_VERBOSE_LEVEL`: Logging level for binaries (`sslocal`, `ssserver` and `ssmanager`). It is valid only when command line argument `-v` is not applied. Example: `SS_LOG_VERBOSE_LEVEL=1` +- `SS_LOG_WITHOUT_TIME`: Logging format for binaries (`sslocal`, `ssserver` and `ssmanager`). It is valid only when command line argument `--log-without-time` is not applied. Example `SS_LOG_WITHOUT_TIME=1` + ## Supported Ciphers ### AEAD Ciphers diff --git a/bin/common/config.rs b/bin/common/config.rs index 75210c7c..1bfa2a11 100644 --- a/bin/common/config.rs +++ b/bin/common/config.rs @@ -30,7 +30,8 @@ pub fn get_default_config_path() -> Option { } // UNIX global configuration file - if cfg!(unix) { + #[cfg(unix)] + { let global_config_path = Path::new("/etc/shadowsocks-rust/config.json"); if global_config_path.exists() { return Some(global_config_path.to_path_buf()); diff --git a/bin/common/logging/mod.rs b/bin/common/logging/mod.rs index 14e6854c..34a4e0fd 100644 --- a/bin/common/logging/mod.rs +++ b/bin/common/logging/mod.rs @@ -1,6 +1,6 @@ //! Loggin facilities -use std::path::Path; +use std::{env, path::Path}; use clap::ArgMatches; use log::LevelFilter; @@ -18,8 +18,24 @@ where } pub fn init_with_config(bin_name: &str, matches: &ArgMatches) { - let debug_level = matches.occurrences_of("VERBOSE"); - let without_time = matches.is_present("LOG_WITHOUT_TIME"); + let mut debug_level = matches.occurrences_of("VERBOSE"); + if debug_level == 0 { + // Override by SS_LOG_VERBOSE_LEVEL + if let Ok(verbose_level) = env::var("SS_LOG_VERBOSE_LEVEL") { + if let Ok(verbose_level) = verbose_level.parse::() { + debug_level = verbose_level; + } + } + } + + let mut without_time = matches.is_present("LOG_WITHOUT_TIME"); + if !without_time { + if let Ok(log_without_time) = env::var("SS_LOG_WITHOUT_TIME") { + if let Ok(log_without_time) = log_without_time.parse::() { + without_time = log_without_time != 0; + } + } + } let mut pattern = String::new(); if !without_time {