fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.05k stars 164 forks source link

[Feedback] The inconsistency of `stdlib` internal modules about `access` keyword of opening files: stdlib_io/open, stdlib_logger #465

Open zoziha opened 3 years ago

zoziha commented 3 years ago

Describe the issue

I tried to use the stdlib_logger module, and used the open function in stdlib_io, and found that there was an adaptability problem. The root cause was that stdlib_logger required to open the log file as access=sequential, while the stdlib_io:open function defaulted to open the file as access=stream , I was wondering if this is a problem and how to solve this problem?

program test_io_logger

    use stdlib_logger, only: global_logger
    use stdlib_io, only: open
    implicit none
    integer :: log_unit

    log_unit = open("log.txt","w+")
    call global_logger%add_log_unit(log_unit)
        !! ERROR STOP unit in stdlib_logger % set_log_unit is not "sequential".

end program test_io_logger

Links

zoziha commented 3 years ago

Maybe I think open as a more basic function, can you consider leaving a loose possibility for access in open function? access can be included by the mode argument, and access=stream can be used by default. https://github.com/fortran-lang/stdlib/blob/d0f13b08e4778961cb6d9c93886a65764bc5762d/src/stdlib_io.fypp#L211

ivan-pi commented 3 years ago

Thanks @zoziha for bringing this inconsistency up.

I believe that @jvdp1 as author of the stdlib_io and reviewer of the logger module is the best person to comment on this issue.

jvdp1 commented 3 years ago

Thank you for reporting this incompatibilit. Please see the PR #71 and its code that introduced open() in stdlib. With this code, the access was sequential. In #71 there was already a lot of discussions about this. The specs of open() specify that all files are streamed files.

I don't recall when the change of access was implemented. However, using mode argument is a good idea IMO to deal with that. Do you have any suggestions?

Another possibiliy would change the logger to accept streamed files, if possible (@wclodius2).

zoziha commented 3 years ago
select case (mode_(4:4))
    case('u'); access_ = 'sequential'
    case('d'); access_ = 'direct'
    case('s'); access_ = 'stream'
end select

I tried to parse the access into mode of the open function, and the default mode is now rts. I submitted my branch here. I refer to the open function of python: open image Among them, python abandoned u (universal) mode, which is sequential access in fortran. But I think three types of access (sequential, direct, stream) are supported in the fortran syntax, so keeping stream as the default and adding support for sequential and direct is currently acceptable.