#redirecting stdout/stderr (or similar) to file ?

1 messages · Page 1 of 1 (latest)

rustic kiln
#

Hey, I was hoping to write a function which could redirect stdout and stderr to a file (or separate files, as indicated by arguments to said function).

Anyway. Not really sure if there's any bits I can use to accomplish this in a cross-platform way...?

compact geyser
rustic kiln
#

for the current process. I'm embedding lua and writing a minimal test library to test out code I have which will be both in lua (same process) and zig.
And I want to capture that output (stdout, stderr) and selectively display it for failing tests, hence the idea

compact geyser
#

and windows seems to have SetStdHandle

#

but apparently that doesnt actually update the fd, it just makes future calls to GetStdHandle give the new one
so if an app caches the file handle windows gives it for stdout/stderr, it wont update it like it would on linux

#

so that might be an issue

rustic kiln
#

Fun.

Actually, a thing I'm struggling with right now. I am pondering writing a posix and a windows path.

But for the windows path, my struct will need different fields than for the posix path.

How do I express such a struct ?

#

(I am guessing something with comptime and std.builtin.os.tag

compact geyser
#

you can just use it like that as long as posix and windows are comptime known

rustic kiln
#

noreturn ? so what.. the field won't be present at all if we are on a non-posix platform ?

compact geyser
#

note that using noreturn as the type means that it cant be accessed
if you try to access a field thats a noreturn, itll be a compile error
if you just want it to be optimized out, you can make it void or u0 (or any other zero-width-type)

#

yep!

#

it wont exit and it would be an error to access it

rustic kiln
#

interesting. I was hunting around (a little!) in the stdlib for something like this

compact geyser