Crate reopen[][src]

Expand description

A tiny Read/Write wrapper that can reopen the underlying IO object.

The main motivation is integration of logging with logrotate. Usually, when logrotate wants to rotate log files, it moves the current log file to a new place and creates a new empty file. However, for the new messages to appear in the new file, a running program needs to close and reopen the file. This is most often signalled by SIGHUP.

Traits

The amount of supported traits is somewhat limited. For example, BufRead or Seek are not implemented, because the behavior across reopens would be confusing if not outright wrong.

Features

The signals feature adds support to registering a reopening as a result of received a signal (for example the SIGHUP one).

Examples

This allows reopening the IO object used inside the logging drain at runtime.

use std::fs::{File, OpenOptions};
use std::io::Error;

use log::info;
use reopen::Reopen;

fn open() -> Result<File, Error> {
    OpenOptions::new()
        .create(true)
        .write(true)
        .append(true)
        .open("/log/file")
}

fn main() -> Result<(), Error> {
    let file = Reopen::new(Box::new(&open))?;
    file.handle().register_signal(signal_hook::consts::SIGHUP)?;
    simple_logging::log_to(file, log::LevelFilter::Debug);
    info!("Hey, it's logging");
    Ok(())
}

Note that this solution is a bit hacky and probably solves only the most common use case.

If you find another use case for it, I’d like to hear about it.

Structs

A handle to signal a companion Reopen object to do a reopen on its next operation.

A Read/Write proxy that can reopen the underlying object.