Fortran error handling including stacktrace generation

I like this. Maybe it is already there, just starting to look but in my error processing procedures I like to easily incorporate intrinsic numbers into many of my messages using something like this, which I changed to be pure …

M_msg
 module M_msg
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64, real32, real64, real128
implicit none
private
public msg
contains
! str(3f) - [M_msg] converts up to nine standard scalar type values to a string
pure function msg(g0, g1, g2, g3, g4, g5, g6, g7, g8, g9)
implicit none
class(*),intent(in),optional  :: g0, g1, g2, g3, g4, g5, g6, g7, g8, g9
character(len=:),allocatable  :: msg
character(len=4096)           :: line
integer                       :: istart
   istart=1
   line=''
   if(present(g0))call print_g(g0,line,istart)
   if(present(g1))call print_g(g1,line,istart)
   if(present(g2))call print_g(g2,line,istart)
   if(present(g3))call print_g(g3,line,istart)
   if(present(g4))call print_g(g4,line,istart)
   if(present(g5))call print_g(g5,line,istart)
   if(present(g6))call print_g(g6,line,istart)
   if(present(g7))call print_g(g7,line,istart)
   if(present(g8))call print_g(g8,line,istart)
   if(present(g9))call print_g(g9,line,istart)
   msg=trim(line)
contains
pure subroutine print_g(g,line,istart)
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64, real32, real64, real128
class(*),intent(in) :: g
character(len=4096),intent(inout) :: line
integer,intent(inout) :: istart
integer,parameter :: increment=2
character(len=*),parameter :: sep=' '
   select type(g)
      type is (integer(kind=int8));     write(line(istart:),'(i0)') g
      type is (integer(kind=int16));    write(line(istart:),'(i0)') g
      type is (integer(kind=int32));    write(line(istart:),'(i0)') g
      type is (integer(kind=int64));    write(line(istart:),'(i0)') g
      type is (real(kind=real32));      write(line(istart:),'(1pg0)') g
      type is (real(kind=real64));      write(line(istart:),'(1pg0)') g
      !type is (real(kind=real128));     write(line(istart:),'(1pg0)') g
      type is (logical);                write(line(istart:),'(l1)') g
      type is (character(len=*));       write(line(istart:),'(a)') trim(g)
      type is (complex);                write(line(istart:),'("(",1pg0,",",1pg0,")")') g
   end select
   istart=len_trim(line)+increment
   line=trim(line)//sep
end subroutine print_g
end function msg
end module M_msg
program testit
use M_msg, only : msg
character(len=:),allocatable :: text
text=msg('it is',.true.,'I like to add some numbers like',10,'and',30.4,'and',(30.0,40.0),'to the message')
print *, text
end program testit

Would that be possible to incorporate? Are there major cons to doing so? (sorry if I missed it and it does that already!)