Skip to content

Commit

Permalink
feat: add debug flag
Browse files Browse the repository at this point in the history
for #454
  • Loading branch information
unknowndevQwQ committed Nov 15, 2021
1 parent 3d7c9fa commit 059a8e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions crates/youki/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::{bail, Context, Result};
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;

/// If in debug mode, default level is debug to get maximum logging
#[cfg(debug_assertions)]
Expand All @@ -19,12 +20,18 @@ const LOG_FORMAT_JSON: &str = "json";
/// Initialize the logger, must be called before accessing the logger
/// Multiple parts might call this at once, but the actual initialization
/// is done only once due to use of OnceCell
pub fn init(log_file: Option<PathBuf>, log_format: Option<String>) -> Result<()> {
pub fn init(log_debug_flag: bool, log_file: Option<PathBuf>, log_format: Option<String>) -> Result<()> {
let formatter = match log_format.as_deref() {
None | Some(LOG_FORMAT_TEXT) => text_write,
Some(LOG_FORMAT_JSON) => json_write,
Some(unknown) => bail!("unknown log format: {}", unknown),
};
let from_env_log_level = env_logger::Env::new().filter("YOUKI_LOG_LEVEL");
let log_level = if log_debug_flag {
"debug"
} else {
DEFAULT_LOG_LEVEL
};
let target = if let Some(log_file) = log_file {
let file = OpenOptions::new()
.create(true)
Expand All @@ -36,12 +43,12 @@ pub fn init(log_file: Option<PathBuf>, log_format: Option<String>) -> Result<()>
} else {
env_logger::Target::Stderr
};
env_logger::Builder::from_env(
env_logger::Env::default().filter_or("YOUKI_LOG_LEVEL", DEFAULT_LOG_LEVEL),
)
.format(formatter)
.target(target)
.init();
env_logger::Builder::new()
.parse_env(from_env_log_level)
.filter_level(FromStr::from_str(log_level).unwrap())
.format(formatter)
.target(target)
.init();

Ok(())
}
Expand Down
7 changes: 6 additions & 1 deletion crates/youki/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ use nix::unistd::getuid;
#[derive(Parser, Debug)]
#[clap(version = crate_version!(), author = "youki team")]
struct Opts {
// I don't know how to get the log level when the --debug flag is not set (I want to show some default values on the help page when the options are not set)
// Example: '--debug change log level to debug. (default: "warn")'
/// change log level to debug.
#[clap(long)]
debug: bool,
#[clap(short, long)]
log: Option<PathBuf>,
#[clap(long)]
Expand Down Expand Up @@ -104,7 +109,7 @@ fn main() -> Result<()> {

let opts = Opts::parse();

if let Err(e) = crate::logger::init(opts.log, opts.log_format) {
if let Err(e) = crate::logger::init(opts.debug, opts.log, opts.log_format) {
eprintln!("log init failed: {:?}", e);
}

Expand Down

0 comments on commit 059a8e9

Please sign in to comment.