pub fn init_log(options: &CliOptions) {
    // builds the logger from cli or the default one from platform specifics
    let logger = &options.clf_logger;

    // options depend on wheter we need to reset the log
    let writable = if options.reset_log {
        OpenOptions::new()
            .write(true)
            .truncate(true)
            .create(true)
            .open(logger)
    } else {
        OpenOptions::new().append(true).create(true).open(logger)
    };

    // check for opening or creation error
    if let Err(ref e) = writable {
        Nagios::exit_critical(&format!(
            "unable to open or create log file {:?}, error {}",
            logger, e
        ));
    }

    // initialize logger
    match WriteLogger::init(
        options.logger_level,
        simplelog::ConfigBuilder::new()
            .set_time_format("%Y-%b-%d %H:%M:%S.%f".to_string())
            .build(),
        writable.unwrap(),
    ) {
        Ok(_) => (),
        Err(e) => {
            Nagios::exit_critical(&format!(
                "unable to create log file: {}, error: {}",
                logger.display(),
                e
            ));
        }
    };

    // check if we have to delete the log, because it's bigger than max logger size
    let metadata = std::fs::metadata(&logger)
        .expect_critical(&format!("error on metadata() API, path {:?}", &logger));

    debug!("current logger size is: {} bytes", metadata.len());
    if metadata.len() > options.max_logger_size {
        if let Err(e) = std::fs::remove_file(&logger) {
            // 'not found' could be a viable error
            if e.kind() != std::io::ErrorKind::NotFound {
                error!("unable to delete logger file: {:?}, error: {}", &logger, e);
            }
        } else {
            info!("deleting logger file {:?}", &logger);
        }
    }

    // useful traces
    info!(
        "=============================> using configuration file: {:?}",
        &options.config_file
    );
    info!("options: {:?}", &options);
}
/// Create new logger and optionally delete logfile is bigger than cli value
Create new logger and optionally delete logfile is bigger than cli value
Create new logger and optionally delete logfile is bigger than cli value
pub fn init_log(options: &CliOptions) {
    // builds the logger from cli or the default one from platform specifics
    let logger = &options.clf_logger;
    // options depend on wheter we need to reset the log
    let writable = if options.reset_log {
        OpenOptions::new()
            .write(true)
            .truncate(true)
            .create(true)
            .open(logger)
    } else {
        OpenOptions::new().append(true).create(true).open(logger)
    };
    // check for opening or creation error
    if let Err(ref e) = writable {
        Nagios::exit_critical(&format!(
            "unable to open or create log file {:?}, error {}",
            logger, e
        ));
    }
    // initialize logger
    match WriteLogger::init(
        options.logger_level,
        simplelog::ConfigBuilder::new()
            .set_time_format("%Y-%b-%d %H:%M:%S.%f".to_string())
            .build(),
        writable.unwrap(),
    ) {
        Ok(_) => (),
        Err(e) => {
            Nagios::exit_critical(&format!(
                "unable to create log file: {}, error: {}",
                logger.display(),
                e
            ));
        }
    };
    // check if we have to delete the log, because it's bigger than max logger size
    let metadata = std::fs::metadata(&logger)
        .expect_critical(&format!("error on metadata() API, path {:?}", &logger));
    debug!("current logger size is: {} bytes", metadata.len());
    if metadata.len() > options.max_logger_size {
        if let Err(e) = std::fs::remove_file(&logger) {
            // 'not found' could be a viable error
            if e.kind() != std::io::ErrorKind::NotFound {
                error!("unable to delete logger file: {:?}, error: {}", &logger, e);
            }
        } else {
            info!("deleting logger file {:?}", &logger);
        }
    }
    // useful traces
    info!(
        "=============================> using configuration file: {:?}",
        &options.config_file
    );
    info!("options: {:?}", &options);
}
pub fn init_log ( options : & CliOptions ) { // builds the logger from cli or the default one from platform specifics let logger = & options . clf_logger ; // options depend on wheter we need to reset the log let writable = if options . reset_log { OpenOptions :: new ( ) . write ( true ) . truncate ( true ) . create ( true ) . open ( logger ) } else { OpenOptions :: new ( ) . append ( true ) . create ( true ) . open ( logger ) } ; // check for opening or creation error if let Err ( ref e ) = writable { Nagios :: exit_critical ( & format ! ( " " logger e ) ) ; } // initialize logger match WriteLogger :: init ( options . logger_level , simplelog :: ConfigBuilder :: new ( ) . set_time_format ( " " . to_string ( ) ) . build ( ) , writable . unwrap ( ) , ) { Ok ( _ ) => ( ) , Err ( e ) => { Nagios :: exit_critical ( & format ! ( " " logger display ( ) e ) ) ; } } ; // check if we have to delete the log, because it's bigger than max logger size let metadata = std :: fs :: metadata ( & logger ) . expect_critical ( & format ! ( " " logger ) ) ; debug ! ( " " metadata len ( ) ) ; if metadata . len ( ) > options . max_logger_size { if let Err ( e ) = std :: fs :: remove_file ( & logger ) { // 'not found' could be a viable error if e . kind ( ) != std :: io :: ErrorKind :: NotFound { error ! ( " " logger e ) ; } } else { info ! ( " " logger ) ; } } // useful traces info ! ( " " options config_file ) ; info ! ( " " options ) ; }
Create new logger and optionally delete logfile is bigger than cli value
Create new logger and optionally delete logfile is bigger than cli value
options
&CliOptions
options
&CliOptions