对于一个较旧的 Fortran 项目,我尝试创建一个现代记录器。它是面向对象编写的,与 python 的日志库非常相似。
最后一件困扰我的是格式化,我会解释一下。
我的记录器看起来与 this library 非常相似但是,它不符合我的要求,所以我自己写了。基本上我的日志库看起来像这样:
lg = Logger("super logger")
... other settings ...
lg%log("DEBUG", "A cool message to log", rank)
但是使用这个记录器,我失去了写入语句的所有格式化功能。基本上在代码中字符串格式化是通过写入语句完成的,例如:
write (stdout, '(7X,A,f12.6)') "The great number is ", number
所以现在我必须为每个输出语句和辅助变量编写两行代码:
write (message, '(A,f12.6)') "The great number is ", number
lg%log("DEBUG", message, rank)
这不是很方便也不是很好。
我试图执行格式化功能,但我猜它是 not possible具有可变数量的参数(如 C 语言),例如:
function fm(fmt, ...) result(message)
我相信我也无法执行 C 解决方法,因为当我指定绑定(bind)接口(interface)时,我必须指定参数。
我现在唯一的解决方案(除了两行示例)是为不同的变量重载一些方法并创建类似于 c++ 中的流的东西。
我的问题是你有更好的主意吗?理想情况下,如果您知道如何在一行中格式化字符串的选项。
最佳答案
您可以将字符串转换包装在函数中,例如
module io
interface str
module procedure str_real
end interface
contains
function str_real(input,format) result(output)
real(dp), intent(in) :: input
character(*), intent(in) :: format
character(:), allocatable :: output
! It's probably possible to do the allocation and re-allocation more cleanly.
allocate(output(100))
write(output, format) input
output = trim(output)
end function
end module
然后您可以将其与记录器一起使用
lg%log("DEBUG", "The great number is "//str(number,'(f12.6)'), rank)
对于要执行此操作的每种类型,您需要单独的 str
函数。
关于fortran - 一种简单的方法,如何在 Fortran 中格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68530011/