Skip to content

Commit

Permalink
Merge branch 'main' of github.com:JakeRoggenbuck/auto-clock-speed int…
Browse files Browse the repository at this point in the history
…o test-logging
  • Loading branch information
JakeRoggenbuck committed Jan 10, 2025
2 parents 940f9bb + 07d9d15 commit 3e3c5f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cached = "0.42.0"
rand = "0.8.5"
globset = "0.4"
time = { version = "0.3", features = ["local-offset", "formatting"]}
chrono = "0.4.38"

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
Expand Down
2 changes: 1 addition & 1 deletion src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Writer for CSVWriter {
let lines = writables.map(|c| c.to_csv()).collect::<String>();

// Open file in append mode
let mut file = match OpenOptions::new().write(true).append(true).open(&self.path) {
let mut file = match OpenOptions::new().append(true).open(&self.path) {
Ok(file) => file,
Err(..) => {
logger.log("Could not open file for writing.", logger::Severity::Error);
Expand Down
6 changes: 5 additions & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,12 @@ impl Checker for Daemon {
fn preprint_render(&mut self) -> String {
let message = format!("{}\n", self.message);
let title = "Name\tMax\tMin\tFreq\tTemp\tUsage\tGovernor\n";

// Render each line of cpu core
let cpus = &self.cpus.iter().map(|c| format!("{c}")).collect::<String>();
let mut cpus = String::new();
for c in &self.cpus {
cpus.push_str(&c.to_string());
}

// Prints battery percent or N/A if not
let battery_status = print_battery_status(&self.battery);
Expand Down
26 changes: 9 additions & 17 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
//! It allows the program to log messages with different severity levels (error, warning, log) and display them in a human-readable format.
//!
//! The log messages contain a timestamp, a severity level and the message. The logs are stored in a vector and are serializable and deserializable. The logs can also be displayed in a human-readable format.
extern crate time;
use chrono::{DateTime, Local};
use efcl::{color, Color};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::time::SystemTime;
use time::{format_description, OffsetDateTime};

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
/// The Severity enum is used to represent the different levels of severity of a log message. It has three possible values:
Expand Down Expand Up @@ -44,22 +43,15 @@ impl fmt::Display for Log {
Severity::Log => color!(Color::BLUE, "notice:"),
};

let date_time_fmt =
format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]")
.expect("Invalid time format.");
let local_time: DateTime<Local> = DateTime::<Local>::from(self.timestamp);

let mut time = OffsetDateTime::from(self.timestamp)
.format(&date_time_fmt)
.unwrap();

if OffsetDateTime::now_local().is_ok() {
time = OffsetDateTime::now_local()
.expect("IndeterminateOffset")
.format(&date_time_fmt)
.unwrap();
}

write!(f, "{} {} -> {}", severity, time, self.message)
write!(
f,
"{} {} -> {}",
severity,
local_time.format("%Y-%m-%d %H:%M:%S"),
self.message
)
}
}

Expand Down

0 comments on commit 3e3c5f6

Please sign in to comment.