summaryrefslogtreecommitdiffstats
path: root/src/log.rs
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-09-26 16:53:13 +0200
committerqkzk <qu3nt1n@gmail.com>2023-09-26 16:53:13 +0200
commit8e52d44cb2fa8947596328aca3425181f1acab83 (patch)
tree6e2ca86beb99098335504a9c261ae22c38858bbb /src/log.rs
parent35c9e0877578d36c5ba8b6da7fa34efe85b40a67 (diff)
remove useless functions in log config
Diffstat (limited to 'src/log.rs')
-rw-r--r--src/log.rs40
1 files changed, 5 insertions, 35 deletions
diff --git a/src/log.rs b/src/log.rs
index 2f84c60..4bfe6f9 100644
--- a/src/log.rs
+++ b/src/log.rs
@@ -1,14 +1,12 @@
use std::sync::RwLock;
use anyhow::Result;
-use chrono::{offset, LocalResult, NaiveDateTime, Utc};
use lazy_static::lazy_static;
use log4rs;
use crate::constant_strings_paths::{ACTION_LOG_PATH, LOG_CONFIG_PATH};
use crate::utils::extract_lines;
-const ONE_DAY_IN_SECONDS: i64 = 24 * 60 * 60;
// static ENV_HOME: &str = "$ENV{HOME}";
/// Set the logs.
@@ -57,56 +55,28 @@ pub fn read_log() -> Result<Vec<String>> {
Ok(extract_lines(content))
}
-/// Parse log line, reads its date and compare it to Utc now.
-/// True, If the date is readable and less than a day ago.
-pub fn is_log_recent_engough(line: &str) -> Result<bool> {
- let strings: Vec<&str> = line.split(" - ").collect();
- if strings.is_empty() {
- Ok(false)
- } else {
- Ok(is_recent_enough(to_naive_datetime(strings[0])?))
- }
-}
-
-/// Parse a string like "2023-09-19 20:44:22" to a `chrono::NaiveDateTime`.
-fn to_naive_datetime(str_date: &str) -> Result<NaiveDateTime> {
- Ok(NaiveDateTime::parse_from_str(
- str_date,
- "%Y-%m-%d %H:%M:%S",
- )?)
-}
-
-/// True iff the datetime can be parsed to Utc and is less than a day ago.
-fn is_recent_enough(dt_naive: NaiveDateTime) -> bool {
- if let LocalResult::Single(dt_utc) = dt_naive.and_local_timezone(offset::Local) {
- let delta = Utc::now().timestamp() - dt_utc.timestamp();
- delta < ONE_DAY_IN_SECONDS
- } else {
- false
- }
-}
-
lazy_static! {
static ref LAST_LOG_LINE: RwLock<String> = RwLock::new("".to_string());
}
/// Read the last value of the "log line".
/// It's a global string created with `lazy_static!(...)`
+/// Fail silently if the global variable can't be read and returns an empty string.
pub fn read_last_log_line() -> String {
- let Ok(r) = LAST_LOG_LINE.read() else {
+ let Ok(last_log_line) = LAST_LOG_LINE.read() else {
return "".to_owned();
};
- r.to_string()
+ last_log_line.to_string()
}
/// Write a new log line to the global variable `LAST_LOG_LINE`.
/// It uses `lazy_static` to manipulate the global variable.
/// Fail silently if the global variable can't be written.
fn write_last_log_line(log: &str) {
- let Ok(mut new_log_string) = LAST_LOG_LINE.write() else {
+ let Ok(mut new_log_line) = LAST_LOG_LINE.write() else {
return;
};
- *new_log_string = log.to_owned();
+ *new_log_line = log.to_owned();
}
/// Write a line to both the global variable `LAST_LOG_LINE` and the special log