All other things being equal, I would think fixing your noisy module would be the better long term solution. How hard would it be to write a patch that replaces all occurances of "print*," with "write(logunit,*)", and "write(*" with "write(logunit", where logunit is some integer variable that you can control from your main program (perhaps through a module init subroutine)?
]]>...
open(unit=stdout, file=nullfile, status="old")
call foo()
open(unit=stdout, file=terminaloutput, status="old")
...
How could that be done, i.e. what is 'terminaloutput'?
EDIT: I've got a few steps nearer to the solution:
...
print*, "hello"
open(unit=stdout, file=nullfile, status="old") ! nullfile="/dev/null"
call foo()
open(unit=stdout, file=terminal, status="old") ! terminal="/dev/stdout"
print*, "bye bye"
...
This works but gives me a runtime error:
$ ./supress_output
hello
bye bye
At line 24 of file supress_output.f90 (unit = 6, file = '/dev/stdout')
Fortran runtime error: Invalid argument
program test
! A simple program to demonstrate suppression of STDOUT.
! This program has been tested with gfortran (GCC 4.7.1)
! On most systems STDOUT is associated with unit 6; however, this association is platform dependent.
! This also assumes the existence of /dev/null (should be available on most Linux systems).
! Let's define a STDOUT parameter so this is easy to change later...
integer, parameter :: stdout=6
! Likewise, let's also define null file parameter...
character(len=*), parameter :: nullfile="/dev/null"
! This will cause suppression of STDOUT instead of printing to terminal.
! Comment this out to enable output to terminal.
open(unit=stdout, file=nullfile, status="old")
! Let's call a subroutine that has obnoxious print statements...
call foo()
contains
subroutine foo()
! A subroutine containing a hard coded print statement
print*, "An obnoxious message."
end subroutine foo
end program test
Note that tricks like this can end up biting you, or your teammate, down the road. Be sure to clearly document this and put it someplace obvious.
]]>program test
! A simple program to demonstrate redirection of STDOUT to file.
! This program has been tested with gfortran (GCC 4.7.1)
! On most systems STDOUT is associated with unit 6; however, this association is platform dependent.
! Let's define a STDOUT parameter so this is easy to change latter...
integer, parameter :: stdout=6
! This will redirect STDOUT to file instead of printing to terminal.
! Comment this out to enable output to terminal.
open(unit=stdout, file="stdout.log", form="formatted")
! Let's call a subroutine that has obnoxious print statements...
call foo()
contains
subroutine foo()
! A subroutine containing a hard coded print statement
print*, "An obnoxious message."
end subroutine foo
end program test
There are a number of ways this code could be improved. For example, you could use preprocessor directives to set the STDOUT file unit instead of hardcoding stdout=6. Also, you could use preprocessor directives to enable/disable the open statement. But, hopefully, you get the idea.
Note that this will redirect the write and print statements to STDOUT of the entire program. If you wanted to only redirect the output of a specific module, then you may need to seek a more innovative solution, or simply fix the offensive module.
EDIT: If you want to simply suppress the output, you could replace "stdout.log" with "/dev/null". (I'm slapping myself for not thinking of this first.) Also, you could use unformatted output to speed up the print/write operations (since you're going to discard the output anyway).
]]>It's (in this case) not a big problem because the output is actually quite useful, but I'm implementing all my terminal output with a "verbose"-switch, so it would be nice to include these external subroutines.
]]>