Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debugging custom function to Prelude #1401

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Juvix/Prelude.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Juvix.Prelude
( module Juvix.Prelude.Base,
module Juvix.Prelude.Trace,
module Juvix.Prelude.Error,
module Juvix.Prelude.Files,
module Juvix.Prelude.Lens,
Expand All @@ -12,3 +13,4 @@ import Juvix.Prelude.Error
import Juvix.Prelude.Files
import Juvix.Prelude.Lens
import Juvix.Prelude.Loc
import Juvix.Prelude.Trace
2 changes: 1 addition & 1 deletion src/Juvix/Prelude/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ tableNestedInsert ::
a ->
HashMap k1 (HashMap k2 a) ->
HashMap k1 (HashMap k2 a)
tableNestedInsert k1 k2 a = tableInsert (HashMap.singleton k2) (HashMap.insert k2) k1 a
tableNestedInsert k1 k2 = tableInsert (HashMap.singleton k2) (HashMap.insert k2) k1

--------------------------------------------------------------------------------
-- NonEmpty
Expand Down
42 changes: 42 additions & 0 deletions src/Juvix/Prelude/Trace.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Juvix.Prelude.Trace where

import Data.Text qualified as Text
import Debug.Trace qualified as T
import GHC.IO (unsafePerformIO)
import Juvix.Prelude.Base

setDebugMsg :: Text -> Text
setDebugMsg msg = "[debug] " <> fmsg <> "\n"
where
fmsg
| Text.null msg = ""
| otherwise = msg <> " :"

traceLabel :: Text -> Text -> a -> a
traceLabel msg a = T.trace (unpack $ setDebugMsg msg <> a)
{-# WARNING traceLabel "Using traceLabel" #-}

trace :: Text -> a -> a
trace = traceLabel ""
{-# WARNING trace "Using trace" #-}

traceShow :: Show b => b -> b
traceShow b = traceLabel "" (pack . show $ b) b
{-# WARNING traceShow "Using traceShow" #-}

traceToFile :: FilePath -> Text -> a -> a
traceToFile fpath t a =
traceLabel (pack ("[" <> fpath <> "]")) t $
unsafePerformIO $
do
writeFile fpath t
return a
{-# WARNING traceToFile "Using traceToFile" #-}

traceToFile' :: Text -> a -> a
traceToFile' = traceToFile "./juvix.log"
{-# WARNING traceToFile' "Using traceToFile'" #-}

traceToFileM :: (Applicative m) => FilePath -> Text -> a -> m ()
traceToFileM fpath t a = pure (traceToFile fpath t a) $> ()
{-# WARNING traceToFileM "Using traceFileM" #-}