zellij/src/utils/logging.rs
Roee Shapira fd1f1ce697
fix(layout): total layout percent 100 validation (#57)
* Fix for issue #52

* Added missing fixtures.

* Added missing validation.

* Moved layout creation and validation to the Layout struct.

* Ran cargo fmt.

* Added creation of tmp folder if needed.

* Code review edit.

* Code review edit.

* Fancied code up.

* PR request change.

* PR code review.

* Merge from upstream/main.

* Merge from upstream/main.
2020-11-21 19:39:57 +01:00

57 lines
1.2 KiB
Rust

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 debug_log_to_file(message: String) -> io::Result<()> {
let mut file = fs::OpenOptions::new()
.append(true)
.create(true)
.open(MOSAIC_TMP_LOG_FILE)?;
file.write_all(message.as_bytes())?;
file.write_all("\n".as_bytes())?;
Ok(())
}
pub fn debug_log_to_file_pid_0(message: String, pid: RawFd) -> io::Result<()> {
if pid == 0 {
debug_log_to_file(message)?;
}
Ok(())
}
pub fn delete_log_file() -> io::Result<()> {
if fs::metadata(MOSAIC_TMP_LOG_FILE).is_ok() {
fs::remove_file(MOSAIC_TMP_LOG_FILE)?;
}
Ok(())
}
pub fn delete_log_dir() -> io::Result<()> {
if fs::metadata(MOSAIC_TMP_LOG_DIR).is_ok() {
fs::remove_dir_all(MOSAIC_TMP_LOG_DIR)?;
}
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])?;
Ok(())
}