aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/utils
diff options
context:
space:
mode:
authordenis <denismaximov98@gmail.com>2021-01-24 18:40:14 +0200
committerdenis <denismaximov98@gmail.com>2021-01-24 18:40:14 +0200
commit454de6c52cf58466c4ea41a21435835bc69faa6c (patch)
tree2f8f78f041a05b9bf2bea8c6247377dbb5aedb02 /src/common/utils
parentwip: restructing in progress, to prep for server-client (diff)
downloadsl0ppij-454de6c52cf58466c4ea41a21435835bc69faa6c.tar.gz
sl0ppij-454de6c52cf58466c4ea41a21435835bc69faa6c.tar.xz
sl0ppij-454de6c52cf58466c4ea41a21435835bc69faa6c.zip
wip: oops shouldn't forget that
Diffstat (limited to 'src/common/utils')
-rw-r--r--src/common/utils/consts.rs6
-rw-r--r--src/common/utils/logging.rs76
-rw-r--r--src/common/utils/mod.rs3
-rw-r--r--src/common/utils/shared.rs19
4 files changed, 104 insertions, 0 deletions
diff --git a/src/common/utils/consts.rs b/src/common/utils/consts.rs
new file mode 100644
index 00000000..7a0c9ddf
--- /dev/null
+++ b/src/common/utils/consts.rs
@@ -0,0 +1,6 @@
+pub const MOSAIC_TMP_DIR: &str = "/tmp/mosaic";
+pub const MOSAIC_TMP_LOG_DIR: &str = "/tmp/mosaic/mosaic-log";
+pub const MOSAIC_TMP_LOG_FILE: &str = "/tmp/mosaic/mosaic-log/log.txt";
+pub const MOSAIC_IPC_PIPE: &str = "/tmp/mosaic/ipc";
+pub const MOSAIC_ROOT_PLUGIN_DIR: &str = "/usr/share/mosaic/plugins";
+pub const MOSAIC_ROOT_LAYOUT_DIR: &str = "/usr/share/mosaic/layouts";
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])
+}
diff --git a/src/common/utils/mod.rs b/src/common/utils/mod.rs
new file mode 100644
index 00000000..0ea4c76f
--- /dev/null
+++ b/src/common/utils/mod.rs
@@ -0,0 +1,3 @@
+pub mod consts;
+pub mod logging;
+pub mod shared;
diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs
new file mode 100644
index 00000000..1d8ca5c3
--- /dev/null
+++ b/src/common/utils/shared.rs
@@ -0,0 +1,19 @@
+use std::{iter, str::from_utf8};
+
+use strip_ansi_escapes::strip;
+
+pub fn ansi_len(s: &str) -> usize {
+ from_utf8(&strip(s.as_bytes()).unwrap())
+ .unwrap()
+ .chars()
+ .count()
+}
+
+pub fn pad_to_size(s: &str, rows: usize, columns: usize) -> String {
+ s.lines()
+ .map(|l| [l, &str::repeat(" ", columns - ansi_len(l))].concat())
+ .chain(iter::repeat(str::repeat(" ", columns)))
+ .take(rows)
+ .collect::<Vec<_>>()
+ .join("\n\r")
+}