Skip to content

Commit

Permalink
Merge pull request #26 from Goddard-Fortran-Ecosystem/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tclune authored Apr 17, 2020
2 parents 38c8c47 + cb8274d commit 3165f91
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# ------------------------------------------------------------------------ #
cmake_minimum_required (VERSION 3.8.0)
project (PFLOGGER
VERSION 1.3.6
VERSION 1.4.0
LANGUAGES Fortran)

set (CMAKE_MODULE_PATH
Expand Down
13 changes: 13 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.4.0] - 2020-04-17

### Added
- Added basic_config() method for LoggerManager analogous to that
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
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(serial)
add_subdirectory(basic_config)
if (MPI_FOUND)
add_subdirectory(mpi)
endif()
14 changes: 14 additions & 0 deletions examples/basic_config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include_directories(${CMAKE_BINARY_DIR}/src)
include_directories(${FTL}/mod)
link_directories(${FTL}/lib)

file (GLOB CONFIG_INPUTS "${CMAKE_CURRENT_SOURCE_DIR}/*.cfg")
file (COPY ${CONFIG_INPUTS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

if (MPI_FOUND)

add_executable(basic_config.x basic_config.F90)
target_link_libraries(basic_config.x pflogger MPI::MPI_Fortran)

endif()

121 changes: 121 additions & 0 deletions examples/basic_config/basic_config.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
! This program demonstrates the use of basic config
! which allows sidestepping YAML processing.


subroutine sub_A()
use pflogger

integer :: i
class (Logger), pointer :: log
class (Logger), pointer :: plog

log => logging%get_logger('main.A')

call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)
call log%info('inside sub_A')

call log%warning('empty procedure')
call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)

end subroutine sub_A


subroutine sub_B()
use pflogger

integer :: i
class (Logger), pointer :: log
class (Logger), pointer :: plog

log => logging%get_logger('main.B')

call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)
call log%info('inside sub_B')

call log%error('this procedure is empty as well')
call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)

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)

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')

call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)
call log%info('calling sub_A()')
call sub_A()

call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)
call log%info('calling sub_B()')
call sub_B()

call log%debug('at line: %i3.3 in file: %a', __LINE__,__FILE__)


call example_finalize()

contains

! The procedures below are simply to allow the example to link with
! an MPI build of the logging framework. Users that are
! uninterested in MPI should _NOT_ need these procedures in their
! own code.

subroutine example_init()

integer :: ier
call mpi_init(ier)

end subroutine example_init

subroutine example_finalize()


integer :: ier
call mpi_finalize(ier)

end subroutine example_finalize

end program main



97 changes: 92 additions & 5 deletions src/LoggerManager.F90
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ module PFL_LoggerManager
use yafyaml, only: Configuration, ConfigurationIterator
use yafyaml, only: FileStream
use yafyaml, only: yayfaml_SUCCESS => SUCCESS
use yafyaml, only: None
use PFL_RootLogger, only: RootLogger
use PFL_SeverityLevels
use PFL_Logger, only: Logger, newLogger
use PFL_AbstractLogger
use PFL_LoggerPolyVector
use PFL_StringAbstractLoggerPolyMap
use PFL_KeywordEnforcer
#ifdef _LOGGER_USE_MPI
use mpi
#endif
Expand All @@ -40,7 +40,10 @@ module PFL_LoggerManager
type (RootLogger) :: root_node
type (LoggerMap) :: loggers
contains
procedure :: get_logger
procedure :: get_logger_name
procedure :: get_logger_root
generic :: get_logger => get_logger_name
generic :: get_logger => get_logger_root
procedure, private :: fixup_ancestors
procedure, private :: fixup_children
procedure, nopass :: get_parent_prefix
Expand All @@ -49,6 +52,7 @@ module PFL_LoggerManager
procedure :: load_config
procedure :: build_loggers
procedure :: build_root_logger
procedure :: basic_config

end type LoggerManager

Expand Down Expand Up @@ -92,7 +96,7 @@ end function new_LoggerManager

!---------------------------------------------------------------------------
! FUNCTION:
! get_logger
! get_logger_name
!
! DESCRIPTION:
! Get a logger with the specified 'name', creating it if necessary.
Expand All @@ -101,7 +105,25 @@ end function new_LoggerManager
! etc.
! 2) 'name' is case insensitive.
!---------------------------------------------------------------------------
function get_logger(this, name) result(lgr)
function get_logger_root(this) result(lgr)
class (Logger), pointer :: lgr
class (LoggerManager), target, intent(inout) :: this

lgr => this%root_node
end function get_logger_root

!---------------------------------------------------------------------------
! FUNCTION:
! get_logger_name
!
! DESCRIPTION:
! Get a logger with the specified 'name', creating it if necessary.
! Note that:
! 1) 'name' is a dot-separated hierarchical name such as 'A','A.B','A.B.C',
! etc.
! 2) 'name' is case insensitive.
!---------------------------------------------------------------------------
function get_logger_name(this, name) result(lgr)
class (Logger), pointer :: lgr
class (LoggerManager), target, intent(inout) :: this
character(len=*), intent(in) :: name
Expand Down Expand Up @@ -159,7 +181,7 @@ function get_logger(this, name) result(lgr)

end if

end function get_logger
end function get_logger_name

subroutine fixup_ancestors(this, lgr)
class (LoggerManager), target, intent(inout) :: this
Expand Down Expand Up @@ -400,4 +422,69 @@ subroutine build_root_logger(this, cfg, elements, unused, extra)

end subroutine build_root_logger

subroutine basic_config(this, unusable, filename, level, stream, force, handlers, rc)
use pfl_StreamHandler
use pfl_FileHandler
use pfl_AbstractHandlerPolyVector
use pfl_AbstractHandler
class(LoggerManager), intent(inout) :: this
class(KeywordEnforcer), optional, intent(in) :: unusable
character(*), optional, intent(in) :: filename
integer, optional, intent(in) :: level
type(StreamHandler), optional, intent(in) :: stream
logical, optional, intent(in) :: force
type(HandlerVector), optional, intent(in) :: handlers
integer, optional :: rc

type(HandlerVector), pointer :: existing_handlers
type(HandlerVectorIterator) :: iter
class(AbstractHandler), pointer :: h

existing_handlers => this%root_node%get_handlers()

if (existing_handlers%size() > 0) then
if (present(force)) then
if (.not. force) then
if (present(rc)) rc = 0 ! success - do nothing
return
end if
else ! force not present
if (present(rc)) rc = 0 ! success - do nothing
return
end if
end if

! Else ...

! Check that conflicting arguments are not present
if (count([present(filename),present(stream),present(handlers)]) > 1) then
rc = -1 ! conflicting arguments
return
end if

if (present(level)) then
call this%root_node%set_level(level)
end if

if (present(filename)) then
call this%root_node%add_handler(FileHandler(filename))
end if

if (present(stream)) then
call this%root_node%add_handler(stream)
end if

if (present(handlers)) then
iter = handlers%begin()
do while (iter /= handlers%end())
h => iter%get()
call this%root_node%add_handler(h)
call iter%next
end do
end if

if (present(rc)) rc = 0 ! success

end subroutine basic_config

end module PFL_LoggerManager
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
4 changes: 4 additions & 0 deletions src/pflogger.F90
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module pflogger
use Pfl_Exception
use PFL_LoggerManager
use PFL_AbstractHandlerPolyVector
use PFL_Logger
use PFL_RootLogger
use PFL_AbstractHandler
Expand All @@ -14,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 All @@ -30,6 +32,7 @@ module pflogger
public :: WrapArray

public :: AbstractHandler
public :: HandlerVector, HandlerVectorIterator
public :: StreamHandler
public :: FileHandler
public :: RotatingFileHandler
Expand All @@ -56,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 3165f91

Please sign in to comment.