# HG changeset patch # User Jiri Vanek # Date 1550396153 -3600 # Node ID 7ef277699fd11ee694e8a1026366770be044210f # Parent 65de3b40a457109beb9d60356c02fa78f42f3bb5 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 diff -r 65de3b40a457 -r 7ef277699fd1 ChangeLog --- a/ChangeLog Sat Feb 16 20:56:33 2019 +0100 +++ b/ChangeLog Sun Feb 17 10:35:53 2019 +0100 @@ -1,3 +1,21 @@ +2019-02-17 Jiri Vanek + + 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 + 2019-02-16 Jiri Vanek Implemented basic stub for file logging diff -r 65de3b40a457 -r 7ef277699fd1 rust-launcher/src/dirs_paths_helper.rs --- a/rust-launcher/src/dirs_paths_helper.rs Sat Feb 16 20:56:33 2019 +0100 +++ b/rust-launcher/src/dirs_paths_helper.rs Sun Feb 17 10:35:53 2019 +0100 @@ -1,5 +1,4 @@ use os_access; -use property_from_files_resolver; use std; use std::env; @@ -8,36 +7,6 @@ pub static DEPLOYMENT_PROPERTIES: &'static str = "deployment.properties"; -pub struct AdvancedLogging { - pub log_to_file: bool, - pub user_logdir: 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, - user_logdir: 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), - user_logdir: property_from_files_resolver::try_custom_logdir_from_properties(os) - } - } - - -} pub fn get_xdg_config_dir(os: &os_access::Os) -> Option { match env::var("XDG_CONFIG_HOME") { @@ -118,12 +87,12 @@ #[cfg(not(windows))] fn get_os() -> os_access::Linux { - os_access::Linux::new(false) + os_access::Linux::new(false, false) } #[cfg(windows)] fn get_os() -> os_access::Windows { - os_access::Windows::new(false) + os_access::Windows::new(false, false) } @@ -140,7 +109,7 @@ #[test] #[cfg(not(windows))] fn check_config_files_paths_global() { - let os = os_access::Linux::new(false); + let os = os_access::Linux::new(false, false); let p6 = super::get_itw_global_config_file(&os); assert_ne!(None, p6); println!("{}", p6.clone().expect("unwrap failed").display()); @@ -150,7 +119,7 @@ #[test] #[cfg(not(windows))] fn check_legacy_config_files_paths() { - let os = os_access::Linux::new(false); + let os = os_access::Linux::new(false, false); let p4 = super::get_itw_legacy_config_file(&os); let p5 = super::get_itw_legacy_global_config_file(&os); assert_ne!(None, p4); diff -r 65de3b40a457 -r 7ef277699fd1 rust-launcher/src/log_helper.rs --- a/rust-launcher/src/log_helper.rs Sat Feb 16 20:56:33 2019 +0100 +++ b/rust-launcher/src/log_helper.rs Sun Feb 17 10:35:53 2019 +0100 @@ -1,11 +1,14 @@ +//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; +static mut FIRST: bool = true; //0 critical //1 info @@ -18,29 +21,62 @@ println!("{}", s); } } - unsafe { - if first { - //mkdir - //createfile - //rust itw log initiate dor so - first = false; - let start = SystemTime::now(); - let t = start.duration_since(UNIX_EPOCH).expect("time should be measureable"); - let mut file = File::create("my-file").expect("failed to create file log"); - if let Err(e) = write!(&mut file, "itw-rust-debug: file log started: {}\n", t.as_secs()) { - println!("Couldn't write to file: {}", e); + 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(); } - 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, } } - let mut file = OpenOptions::new() - .write(true) - .append(true) - .open("my-file") - .expect("failed to append to file log"); +} - if let Err(e) = writeln!(&mut file, "{}", s) { - println!("Couldn't write to file: {}", e); +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), + } } - file.sync_all(); } \ No newline at end of file diff -r 65de3b40a457 -r 7ef277699fd1 rust-launcher/src/main.rs --- a/rust-launcher/src/main.rs Sat Feb 16 20:56:33 2019 +0100 +++ b/rust-launcher/src/main.rs Sun Feb 17 10:35:53 2019 +0100 @@ -14,13 +14,13 @@ use std::env; #[cfg(not(windows))] -fn get_os(debug: bool) -> os_access::Linux { - os_access::Linux::new(debug) +fn get_os(debug: bool, al: bool) -> os_access::Linux { + os_access::Linux::new(debug, al) } #[cfg(windows)] -fn get_os(debug: bool) -> os_access::Windows { - os_access::Windows::new(debug) +fn get_os(debug: bool, al: bool) -> os_access::Windows { + os_access::Windows::new(debug, al) } fn is_debug_on() -> bool { @@ -29,7 +29,7 @@ return val; } _none => { - let os = get_os(false); + let os = get_os(false, false); return property_from_files_resolver::try_main_verbose_from_properties(&os); } } @@ -74,9 +74,8 @@ } fn main() { - let os = get_os(is_debug_on()); + let os = get_os(is_debug_on(), true); os.log(&dirs_paths_helper::path_to_string(&dirs_paths_helper::current_program())); - let al = dirs_paths_helper::AdvancedLogging::load(&os); let java_dir = utils::find_jre(&os); let mut info2 = String::new(); write!(&mut info2, "selected jre: {}", java_dir.display()).expect("unwrap failed"); diff -r 65de3b40a457 -r 7ef277699fd1 rust-launcher/src/os_access.rs --- a/rust-launcher/src/os_access.rs Sat Feb 16 20:56:33 2019 +0100 +++ b/rust-launcher/src/os_access.rs Sun Feb 17 10:35:53 2019 +0100 @@ -32,7 +32,7 @@ } pub trait Os { - // logging "api" can change + fn advanced_logging(&self) -> &log_helper::AdvancedLogging; fn log(&self, s: &str); fn info(&self, s: &str); fn get_registry_jdk(&self) -> Option; @@ -57,18 +57,27 @@ #[cfg(not(windows))] pub struct Linux { verbose: bool, + al: log_helper::AdvancedLogging, } #[cfg(not(windows))] impl Linux { - pub fn new(debug: bool) -> Linux { - Linux { verbose: debug } + pub fn new(debug: bool, load_advanced: bool) -> Linux { + if ! load_advanced { + Linux { verbose: debug, al: log_helper::AdvancedLogging::default() } + } else { + Linux { verbose: debug, al: log_helper::AdvancedLogging::load(&Linux::new(debug, false)) } + } } } #[cfg(not(windows))] impl Os for Linux { + fn advanced_logging(&self) -> &log_helper::AdvancedLogging { + return &self.al; + } + fn is_verbose(&self) -> bool { return self.verbose; } @@ -150,18 +159,30 @@ #[cfg(windows)] pub struct Windows { verbose: bool, + al: log_helper::AdvancedLogging, } #[cfg(windows)] impl Windows { - pub fn new(debug: bool) -> Windows { - Windows { verbose: debug } + pub fn new(debug: bool, load_advanced: bool) -> Windows { + pub fn new(debug: bool, load_advanced: bool) -> Windows { + if ! load_advanced { + Windows { verbose: debug, al: log_helper::AdvancedLogging::default() } + } else { + Windows { verbose: debug, al: log_helper::AdvancedLogging::load(&Windows::new(debug, false)) } + } + } } } #[cfg(windows)] impl Os for Windows { + + fn advanced_logging(&self) -> &log_helper::AdvancedLogging { + return &self.al; + } + fn log(&self, s: &str) { log_helper::log_impl(2,self, s); } diff -r 65de3b40a457 -r 7ef277699fd1 rust-launcher/src/property_from_files_resolver.rs --- a/rust-launcher/src/property_from_files_resolver.rs Sat Feb 16 20:56:33 2019 +0100 +++ b/rust-launcher/src/property_from_files_resolver.rs Sun Feb 17 10:35:53 2019 +0100 @@ -1,6 +1,9 @@ use property_from_file; use os_access; use dirs_paths_helper; +use::log_helper; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; use std; use std::string::String; @@ -41,7 +44,7 @@ property_from_file::str_to_bool(&val) } None => { - dirs_paths_helper::AdvancedLogging::default().log_to_file + log_helper::AdvancedLogging::default().log_to_file } } } @@ -53,7 +56,7 @@ property_from_file::str_to_bool(&val) } None => { - dirs_paths_helper::AdvancedLogging::default().log_to_stdstreams + log_helper::AdvancedLogging::default().log_to_stdstreams } } } @@ -65,22 +68,36 @@ property_from_file::str_to_bool(&val) } None => { - dirs_paths_helper::AdvancedLogging::default().log_to_system + log_helper::AdvancedLogging::default().log_to_system } } } -pub fn try_custom_logdir_from_properties(logger: &os_access::Os) -> std::path::PathBuf { +//this method is not in log_helpers because of io::write and fmt::write issue +pub fn logfile_name() -> String { + let start = SystemTime::now(); + let t = start.duration_since(UNIX_EPOCH).expect("time should be measureable"); + let m = t.as_secs(); + //itw-javantx-2019-02-16_20:56:08.882.log + let mut future_name = String::new(); + write!(&mut future_name, "itw-nativerustlauncher-{}.log", m).expect("unwrap failed"); + future_name +} + +pub fn try_logtarget_from_properties(logger: &os_access::Os) -> std::path::PathBuf { let str_candidate = try_key_from_properties_files(logger, &get_basic_array(logger), property_from_file::KEY_USER_LOG_DIR, &property_from_file::NotMandatoryPathValidator {}); match str_candidate { Some(val) => { - std::path::PathBuf::from(val) + let mut futureFile=std::path::PathBuf::from(val); + futureFile.push(logfile_name()); + futureFile } None => { let mut cfgdir_candidate = logger.get_user_config_dir(); match cfgdir_candidate { Some(mut cfgdir) => { - cfgdir.push("/log"); + cfgdir.push("log"); + cfgdir.push(logfile_name()); cfgdir } None => { diff -r 65de3b40a457 -r 7ef277699fd1 rust-launcher/src/utils.rs --- a/rust-launcher/src/utils.rs Sat Feb 16 20:56:33 2019 +0100 +++ b/rust-launcher/src/utils.rs Sun Feb 17 10:35:53 2019 +0100 @@ -139,6 +139,7 @@ use dirs_paths_helper; use hardcoded_paths; use std::ffi::OsString as fo; + use log_helper; #[test] fn try_none_jre_from_path() { @@ -217,6 +218,10 @@ impl os_access::Os for TestLogger { + fn advanced_logging(&self) -> &log_helper::AdvancedLogging { + panic!("not implemented"); + } + fn is_verbose(&self) -> bool { return true; }