1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::{fs::OpenOptions, io::{self, Write}};

use chrono::{DateTime, Local};

fn formatted_time_entry(time_format: Option<&'static str>) -> String {
    let default_time_format = "%Y-%m-%d %H:%M:%S,%3f";
    let time_format_to_use = time_format.unwrap_or(default_time_format);
    
    let local: DateTime<Local> = Local::now();
    let formatted = format!("[{}]", local.format(time_format_to_use).to_string());
    formatted
}

fn record_entry_in_log(filename: &str, bytes: &[u8]) -> io::Result<()> {
    let mut file = OpenOptions::new().
                        append(true).
                        write(true).
                        create(true).
                        open(filename)?;
    file.write_all(bytes)?;
    Ok(())
}

pub fn log(filename: &'static str, entry: &'static str, time_format: Option<&'static str>) -> io::Result<String> {
    let entry_time = formatted_time_entry(time_format);
    {
    
    let together = format!("{} {}\n", entry_time, entry);
        let entry_bytes = together.as_bytes();
        record_entry_in_log(filename, &entry_bytes)?;
    }
    Ok(entry_time)
}