diff options
| author | namilsk <namilsk@namilsk.tech> | 2026-03-06 22:42:10 +0300 |
|---|---|---|
| committer | namilsk <namilsk@namilsk.tech> | 2026-03-06 22:42:10 +0300 |
| commit | 772093279e1dd162a47e2dfe50d9a9ae0192d750 (patch) | |
| tree | 49b10a7543cb43fe1c024f327bf7a55d791ccd0b /init/src/log.rs | |
| parent | 733fa015a0499139a2dc171503fa48701fcba1d0 (diff) | |
implemented `SIGUSER/SIGINT/SIGTERM` handlers, graceful shutdown, logging to file
Diffstat (limited to 'init/src/log.rs')
| -rw-r--r-- | init/src/log.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/init/src/log.rs b/init/src/log.rs index 4c89dd1..207394b 100644 --- a/init/src/log.rs +++ b/init/src/log.rs @@ -1,11 +1,46 @@ +use std::fs::OpenOptions; +use std::io::Write; +use std::sync::Mutex; + +const LOG_FILE_PATH: &str = "/var/log/vigil.log"; + +static LOG_FILE: Mutex<Option<std::fs::File>> = Mutex::new(None); + +/// Инициализирует файл логирования. Должен вызываться один раз при старте. +pub fn init_logging() -> Result<(), Box<dyn std::error::Error>> { + let file = OpenOptions::new() + .create(true) + .append(true) + .open(LOG_FILE_PATH)?; + + let mut guard = LOG_FILE.lock().map_err(|e| e.to_string())?; + *guard = Some(file); + Ok(()) +} + +fn write_to_log(message: &str) { + let mut guard = match LOG_FILE.lock() { + Ok(g) => g, + Err(poisoned) => poisoned.into_inner(), + }; + + if let Some(ref mut file) = *guard { + let _ = writeln!(file, "{}", message); + let _ = file.flush(); + } +} + pub fn log_critical_error(message: &str) { println!("\x1b[31m * \x1b[0m {}", message); + write_to_log(&format!("[CRITICAL] {}", message)); } pub fn log_warning(message: &str) { println!("\x1b[33m * \x1b[0m {}", message); + write_to_log(&format!("[WARNING] {}", message)); } pub fn log_success(message: &str) { println!("\x1b[32m * \x1b[0m {}", message); + write_to_log(&format!("[OK] {}", message)); } |
