Skip to content

Writing to std::fs::File (or any type implementing std::io::Write trait) #124

@bobmoretti

Description

@bobmoretti

I have a use case where I'd like to write to an existing std::fs::File object.

In particular, I have used the Win32 function AllocConsole() to create a console window for my code, which runs inside of a DLL called from a GUI application. I would like to log to this console. E.g., I have the following function

fn create_console() -> windows::core::Result<File> {
    unsafe {
        Console::AllocConsole();
        let h_stdout = Console::GetStdHandle(Console::STD_OUTPUT_HANDLE)?;
        Ok(File::from_raw_handle(h_stdout.0 as *mut libc::c_void))
    }
}

I want to log to the File object returned by this function.

I see that there is a duplicate_to_stdout() method in flexi_logger, but nothing to duplicate to an arbitrary file. It looks like there's a writers module but that seems to require implementing an entire trait. Is there a straightforward way to tell flexi_logger to log to a given Write sink?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions