It's a combination of using isatty()
as suggested by genotrance and the code that you found :)
# stdout_to_file.nim import terminal, strformat, timesif isatty(stdout): # ./stdout_to_file echo "This is output to the terminal."else: # ./stdout_to_file | cat const logFileName = "log.txt" let # https://github.com/jasonrbriggs/nimwhistle/blob/183c19556d6f11013959d17dfafd43486e1109e5/tests/cgitests.nim#L15 logFile = open(logFileName, fmWrite) stdout = logFile echo fmt"This is output to the {logFileName} file." echo fmt"- Run using nim {NimVersion} on {now()}."
Save above file as stdout_to_file.nim
.
On running:
nim c stdout_to_file.nim && ./stdout_to_file | cat
I get this in the created log.txt
:
This is output to the log.txt file.- Run using nim 0.19.9 on 2019-01-23T22:42:27-05:00.