Fortran substitute subroutine name using macro

fortran, macros

Solution

In this case you would have to use the same approach C uses. Define the macro you proposed

 #define log(msg) m_log(msg,__FILE__,__LINE__)

in a separate file (possibly with other useful macros) and include it using `#include "file.inc"` (standard Fortran include won't suffice).

If the macro has a different name, than the subroutine it actually calls, you can ensure that the user has to use the include and cannot forget it.

Problem

I am writing a module that allows users to log information. I want to provide an interface that logs a string message, which can be called as ``` call m_log(msg) ``` So in file m_logger.f90, I will have ``` module m_logger .. subroutine m_log(msg) .. end module ``` In file main.f90, a user will have ``` program main use m_logger call m_log(msg) end program ``` Now how can I substitute `call m_log(msg)` with `call m_log(msg, __FILE__, __LINE__)` ? Because of this substitution, a different subroutine `subroutine m_log(msg, filename, linenum)` in the logger module will be called instead. If I use a macro like `#define m_log(msg) m_log(msg,__FILE__,__LINE__)` , it will have to be added to every user file that uses the logger. Also, I do not want to enforce the user to pass `__FILE__` and `__LINE__` explicitly. Is there a way I can do this? Or are there any other alternatives altogether? Thanks in advance Edit: I had a discussion on comp.lang.fortran. Adding a link for reference. here

Original source