Reputation: 1363
I'm using Fortran for my research and sometimes, for debugging purposes, someone will insert in the code something like this:
write(*,*) 'Variable x:', varx
The problem is that sometimes it happens that we forget to remove that statement from the code and it becomes difficult to find where it is being printed. I usually can get a good idea where it is by the name 'Variable x' but it sometimes happens that that information might no be present and I just see random numbers showing up.
One can imagine that doing a grep for write(*,*) is basically useless so I was wondering if there is an efficient way of finding my culprit, like forcing every call of write(*,*)
to print a file and line number, or tracking stdout.
Thank you.
Upvotes: 2
Views: 398
Reputation: 32366
As has been previously implied, there's no Fortran--although there may be a compiler approach---way to change the behaviour of the write
statement as you want. However, as your problem is more to do with handling (unintentionally produced) bad code there are options.
If you can't easily find an unwanted write(*,*)
in your code that suggests that you have many legitimate such statements. One solution is to reduce the count:
*
as the format);*
as the output unit, use output_unit
from the intrinsic module iso_fortran_env
.[Having an explicit format for "proper" output is a good idea, anyway.]
If that fails, use your version control system to compare an old "good" version against the new "bad" version. Perhaps even have your version control system flag/block commits with new write(*,*)
s.
And if all that still doesn't help, then the pre-processor macros previously mentioned could be a final resort.
Upvotes: 0
Reputation: 78316
Intel's Fortran preprocessor defines a number of macros, such as __file__
and __line__
which will be replaced by, respectively, the file name (as a string) and line number (as an integer) when the pre-processor runs. For more details consult the documentation.
GFortran offers similar facilities, consult the documentation.
Perhaps your compiler offers similar capabilities.
Upvotes: 4