Quantcast
Channel: How to redirect output to file if no console detected in Nim - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Kaushal Modi for How to redirect output to file if no console detected in Nim

$
0
0

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.

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>