From 29ab398f73c791f9591674c813c47267c524e6be Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Thu, 8 Apr 2021 15:54:53 -0400 Subject: Added concept of logging level. Log file now opened and closed on every write to avoid possible locking issues. Build is broken because of log init call. --- captain/log.f90 | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/captain/log.f90 b/captain/log.f90 index 3757faf..44ba46e 100644 --- a/captain/log.f90 +++ b/captain/log.f90 @@ -1,33 +1,60 @@ module logging implicit none + + integer, parameter::logunit = 1197 + + integer::level_threshold - integer::logunit + character(len=:), pointer::logfile + !integer::logunit contains - subroutine initialize(filename) + subroutine initialize(filename, logging_threshold) implicit none character(*), intent(in)::filename - open(newunit=logunit, file=trim(filename), action="write", status="unknown", position="append") + integer, intent(in)::logging_threshold + + allocate(character(len=len_trim(filename)) :: logfile) + logfile = filename + level_threshold = logging_threshold end subroutine initialize subroutine shutdown() implicit none - close(logunit) + deallocate(logfile) + logfile => null() end subroutine shutdown - subroutine write_log(string) + subroutine write_log(string, level) implicit none character(*), intent(in)::string + integer, intent(in), optional::level + + integer::ierr + + if(present(level)) then + if(level > level_threshold) then + return + end if + end if + + open(unit=logunit, file=logfile, action="write", & + status="unknown", position="append", iostat=ierr) + + ! Silently move on if we failed to open the file + if(ierr == 0) then - ! GNU Extension... :( - write(logunit, *) fdate()//" :: "//string - call flush(logunit) + ! GNU Extension... :( + write(logunit, *) fdate()//" :: "//string + call close(logunit) + + end if end subroutine write_log -- cgit v1.2.3