#[cfg(feature = "signals")]
#[rustfmt::skip]
mod example {
use std::fs::File;
use std::io::{Error, Write};
use std::path::Path;
use std::thread;
use std::time::Duration;
use reopen::Reopen;
#[cfg(windows)] use signal_hook::consts::SIGINT as SIGHUP;
#[cfg(not(windows))]
use signal_hook::consts::SIGHUP;
fn log_forever<W: Write>(mut w: W) -> Result<(), Error> {
let mut no = 1u128;
loop {
thread::sleep(Duration::from_secs(1));
writeln!(w, "Tick no {}", no)?;
no += 1;
}
}
fn open_log<P: AsRef<Path>>(p: P) -> Result<File, Error> {
File::create(p)
}
pub fn main() -> Result<(), Error> {
let log = Reopen::new(Box::new(|| open_log("log.txt")))?;
log.handle().register_signal(SIGHUP)?;
log_forever(log)
}
}
#[cfg(not(feature = "signals"))]
#[rustfmt::skip]
mod example {
pub fn main() -> Result<(), std::io::Error> {
Ok(())
}
}
fn main() -> Result<(), std::io::Error> {
example::main()
}