Skip to content

Commit

Permalink
0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
skyline69 committed May 11, 2023
1 parent 3403737 commit c2564d5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
42 changes: 35 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "haxrs"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
authors = ["skyline <[email protected]>"]
license-file = "LICENSE"
Expand All @@ -18,6 +18,7 @@ chrono = "0.4.24"
clearscreen = { version = "2.0.1", features = ["windows-console"] }
colored = "2.0.0"
crossterm = "0.26.1"
dirs = "5.0.1"
log = "0.4.17"
log4rs = "1.2.0"
ping = "0.4.1"
Expand Down
51 changes: 29 additions & 22 deletions src/behind/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,42 @@ use log4rs::config::{Appender, Root};
use log4rs::encode::pattern::PatternEncoder;
use log::LevelFilter;
use crate::behind::cli::error_msg;

use dirs::data_dir;

pub(crate) fn log_init() {
let now = Local::now();
let date = now.format("%Y-%m-%d").to_string();


// check if os is linux and create logs folder in home directory
let filename: String = if env::consts::OS != "windows" || env::consts::OS == "linux" {
if let Err(e) = std::fs::create_dir_all(format!("{}/.haxrs/logs", get_home())) {
error_msg(&format!("Failed to create log directory: {}", e));
std::process::exit(1);
}
format!("{}/.haxrs/logs/execution-{}.log", get_home(), date)
let log_dir_path: String = if env::consts::OS != "windows" || env::consts::OS == "linux" {
format!("{}/.haxrs/logs", get_home())
} else {
if let Err(e) = std::fs::create_dir_all("logs") {
error_msg(&format!("Failed to create log directory: {}", e));
std::process::exit(1);
match data_dir() {
Some(mut log_dir) => {
log_dir.push("HaxRS/logs");
match log_dir.to_str() {
Some(s) => s.to_owned(),
None => {
error_msg("Failed to convert log directory to string");
std::process::exit(1);
}
}
},
None => {
error_msg("Failed to get AppData directory");
std::process::exit(1);
}
}
format!("logs/execution-{}.log", date)
};

if let Err(e) = std::fs::create_dir_all(&log_dir_path) {
error_msg(&format!("Failed to create log directory: {}", e));
std::process::exit(1);
}

let filename = format!("{}/execution-{}.log", log_dir_path, date);

let logfile: FileAppender = {
match FileAppender::builder().encoder(Box::new(PatternEncoder::new("{d} {l} - {m}\n"))).build(filename) {
match FileAppender::builder().encoder(Box::new(PatternEncoder::new("{d} {l} - {m}\n"))).build(filename.clone()) {
Ok(file_appender) => file_appender,
Err(e) => {
error_msg(&format!("Failed to create log file: {}", e));
Expand All @@ -49,16 +61,11 @@ pub(crate) fn log_init() {
}
};
// clear the log file on startup
// check if os is linux and create logs folder in home directory
if env::consts::OS != "windows" || env::consts::OS == "linux" {
if let Err(e) = std::fs::write(format!("{}/.haxrs/logs/execution-{}.log", get_home(), date), "") {
error_msg(&format!("Failed to clear log file: {}", e));
std::process::exit(1);
}
} else if let Err(e) = std::fs::write(format!("logs/execution-{}.log", date), "") {
if let Err(e) = std::fs::write(&filename, "") {
error_msg(&format!("Failed to clear log file: {}", e));
std::process::exit(1);
}

match log4rs::init_config(config) {
Ok(_) => {}
Err(e) => {
Expand All @@ -76,4 +83,4 @@ fn get_home() -> String {
std::process::exit(1);
}
}
}
}

0 comments on commit c2564d5

Please sign in to comment.