auto renew log after 30days
This commit is contained in:
parent
4e558d8c6b
commit
b316b1dd29
|
|
@ -5,7 +5,12 @@ use fltk::dialog;
|
|||
use fltk_theme::ThemeType;
|
||||
use lazy_static::lazy_static;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, fs::File, path::PathBuf};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
path::PathBuf,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
static ref CONFIG_DIR: PathBuf = {
|
||||
|
|
@ -187,14 +192,38 @@ pub(crate) fn config() -> Args {
|
|||
args
|
||||
}
|
||||
|
||||
pub(crate) fn is_file_30_days_old(file: &File) -> bool {
|
||||
if let Ok(meta) = file.metadata() {
|
||||
if let Ok(time) = meta.created() {
|
||||
if let Ok(dur) = SystemTime::now().duration_since(time) {
|
||||
if dur > Duration::from_secs(60 * 60 * 24 * 30) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) fn log_file() -> File {
|
||||
match File::open(&*LOG_PATH) {
|
||||
Ok(f) => f,
|
||||
Ok(mut file) => {
|
||||
if is_file_30_days_old(&file) {
|
||||
match File::create(&*LOG_PATH) {
|
||||
Ok(f) => file = f,
|
||||
Err(e) => {
|
||||
dialog::alert_default("Can't open log file!");
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
file
|
||||
}
|
||||
Err(_) => match File::create(&*LOG_PATH) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
dialog::alert_default("Can't open log file!");
|
||||
panic!("{:?}", e)
|
||||
panic!("{:?}", e);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue