forked from BynBang/OpenPrestige
34 lines
1.1 KiB
Rust
34 lines
1.1 KiB
Rust
use std::ffi::c_void;
|
|
use std::fs::OpenOptions;
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
use std::sync::OnceLock;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
extern "system" {
|
|
fn GetModuleFileNameW(module: *mut c_void, buf: *mut u16, size: u32) -> u32;
|
|
}
|
|
|
|
fn log_path() -> &'static PathBuf {
|
|
static PATH: OnceLock<PathBuf> = OnceLock::new();
|
|
PATH.get_or_init(|| {
|
|
let mut buf = [0u16; 1024];
|
|
let len = unsafe { GetModuleFileNameW(crate::MODULE, buf.as_mut_ptr(), buf.len() as u32) } as usize;
|
|
let dll = PathBuf::from(String::from_utf16_lossy(&buf[..len]));
|
|
dll.with_file_name("prestige-native.log")
|
|
})
|
|
}
|
|
|
|
/// Appends a line to prestige-native.log beside the DLL.
|
|
pub fn log(message: &str) {
|
|
let secs = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);
|
|
if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_path()) {
|
|
let _ = writeln!(file, "[{secs}] {message}");
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! log {
|
|
($($arg:tt)*) => { $crate::log::log(&format!($($arg)*)) };
|
|
}
|