Skip to content

Commit

Permalink
add logging capability for easier debugging (#680)
Browse files Browse the repository at this point in the history
* adding logging capability

* adding logging

* more logging

* more logging

* more logging

* more logging

* more logging

* more documentation

* changed cache key
  • Loading branch information
edwardhartnett authored May 15, 2024
1 parent dcae6b4 commit 8db6654
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/developer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
uses: actions/cache@v4
with:
path: ~/data
key: data-developer-2
key: data-developer-3

- name: asan
if: matrix.config == 'asan/code coverage'
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(fortran_src compack.F90 drstemplates.F90 g2bytes.F90 g2grids.F90
g2get.F90 g2getgb2.F90 g2index.F90 g2gf.F90 g2unpack.F90 g2create.F90
${CMAKE_CURRENT_BINARY_DIR}/gribmod.F90 gridtemplates.F90 intmath.F90
g2jpc.F90 pack_gp.f params_ecmwf.F90 params.F90 pdstemplates.F90
g2png.F90 realloc.F90 reduce.f g2sim.F90 skgb.F90 g2spec.F90)
g2png.F90 realloc.F90 reduce.f g2sim.F90 skgb.F90 g2spec.F90 g2logging.F90)

# This function calls NCEPLIBS-w3emc.
if (BUILD_WITH_W3EMC)
Expand Down
26 changes: 26 additions & 0 deletions src/g2getgb2.F90
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ end subroutine getgb2
!> @author Ed Hartnett, Mark Iredell @date 2024-03-19
subroutine getgb2i2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, &
unpack, idxver, k, gfld, iret)
use g2logging
use grib_mod
implicit none

Expand Down Expand Up @@ -245,6 +246,12 @@ subroutine getgb2r2(lugb, idxver, cindex, gfld, iret)
end subroutine getgb2r2
end interface

#ifdef LOGGING
write(g2_log_msg, '(a, i2, a, i2, a, i5, a, i5, a, l, a, i1)') 'getgb2i2: lugb ', lugb, ' lugi ', lugi, &
' j ', j, ' jdisc ', jdisc, ' unpack ', unpack, ' idxver ', idxver
call g2_log(1)
#endif

! Fill cbuf with the index records of this file, by recalling them
! from memory, reading them from the index file, or generating them
! from the data file.
Expand Down Expand Up @@ -348,6 +355,7 @@ end subroutine getgb2l
!>
!> @author Stephen Gilbert @date 2002-05-07
subroutine getgb2l2(lugb, idxver, cindex, gfld, iret)
use g2logging
use grib_mod
implicit none

Expand Down Expand Up @@ -376,6 +384,11 @@ subroutine gf_unpack2(cgrib, lcgrib, iofst, lencsec2, csec2, ierr)
end subroutine gf_unpack2
end interface

#ifdef LOGGING
write(g2_log_msg, '(a, i2, a, i1)') 'getgb2l2: lugb ', lugb, ' idxver ', idxver
call g2_log(1)
#endif

! Get info.
nullify(gfld%local)
iret = 0
Expand Down Expand Up @@ -775,6 +788,7 @@ end subroutine getgb2r
!>
!> @author Ed Hartnett, Stephen Gilbert @date Feb 14, 2024
subroutine getgb2r2(lugb, idxver, cindex, gfld, iret)
use g2logging
use grib_mod
implicit none

Expand Down Expand Up @@ -813,6 +827,11 @@ subroutine gf_unpack7(cgrib, lcgrib, iofst, igdsnum, igdstmpl, &
end subroutine gf_unpack7
end interface

#ifdef LOGGING
write(g2_log_msg, '(a, i2, a, i1)') 'getgb2r2: lugb ', lugb, ' idxver ', idxver
call g2_log(1)
#endif

! Get info.
nullify(gfld%bmap, gfld%fld)
iret = 0
Expand Down Expand Up @@ -995,6 +1014,7 @@ end subroutine getgb2rp
!>
!> @author Edward Hartnett, Stephen Gilbert @date Feb 13, 2024
subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret)
use g2logging
implicit none

integer, intent(in) :: lugb, idxver
Expand All @@ -1015,6 +1035,12 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret)
integer :: mypos, inc = 0
integer (kind = 8) :: lread8, iskip8, leng8, len2_8, len7_8, len6_8

#ifdef LOGGING
write(g2_log_msg, '(a, i2, a, i1, a, l)') 'getgb2rp2: lugb ', lugb, ' idxver ', idxver, &
' extract ', extract
call g2_log(1)
#endif

iret = 0

! Extract grib message from file.
Expand Down
43 changes: 42 additions & 1 deletion src/g2index.F90
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ end subroutine getidx
!>
!> @author Stephen Gilbert, Ed Hartnett @date Feb 9, 2024
subroutine getidx2(lugb, lugi, idxver, cindex, nlen, nnum, iret)
use g2logging
implicit none

integer, intent(in) :: lugb, lugi
Expand Down Expand Up @@ -293,6 +294,8 @@ subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret)
character(len=1), pointer, dimension(:) :: cbuf
integer, intent(out) :: idxver, nlen, nnum, iret
end subroutine getg2i2
end interface
interface
subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, &
nlen, nnum, nmess, iret)
integer, intent(in) :: lugb
Expand All @@ -303,6 +306,13 @@ subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, &
end subroutine getg2i2r
end interface

#ifdef LOGGING
! Log results for debugging.
write(g2_log_msg, '(a, i2, a, i2, a, i1)') 'getidx2: lugb ', lugb, ' lugi ', lugi, &
' idxver ', idxver
call g2_log(1)
#endif

! Free all associated memory and exit.
if (lugb .eq. 0) then
!print *, 'getidx: Freeing all memory'
Expand Down Expand Up @@ -501,6 +511,7 @@ end subroutine getg2i
!>
!> @author Ed Hartnett, Mark Iredell @date Feb 9, 2024
subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret)
use g2logging
implicit none

integer, intent(in) :: lugi
Expand All @@ -510,6 +521,12 @@ subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret)
character chead*162
integer :: ios, istat, lbuf, lhead, nskp

#ifdef LOGGING
! Log results for debugging.
write(g2_log_msg, '(a, i2, a, i1)') 'getg2i2: lugi ', lugi, ' idxver ', idxver
call g2_log(1)
#endif

nullify(cbuf)
nlen = 0
nnum = 0
Expand Down Expand Up @@ -624,6 +641,7 @@ end subroutine getg2ir
!>
!> @author Mark Iredell, Ed Hartnett @date 1995-10-31
subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, iret)
use g2logging
use re_alloc ! needed for subroutine realloc
implicit none

Expand All @@ -650,6 +668,13 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret)
end subroutine ix2gb2
end interface

#ifdef LOGGING
! Log results for debugging.
write(g2_log_msg, '(a, i2, a, i7, a, i7, a, i5, a, i1)') 'getg2i2r: lugb ', lugb, ' msk1 ', msk1, ' msk2 ', msk2, &
' mnum ', mnum, ' idxver ', idxver
call g2_log(1)
#endif

! Initialize.
iret = 0
nullify(cbuf)
Expand Down Expand Up @@ -898,6 +923,7 @@ end subroutine getgb2s
subroutine getgb2s2(cbuf, idxver, nlen, nnum, j, jdisc, jids, jpdtn, jpdt, jgdtn, &
jgdt, k, gfld, lpos, iret)
use grib_mod
use g2logging
implicit none

character(len = 1), intent(in) :: cbuf(nlen)
Expand Down Expand Up @@ -953,6 +979,13 @@ subroutine gf_unpack5(cgrib, lcgrib, iofst, ndpts, idrsnum, &
end subroutine gf_unpack5
end interface

#ifdef LOGGING
! Log results for debugging.
write(g2_log_msg, '(a, i1, a, i5, a, i7, a, i3, a, i3)') 'getgb2s2: idxver ', idxver, ' nlen ', nlen, &
' nnum ', nnum, ' j ', j, ' jdisc ', jdisc
call g2_log(1)
#endif

! Initialize.
k = 0
lpos = 0
Expand Down Expand Up @@ -1212,6 +1245,7 @@ end subroutine ixgb2
!> @author Ed Hartnett, Mark Iredell @date Feb 5, 2024
subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret)
use re_alloc ! needed for subroutine realloc
use g2logging
implicit none

! Subroutine parameters.
Expand All @@ -1232,7 +1266,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret)
integer (kind = 8) :: ibread8, lbread8, ibskip8, lengds8
integer (kind = 8) :: ilnpds8, ilndrs8
integer :: lensec, lensec1
integer :: mypos, inc, i
integer :: mypos, inc

! Parameters.
! Size of the internal char buffers used in this subroutine.
Expand Down Expand Up @@ -1264,6 +1298,13 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret)
character cbread(LINMAX), cindex(LINMAX)
character cids(LINMAX), cgds(LINMAX)

#ifdef LOGGING
! Log results for debugging.
write(g2_log_msg, '(a, i2, a, i7, a, i1)') 'ix2gb2: lugb ', lugb, ' lskip8 ', lskip8, &
' idxver ', idxver
call g2_log(1)
#endif

! Are we using index version 1 (legacy), or version 2 (introduced to
! handle files > 2 GB).
if (idxver .eq. 1) then
Expand Down
28 changes: 28 additions & 0 deletions src/g2logging.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
!> @file
!> @brief Logging for the g2 library.
!> @author Edward Hartnett @date 5/15/24

!> @brief Logging for the g2 library.
!>
!> Set g2_log_level to turn on logging.
!>
!> @author Edward Hartnett @date 5/15/24
module g2logging
integer g2_log_level !< 0 for no logging.
character* 120 g2_log_msg !< Fill this with the logging message.

contains
!> Print a debug message for the g2 library.
!>
!> @param[in] level If this is lower or equal to the current setting
!> of g2_log_level, print this log message.
!>
!> @author Edward Hartnett @date 5/15/24
subroutine g2_log(level)
integer, intent(in) :: level

if (level .le. g2_log_level) then
print *, g2_log_msg
endif
end subroutine g2_log
end module g2logging
3 changes: 3 additions & 0 deletions tests/test_getgb2rp.F90
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
! Ed Hartnett 7/26/22
program test_getgb2rp
use bacio_module
use g2logging
implicit none

integer :: lugi
Expand Down Expand Up @@ -50,7 +51,9 @@ end subroutine getgb2rp
! Extract the whole message.
extract = .false.
nullify(gribm)
g2_log_level = 3
call getgb2rp(lugb, cbuf, extract, gribm, leng, iret)
g2_log_level = 0
print *, 'leng ', leng
if (leng .ne. 11183) stop 110
! Deallocate buffer that got GRIB message.
Expand Down

0 comments on commit 8db6654

Please sign in to comment.