blob: 207394bc9e9fccaef097bd91d97ef557d32a0fa6 (
plain)
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
|
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));
}
|