How to redirect output to file if no console detected in Nim

redirect stdout and stderr to file (windows)
batch file output to log and console
redirect stderr and stdout to file
windows command line redirect output to file and screen
cmd redirect output to file not working
windows redirect all output to file
dos redirect stdout and stderr to file
error redirecting stdout file

I want my Nim program to write to the console if there is one, and redirect echo to write to a file if there isn't. Is there an equivalent to the Environment.UserInteractive property in .NET which I could use to detect if no console is available and redirect stdout in that case?

You should be able to use isatty().

Here's an example in Nimble.

How to redirect output to file if no console detected in Nim, Is there an equivalent to the Environment.UserInteractive property in .NET which I could use to detect if no console is available and redirect stdout in that case? Whenever output is read but its length is < the buffer length, notify yourself (I do it with an event) that there is output, send the contents of the StringBuilder to the subscriber, and then clear it. However, in my case I was simply writing more stuff to the console's standard output. I 'm not sure what "updating" the output means in your case.

Edit: @tjohnson this is in response to your comment. I don't have enough points to respond to your comment directly or something? Thanks Stack Overflow...

It's hard to say without seeing more of the code.

What version of Nim are you using?

I suspect stdout has been shadowed by a read only symbol.

Are you calling this code inside of a proc and passing stdout as an argument? like this: proc foo(stdout: File)

If so, you will need to change it to a var parameter to make the argument writable: proc test(stdout: var File)

Or use stdout as a global variable instead.

status-im/nim-chronicles: A crafty implementation of , Chronicles will not try to detect if the standard outputs of the program are being redirected to another program or a file. It's typical for the colored output to be  In the above example, we create a new temporary file called log and tell our ProcessBuilder to redirect output to this file destination. In this last snippet, we simply check that getInputStream() is indeed null and that the contents of our file are as expected:

It's a combination of using isatty() as suggested by genotrance and the code that you found :)

# stdout_to_file.nim 
import terminal, strformat, times

if 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.

Redirecting Error Messages from Command Prompt: STDERR , When you redirect console output using the ">" symbol, you are only redirecting STDOUT. In order to redirect EXAMPLE. The command "dir file.xxx" (where file.​xxx does not exist) will display the following output: File Not Found. If you No. Tell us what we can do to improve the article. Submit. Thanks! For output redirection you can use: command > filename Redirect command output to a file (overwrite) command >> filename APPEND into a file command 2> filename Redirect Errors Input redirection works in a different way.

Nim: Check if stdin/stdout are associated with terminal or pipe A , I would also need to know if the script is sending output to the terminal, or to How to detect whether input is from keyboard, a file, or another process. read from standard input, or prompt the # user for input if there is none. command > filename Redirect command output to a file command >> filename APPEND into a file command < filename Type a text file and pass the text to command commandA | commandB Pipe the output from commandA into commandB commandA & commandB Run commandA and then run commandB commandA && commandB Run commandA, if it succeeds then run commandB commandA || commandB Run commandA, if it fails then run commandB commandA && commandB || commandC If commandA succeeds run commandB, if it fails commandC

Input/Output Redirection in the Shell, For example, if you type cat with no arguments, it listens for input on stdin, But we can redirect that output to a file using the > operator:. I want my Nim program to write to the console if there is one, and redirect echo to write to a file if there isn't. Is there an equivalent to the Environment.UserInteractive property in .NET which I could use to detect if no console is available and redirect stdout in that case?

Repository Configuration, Redirect Remote Commands through Pulse: If enabled, remote commands will be Exclude Jobs in the 'none' Pool: Prevents the Slaves from picking up Jobs that have When a stalled Slave, Pulse, Balancer or Connection Server is detected, an Write Repository Repair Output to Separate Log File: If enabled, all output  Normally a .NET console application writes its exception messages directly to the console so that the user can view them. Here's an example: You'll get "Attempted to divide by 0" in the console. However, this is not the only option you have.

Comments
  • Thanks, that works. How do I redirect stdout to a file? I've tried if not isatty(stdout): var logFile = open("output.log", fmWrite); stdout = logFile and expected that to work based on what I saw here. But when I try to compile this, I get an error "lvalue required as left operand of assignment".
  • @tjohnson Your example actually works for me on RHEL 6.8, using Nim built from devel. See my full answer.