view rust-launcher/src/log_helper.rs @ 1555:7ef277699fd1

Implemented proper file logging * rust-launcher/src/dirs_paths_helper.rs: AdvancedLogging moved to log_helper; Linux::new(false) redeclared new to Linux::new(false, false); * rust-launcher/src/log_helper.rs: now harbor AdvancedLogging as it should since beggining log_impl now logs to fille only if log_to_file is true, logs to correct file, format time-stamp a bit better, creates a parent directory * rust-launcher/src/main.rs: get_os functions redeclared to (debug: bool, al: bool) and now calls new(debug, al) main no longer creates AdvancedLogging but calls proper get_os * rust-launcher/src/os_access.rs: added trait method of (advanced_logging), implementations now keep AdvancedLogging variable - dummy or loaded from properties as requested during creation * rust-launcher/src/property_from_files_resolver.rs: adapted to log_to_file -> log_to_file refactoring * rust-launcher/src/utils.rs: try_custom_logdir_from_properties renamed to try_logtarget_from_properties and now returns final log file. New method of (logfile_name) whic compses itw-like name for new log file. TestLogger implements (unimplemented) advanced_logging function
author Jiri Vanek <jvanek@redhat.com>
date Sun, 17 Feb 2019 10:35:53 +0100
parents 65de3b40a457
children bfafe88b8719
line wrap: on
line source

//this module was created as std::io::Write; and std::fmt::Write; hcat be imoted together
//adn still, there are different methods. Notably writeln is only in io version. but format! is only in fmt version
use os_access;
use std::fs::OpenOptions;
use std::io::Write;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use std::fs::File;
use property_from_files_resolver;

static mut FIRST: bool = true;

//0 critical
//1 info
//2 debug only
pub fn log_impl(level: i32, os: &os_access::Os, s: &str) {
    if level == 0 {} else if level == 1 {
        println!("{}", s);
    } else if level == 2 {
        if os.is_verbose() {
            println!("{}", s);
        }
    }
    if os.advanced_logging().log_to_file {
        unsafe {
            if FIRST {
                FIRST = false;
                std::fs::create_dir_all(os.advanced_logging().log_target_file.parent().expect("hard to imagine log file without parent"));
                let start = SystemTime::now();
                let t = start.duration_since(UNIX_EPOCH).expect("time should be measureable");
                let mut file = File::create(&os.advanced_logging().log_target_file).expect("failed to create file log");
                let allsec = t.as_secs();
                let sec = allsec % 60;
                let min = (allsec / 60) % 60;
                let h = allsec / (60 * 60);
                if let Err(e) = write!(&mut file, "itw-rust-debug: file log started: {}:{}:{}\n", h, min, sec) {
                    println!("Couldn't write to file: {}", e);
                }
                file.sync_all();
            }
        }
        let mut file = OpenOptions::new()
            .write(true)
            .append(true)
            .open(&os.advanced_logging().log_target_file)
            .expect("failed to append to file log");

        if let Err(e) = writeln!(&mut file, "{}", s) {
            println!("Couldn't write to file: {}", e);
        }
        file.sync_all();
    }
}

pub struct AdvancedLogging {
    pub log_to_file: bool,
    pub log_target_file: std::path::PathBuf,
    pub log_to_stdstreams: bool,
    pub log_to_system: bool,
}

impl Default for AdvancedLogging {
    fn default() -> AdvancedLogging {
        AdvancedLogging {
            log_to_file: false,
            log_target_file: std::path::PathBuf::from("undeffined"),
            log_to_stdstreams: true,
            log_to_system: true,
        }
    }
}

impl AdvancedLogging {
    pub fn load(os: &os_access::Os) -> AdvancedLogging {
        AdvancedLogging {
            log_to_file: property_from_files_resolver::try_log_to_file_from_properties(os),
            log_to_stdstreams: property_from_files_resolver::try_log_to_streams_from_properties(os),
            log_to_system: property_from_files_resolver::try_log_to_system_from_properties(os),
            log_target_file: property_from_files_resolver::try_logtarget_from_properties(os),
        }
    }
}