Skip to content

Commit

Permalink
Change where logging is init
Browse files Browse the repository at this point in the history
- Needed to change because going to using current working directory to
    decide where to put logs thus need that to be set before starting
    logging.
- So removed log messages during folder setting as they cannot be seen
    and would just be misleading.
- Added new log message after setting current working directory. Also
    marks the start of the program.
  • Loading branch information
c-git committed Nov 17, 2023
1 parent 2034793 commit 446b723
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
15 changes: 2 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{env, path::PathBuf};
use std::path::PathBuf;

use anyhow::Context;
use clap::{Parser, ValueEnum};
use log::{debug, info, LevelFilter};
use log::LevelFilter;

#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)]
#[command(
Expand All @@ -29,20 +29,9 @@ impl Cli {
}
/// Changes the current working directory to path if one is given
pub fn update_current_working_dir(&self) -> anyhow::Result<()> {
debug!(
"Before attempting update current dir, it is: {}",
env::current_dir()?.display()
);
if let Some(path) = &self.working_dir {
info!("Going to update working directory to to '{path}'");
std::env::set_current_dir(path)
.with_context(|| format!("Failed to set current dir to: '{path}'"))?;
info!(
"After updating current dir, it is: '{}'",
env::current_dir()?.display()
);
} else {
debug!("No user supplied path found. No change")
}
Ok(())
}
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod cli;
mod config;
mod event_recorder;
mod logging;
mod notification;
mod ping;
mod state_management;
Expand All @@ -22,13 +23,20 @@ pub(crate) use crate::{
};
use anyhow::Context;
use event_recorder::{ResponseMessage, TargetID};
use log::debug;
use log::{debug, warn};

pub use crate::{cli::Cli, event_recorder::TimestampedResponse};

pub fn run(cli: Cli) -> anyhow::Result<()> {
cli.update_current_working_dir()
.context("Failed to update current working directory")?;
logging::init_logging(cli.log_level.into())?;
warn!(
"Starting up in dir: {:?}",
std::env::current_dir()
.context("Failed to get cwd")?
.display()
);
let config = Config::load_from(&cli.get_config_path()).context("Failed to load config")?;

let (tx, rx) = mpsc::channel();
Expand Down
9 changes: 0 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
use clap::Parser;
use conn_mon::{run, Cli};
use env_logger::Builder;
use log::LevelFilter;

fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
init_logging(cli.log_level.into())?;
run(cli)?;
Ok(())
}

fn init_logging(level: LevelFilter) -> anyhow::Result<()> {
// TODO: Change to log to file
Builder::new().filter(None, level).try_init()?;
Ok(())
}

0 comments on commit 446b723

Please sign in to comment.