use e_utils::{ fs::{FileCustom, FileExt, FileOptionsExt}, parse::AutoPath as _, system::time::{TimezoneStrategy, LOG_FILE_DATE_FORMAT}, }; use std::{ fs, io::{self, Write}, path::Path, time::Duration, }; /// 关闭时删除文件; 直接写入; 随机接入 fn main() -> Result<(), io::Error> { let now = TimezoneStrategy::UseUtc .get_now() .format(LOG_FILE_DATE_FORMAT); let fpath = Path::new("logs").join(format!("test.{}.log", now)); fpath.auto_create_dir().unwrap(); let mut f = fs::OpenOptions::new() .create(true) .write(true) .read(true) .append(true) .custom_flags2([ FileCustom::DeleteOnClose, FileCustom::WriteThrough, FileCustom::RandomAccess, ]) .open(&fpath)?; f.write("locked\n".as_bytes())?; for i in 0..30 { let _ = f .write(format!("{i}.locke write\n").as_bytes()) .inspect_err(|e| eprintln!("{}", e)); if i > 20 { let _ = f.unlock(); } let x = f.allocated_size()?; println!("allocated -> {x}"); std::thread::sleep(Duration::from_millis(500)); } Ok(()) }