ragit_fs/
log.rs

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::{
    exists,
    write_string,
    FileError,
    WriteMode,
};
use chrono::offset::Local;

static mut LOG_FILE_PATH: Option<[u8; 1024]> = None;
static mut LOG_FILE_PATH_LEN: usize = 0;

pub fn set_log_file_path(path: Option<String>) {
    unsafe {
        if let Some(path) = path {
            let mut bytes = [0; 1024];

            for (i, c) in path.as_bytes().iter().enumerate() {
                bytes[i] = *c;
            }

            LOG_FILE_PATH_LEN = path.len();
            LOG_FILE_PATH = Some(bytes);
        }

        else {
            LOG_FILE_PATH = None;
        }
    }
}

fn get_log_file_path() -> Option<String> {
    unsafe {
        LOG_FILE_PATH.map(|bytes| String::from_utf8_lossy(&bytes[..LOG_FILE_PATH_LEN]).to_string())
    }
}

pub fn initialize_log_file(path: &str, remove_existing_file: bool) -> Result<(), FileError> {
    if remove_existing_file {
        if exists(path) {
            // TODO: append to the old file, instead of overwriting it
            if let Err(e) = std::fs::copy(
                path,
                &format!("{path}-backup"),
            ) {
                return Err(FileError::from_std(e, path));  // TODO: which path?
            }
        }

        write_string(path, "", WriteMode::CreateOrTruncate)?;
    }

    Ok(())
}

pub fn write_log(owner: &str, msg: &str) {
    if let Some(path) = get_log_file_path() {
        write_string(
            &path,
            &format!(
                "{} | {} | {msg}\n",
                Local::now().to_rfc2822(),
                if owner.len() < 32 {
                    format!("{}{owner}", " ".repeat(32 - owner.len()))
                } else {
                    owner.to_string()
                },
            ),
            WriteMode::AlwaysAppend,
        ).unwrap();
    }
}