module Trace

  ! Print trace beginnings and ends

  use Tree, only: Dump_Tree_Node

  implicit NONE
  private

  public :: Trace_Begin, Trace_End
  public :: Trace_Begin_Array, Trace_End_Array
  public :: Trace_Begin_Scalar, Trace_End_Scalar

  interface Trace_Begin
    procedure Trace_Begin_Scalar
  end interface Trace_Begin

  interface Trace_End
    procedure Trace_End_Scalar
  end interface Trace_End

  integer, private :: Depth = -1

!---------------------------- RCS Module Info ------------------------------
  character (len=*), private, parameter :: ModuleName= &
       "$RCSfile: Trace.f90,v $"
  private :: not_used_here 
!---------------------------------------------------------------------------

contains

  subroutine Trace_Begin_Array ( Who, Number, Text, For, Node )
    use Output_m, only: NewLine, Output
    character(len=*), intent(in) :: Who
    integer, intent(in), optional :: Number(:)
    character(len=*), intent(in), optional :: Text
    character(len=*), intent(in), optional :: For
    integer, intent(in), optional :: Node ! A tree node
    integer :: I
    depth = depth + 1
    call output ( repeat('.',max(depth,0)) )
    call output ( 'Enter ' )
    call output ( trim(who) )
    if ( present(for) ) call output ( for )
    if ( present(text) ) call output ( text )
    if ( present(node) ) then
      call output ( node, after=':' )
      call dump_tree_node ( node, 0 )
    end if
    if ( present(number) ) then
      do i = 1, size(number)
        call output ( number(i), before=' ' )
      end do
    end if
    call newLine
  end subroutine Trace_Begin_Array

  subroutine Trace_Begin_Scalar ( Who, Number, Text, For, Node )
    use Output_m, only: NewLine, Output
    character(len=*), intent(in) :: Who
    integer, intent(in), optional :: Number
    character(len=*), intent(in), optional :: Text
    character(len=*), intent(in), optional :: For
    integer, intent(in), optional :: Node ! A tree node
    depth = depth + 1
    call output ( repeat('.',max(depth,0)) )
    call output ( 'Enter ' )
    call output ( trim(who) )
    if ( present(for) ) call output ( for )
    if ( present(text) ) call output ( text )
    if ( present(node) ) then
      call output ( node, after=':' )
      call dump_tree_node ( node, 0 )
    end if
    if ( present(number) ) call output ( number, before=' ' )
    call newLine
  end subroutine Trace_Begin_Scalar

  subroutine Trace_End_Array ( Who, Number, Text, Node, Advance )
    use Output_m, only: Output
    character(len=*), intent(in) :: Who
    integer, intent(in), optional :: Number(:)
    character(len=*), intent(in), optional :: Text
    integer, intent(in), optional :: Node ! A tree node
    character(*), intent(in), optional :: Advance ! Default 'yes'
    integer :: I
    character(3) :: MyAdv
    myAdv = 'yes'
    if ( present(advance) ) myAdv = advance
    call output ( repeat('.',max(depth,0)) )
    depth = depth - 1
    call output ( 'Exit  ' )
    call output ( trim(who) )
    if ( present(text) ) call output ( text )
    if ( present(node) ) then
      call output ( node, after=':' )
      call dump_tree_node ( node, 0 )
    end if
    if ( present(number) ) then
      do i = 1, size(number)
        call output ( number(i), before=' ' )
      end do
    end if
    call output ( '', advance=myAdv )
  end subroutine Trace_End_Array

  subroutine Trace_End_Scalar ( Who, Number, Text, Node, Advance )
    use Output_m, only: Output
    character(len=*), intent(in) :: Who
    integer, intent(in), optional :: Number
    character(len=*), intent(in), optional :: Text
    integer, intent(in), optional :: Node ! A tree node
    character(*), intent(in), optional :: Advance ! Default 'yes'
    character(3) :: MyAdv
    myAdv = 'yes'
    if ( present(advance) ) myAdv = advance
    call output ( repeat('.',max(depth,0)) )
    depth = depth - 1
    call output ( 'Exit  ' )
    call output ( trim(who) )
    if ( present(text) ) call output ( text )
    if ( present(number) ) call output ( number )
    if ( present(node) ) then
      call output ( node, after=':' )
      call dump_tree_node ( node, 0 )
    end if
    call output ( '', advance=myAdv )
  end subroutine Trace_End_Scalar

!--------------------------- end bloc --------------------------------------
  logical function not_used_here()
  character (len=*), parameter :: IdParm = &
       "$Id: Trace.f90,v 1.2 2022/01/24 22:29:06 vsnyder Exp $"
  character (len=len(idParm)) :: Id = idParm
    not_used_here = (id(1:1) == ModuleName(1:1))
    print *, Id ! .mod files sometimes change if PRINT is added
  end function not_used_here
!---------------------------------------------------------------------------

end module Trace

! $Log: Trace.f90,v $
! Revision 1.2  2022/01/24 22:29:06  vsnyder
! Add Log line
!

