0

I'm trying to simplify the output of text of console programs. Currently the programs are run in two different ways:

  1. Directly from CMD or batch script
  2. Remotely or blindly, called by another program

Thus, all information output to the user needs to go to both the console and to a default error file.

Currently, I'm just repeating myself:

write(*,*) "That wasn't a number!"
write(199,*) "That wasn't a number!"

The best I've come up with is using a subroutine:

subroutine werr(string)

  implicit none

  character*(*), intent(in) :: string

  write(*,*) string
  write(199,*) string

  return

end subroutine

Better, but still requires call werr("Some text or another.")

Is there any way to emulate the way write works, whereby I can simply have werr("Some text or another.")?

RockDoctor
  • 331
  • 3
  • 11
  • 1
    Is really the problem the `call` keyword? In other languages it is not necessary for subroutines/functions but due to the specifics of Fortran syntax it is required. You could make a macro `#define WERR(a) call werr(a)` but it is hardly any better and it will be limited to the file where it is defined. – Vladimir F Героям слава Nov 13 '19 at 08:45
  • If macros are an option, `#define werr(x) print *, x; write(199,*) x` could be an alternative way (where 199 may be an argument of macro also, plus the file may be redirected to /dev/null if unnecessary). – roygvib Nov 13 '19 at 10:20
  • I'm not sure you need to print to *both* a file and the console. As I understand your question, you need to print to either one or the other, depending on the context. That is usually done either with a redirection, either with an option passed to the program. You then print to only one unit, which is either the console or a file. –  Nov 13 '19 at 10:34
  • If it either/or and you don't want to force redirection on the user you might look at using OUTPUT_UNIT in iso_fortran_env as the unit number - have a variable as the unit and set it to this or 199 as appropriate – Ian Bush Nov 13 '19 at 10:49
  • Why not just print to standard output and pipe the output through 'tee' (which seems to be available on windows these days). https://en.wikipedia.org/wiki/Tee_(command) – janneb Nov 13 '19 at 14:05

0 Answers0