packages feed

relude-1.0.0.1 / src / Relude / Extra / CallStack.hs

{-# LANGUAGE Safe #-}{- |Copyright:  (c) 2018-2021 KowainikSPDX-License-Identifier: MITMaintainer:  Kowainik <xrom.xkov@gmail.com>Stability:   ExperimentalPortability: PortableContains useful functions to work with GHC callstack.@since 0.2.0-}module Relude.Extra.CallStack    ( ownName    , callerName    ) whereimport Relude{- | This function returns the name of its caller function, but it requiresthat the caller function has 'HasCallStack' constraint. Otherwise, it returns@"<unknown>"@.>>> foo :: HasCallStack => String; foo = ownName>>> foo"foo">>> bar :: HasCallStack => String; bar = foo>>> bar"foo"@since 0.2.0-}ownName :: HasCallStack => StringownName = case getCallStack callStack of    _:caller:_ -> fst caller    _          -> "<unknown>"{- | This function returns the name of its caller of the caller function, but itrequires that the caller function and caller of the caller function have'HasCallStack' constraint. Otherwise, it returns @"<unknown>"@. It's useful forlogging:>>> log :: HasCallStack => String -> IO (); log s = putStrLn $ callerName ++ ":" ++ s>>> greeting :: HasCallStack => IO (); greeting = log "Starting..." >> putStrLn "Hello!" >> log "Ending...">>> greetinggreeting:Starting...Hello!greeting:Ending...@since 0.2.0-}callerName :: HasCallStack => StringcallerName = case getCallStack callStack of    _:_:caller:_ -> fst caller    _            -> "<unknown>"