diff options
| author | denis <denismaximov98@gmail.com> | 2021-01-24 18:40:14 +0200 |
|---|---|---|
| committer | denis <denismaximov98@gmail.com> | 2021-01-24 18:40:14 +0200 |
| commit | 454de6c52cf58466c4ea41a21435835bc69faa6c (patch) | |
| tree | 2f8f78f041a05b9bf2bea8c6247377dbb5aedb02 /src/common/utils/logging.rs | |
| parent | wip: restructing in progress, to prep for server-client (diff) | |
| download | sl0ppij-454de6c52cf58466c4ea41a21435835bc69faa6c.tar.gz sl0ppij-454de6c52cf58466c4ea41a21435835bc69faa6c.tar.xz sl0ppij-454de6c52cf58466c4ea41a21435835bc69faa6c.zip | |
wip: oops shouldn't forget that
Diffstat (limited to 'src/common/utils/logging.rs')
| -rw-r--r-- | src/common/utils/logging.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/common/utils/logging.rs b/src/common/utils/logging.rs new file mode 100644 index 00000000..05af455c --- /dev/null +++ b/src/common/utils/logging.rs @@ -0,0 +1,76 @@ +use std::{ + fs, + io::{self, prelude::*}, + os::unix::io::RawFd, + path::PathBuf, +}; + +use crate::utils::consts::{MOSAIC_TMP_LOG_DIR, MOSAIC_TMP_LOG_FILE}; + +pub fn atomic_create_file(file_name: &str) { + let _ = fs::OpenOptions::new().create(true).open(file_name); +} + +pub fn atomic_create_dir(dir_name: &str) -> io::Result<()> { + if let Err(e) = fs::create_dir(dir_name) { + if e.kind() == std::io::ErrorKind::AlreadyExists { + Ok(()) + } else { + Err(e) + } + } else { + Ok(()) + } +} + +pub fn debug_log_to_file(mut message: String) -> io::Result<()> { + message.push('\n'); + debug_log_to_file_without_newline(message) +} + +pub fn debug_log_to_file_without_newline(message: String) -> io::Result<()> { + atomic_create_file(MOSAIC_TMP_LOG_FILE); + let mut file = fs::OpenOptions::new() + .append(true) + .create(true) + .open(MOSAIC_TMP_LOG_FILE)?; + file.write_all(message.as_bytes()) +} + +pub fn _debug_log_to_file_pid_3(message: String, pid: RawFd) -> io::Result<()> { + if pid == 3 { + debug_log_to_file(message) + } else { + Ok(()) + } +} + +#[allow(dead_code)] +pub fn delete_log_file() -> io::Result<()> { + if fs::metadata(MOSAIC_TMP_LOG_FILE).is_ok() { + fs::remove_file(MOSAIC_TMP_LOG_FILE) + } else { + Ok(()) + } +} + +#[allow(dead_code)] +pub fn delete_log_dir() -> io::Result<()> { + if fs::metadata(MOSAIC_TMP_LOG_DIR).is_ok() { + fs::remove_dir_all(MOSAIC_TMP_LOG_DIR) + } else { + Ok(()) + } +} + +pub fn debug_to_file(message: u8, pid: RawFd) -> io::Result<()> { + let mut path = PathBuf::new(); + path.push(MOSAIC_TMP_LOG_DIR); + path.push(format!("mosaic-{}.log", pid.to_string())); + + let mut file = fs::OpenOptions::new() + .append(true) + .create(true) + .open(path)?; + file.write_all(&[message]) +} |
