Skip to content

Commit 43fc754

Browse files
authored
Merge pull request #5 from iCog-Labs-Dev/feature/logger
Output Logger Implementation
2 parents 565a7a4 + 0f0cf24 commit 43fc754

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

metta-run/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
chrono = "0.4.38"
910
clap = { version = "4.5.13", features = ["derive"] }

metta-run/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use formatters::commands::{format, FormatterCommands};
33
use std::io;
44
mod formatters;
55
mod runners;
6+
mod tools;
67

78
fn main() -> io::Result<()> {
89
#[derive(Parser)]
@@ -22,7 +23,10 @@ fn main() -> io::Result<()> {
2223

2324
let args = Args::parse();
2425
let file = args.file;
26+
27+
let start_time = tools::logger::start_timer();
2528
let metta_output = runners::metta::run(file);
29+
tools::logger::stop_timer(start_time, &metta_output)?;
2630

2731
if let Some(command) = args.commands {
2832
match command {
@@ -31,6 +35,5 @@ fn main() -> io::Result<()> {
3135
} else {
3236
println!("{}", metta_output);
3337
}
34-
3538
Ok(())
3639
}

metta-run/src/tools/logger.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use chrono::Local;
2+
use std::fs::OpenOptions;
3+
use std::io::Write;
4+
use std::time::{Duration, Instant};
5+
6+
pub fn start_timer() -> Instant {
7+
Instant::now()
8+
}
9+
10+
pub fn stop_timer(start_time: Instant, metta_output: &String) -> Result<(), std::io::Error> {
11+
let now = Local::now();
12+
let formatted_date = now.format("%Y-%m-%d").to_string();
13+
14+
let log_file_name = format!("{}.log", formatted_date);
15+
16+
let end_time = Instant::now();
17+
let elapsed_time = end_time.duration_since(start_time);
18+
let final_output = format!(
19+
"\nStart time: {}\nElapsed time: {:.3}\n{}\n",
20+
now.format("%Y-%m-%d %H-%m-%s").to_string(),
21+
elapsed_time.as_secs_f32(),
22+
metta_output
23+
);
24+
25+
let mut output_file = OpenOptions::new()
26+
.append(true)
27+
.create(true)
28+
.open(&log_file_name)?;
29+
30+
output_file.write_all(final_output.as_bytes())?;
31+
32+
Ok(())
33+
}

metta-run/src/tools/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod logger;

0 commit comments

Comments
 (0)