Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix std/random bug (#204) #211

Merged
merged 5 commits into from
Aug 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions src/nimib/capture.nim
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
## This submodule implements only one template to temporarily redirect and capture stdout during execution of a chunk of code.
import tempfile

# see https://stackoverflow.com/questions/64026829/how-to-temporarily-capture-stdout
# Thanks, Clonk!

# low level, should be Posix only but they happen to work on (my) Windows, too!
proc dup(oldfd: FileHandle): FileHandle {.importc, header: "unistd.h".}
proc dup2(oldfd: FileHandle, newfd: FileHandle): cint {.importc, header: "unistd.h".}
import fusion/ioutils
import std/os
import std/tempfiles

template captureStdout*(ident: untyped, body: untyped) =
## redirect stdout to a temporary file and captures output of body in ident
let
# Duplicate stdout
stdoutFileno = stdout.getFileHandle()
stdoutDupfd = dup(stdoutFileno)
# Create a new temporary file
(tmpFile, tmpFilename) = mkstemp(mode=fmWrite)
tmpFileFd: FileHandle = tmpFile.getFileHandle()
discard dup2(tmpFileFd, stdoutFileno) # writing to stdoutFileno now writes to tmpFile
captureStdout(ident, "", body)
sent44 marked this conversation as resolved.
Show resolved Hide resolved

body
flushFile stdout

# before reading tmpFile, flush and close
tmpFile.flushFile()
tmpFile.close()
ident = readFile(tmpFileName)
# Restore stdout
discard dup2(stdoutDupfd, stdoutFileno)
template captureStdout*(ident: untyped, filename: string, body: untyped) =
## redirect stdout to a temporary file and captures output of body in ident
# Duplicate stdout
let stdoutFileno: FileHandle = stdout.getFileHandle()
let stdoutDupFd: FileHandle = stdoutFileno.duplicate()

# Create a new temporary file or attemp to open it
let (tmpFile, tmpFileName {.used.}) = when filename == "":
sent44 marked this conversation as resolved.
Show resolved Hide resolved
createTempFile("tmp", "")
else:
let tmpName = getTempDir() / filename
(tmpName.open(fmAppend), tmpName)
let tmpFileFd: FileHandle = tmpFile.getFileHandle()

# writing to stdoutFileno now writes to tmpFile
tmpFileFd.duplicateTo(stdoutFileno)

# Execute body code
body

# Flush stdout and tmpFile, read tmpFile from start to ident and then close tmpFile
stdout.flushFile()
tmpFile.flushFile()
when filename == "":
sent44 marked this conversation as resolved.
Show resolved Hide resolved
tmpFile.setFilePos(0)
else:
discard tmpFile.reopen(tmpFileName)
ident = tmpFile.readAll()
tmpFile.close()

# Restore stdout
stdoutDupFd.duplicateTo(stdoutFileno)
Loading