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 16, 2021
1 parent ba08bb3 commit 93bbaba
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
42 changes: 35 additions & 7 deletions crates/youki/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Default Youki Logger

use anyhow::{bail, Context, Result};
use log::LevelFilter;
use std::env;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::process::exit;
use std::str::FromStr;

/// If in debug mode, default level is debug to get maximum logging
#[cfg(debug_assertions)]
Expand All @@ -19,12 +23,37 @@ 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 log_level_from_env = match env::var("YOUKI_LOG_LEVEL") {
Ok(val) => val,
Err(err) => {
log::error!("unknown log level env: {}", err);
exit(1)
}
};
let log_level = if log_debug_flag {
"debug"
} else if log_level_from_env != "" {
&log_level_from_env
} else {
DEFAULT_LOG_LEVEL
};
let log_level = match LevelFilter::from_str(log_level) {
Ok(val) => val,
Err(err) => {
log::error!("error: {}", err);
exit(1)
}
};
let target = if let Some(log_file) = log_file {
let file = OpenOptions::new()
.create(true)
Expand All @@ -36,12 +65,11 @@ 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()
.filter_level(log_level)
.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 93bbaba

Please sign in to comment.