Skip to content

Commit

Permalink
Improved basic_config() example
Browse files Browse the repository at this point in the history
  - also fixed minor bug
  • Loading branch information
tclune committed Apr 17, 2020
1 parent 598b3af commit 2881249
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
7 changes: 6 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
of Python's logger.
- Also added overload of get_logger() with no name argument which
returns the root logger. (Again as per Python.)

- Added example/basic_config/basic_config.F90 (requires MPI)

### Fixed
- minor bug in default fmt_ for MpiFormatter
"rank" should have been "mpi_rank".

## [1.3.6] - 2020-04-16

### Fixed
Expand Down
30 changes: 25 additions & 5 deletions examples/basic_config/basic_config.F90
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,41 @@ end subroutine sub_B
program main
use, intrinsic :: iso_fortran_env, only: ERROR_UNIT, OUTPUT_UNIT
use pflogger
use mpi
implicit none

integer :: ier
class (Logger), pointer :: log
integer :: status
type(StreamHandler) :: stream
type(FileHandler) :: fh
type(HandlerVector) :: hv

call example_init()

call initialize() ! init logger
stream = StreamHandler()
call stream%set_level(DEBUG)
call logging%basic_config(stream=stream, level=DEBUG, rc=status)

fh = FileHandler('debug')
call fh%set_lock(MpiLock(MPI_COMM_WORLD))
call fh%set_level(DEBUG)
call fh%set_formatter(MpiFormatter(MPI_COMM_WORLD))
call hv%push_back(fh)

fh = FileHandler('info')
call fh%set_lock(MpiLock(MPI_COMM_WORLD))
call fh%set_level(INFO)
call hv%push_back(fh)

fh = FileHandler('warn')
call fh%set_lock(MpiLock(MPI_COMM_WORLD))
call fh%set_level(WARNING)
call hv%push_back(fh)

call hv%push_back(stream)

call logging%basic_config(handlers=hv, level=DEBUG, rc=status)
if (status /= 0) error stop 'basic_config() failed'

log => logging%get_logger('main')
Expand All @@ -80,19 +101,18 @@ program main
! own code.

subroutine example_init()
#ifdef LOGGER_USE_MPI

integer :: ier
call mpi_init(ier)
#endif

end subroutine example_init

subroutine example_finalize()
#ifdef LOGGER_USE_MPI


integer :: ier
call mpi_finalize(ier)
#endif

end subroutine example_finalize

end program main
Expand Down
4 changes: 2 additions & 2 deletions src/MpiFormatter.F90
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function newMpiFormatter_comm(comm, unused, rank_keyword, size_keyword, fmt, dat
type (Unusable), optional :: unused
character(len=*), optional, intent(in) :: rank_keyword
character(len=*), optional, intent(in) :: size_keyword
character(len=*), intent(in) :: fmt
character(len=*), optional, intent(in) :: fmt
character(len=*), optional, intent(in) :: datefmt

type (StringUnlimitedMap) :: dictionary
Expand All @@ -42,7 +42,7 @@ function newMpiFormatter_comm(comm, unused, rank_keyword, size_keyword, fmt, dat
! workaround for gfortran 10.0
call init_MpiCommConfig(dictionary, comm, rank_keyword=rank_keyword, size_keyword=size_keyword)

fmt_ = default(fmt, 'pe=%(rank)a~: %(name)a~: %(message)a')
fmt_ = default(fmt, 'pe=%(mpi_rank)i4.4~: %(name)a~: %(message)a')
f%Formatter = Formatter(fmt_, datefmt=datefmt, extra=dictionary)

end function newMpiFormatter_comm
Expand Down
2 changes: 2 additions & 0 deletions src/pflogger.F90
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module pflogger
# ifdef SUPPORT_FOR_MPI_ALLOC_MEM_CPTR
use PFL_MpiLock
use PFL_MpiFilter
use PFL_MpiFormatter
# endif
#endif
use PFL_RotatingFileHandler
Expand Down Expand Up @@ -58,6 +59,7 @@ module pflogger
# ifdef SUPPORT_FOR_MPI_ALLOC_MEM_CPTR
public :: MpiLock
public :: MpiFilter
public :: MpiFormatter
# endif
#endif

Expand Down

0 comments on commit 2881249

Please sign in to comment.