Add fancier trace debugging functions to Hasura.Prelude

## Suggestion: Add fancier trace debugging functions to `Hasura.Prelude`

This PR adds two trace functions, `ltrace` and `ltraceM`, which use the `pretty-simple` package to `show` the input with nice formatting and colors for ease of reading (and comparing using diff tools such as `meld` or `vim-diff`).

I've also added warning pragmas to the functions, which means:
1. Traces will not be left in code, as CI builds with -Werror
2. Developers will have to change the `ghc-options` to `-Wwarn` in their `cabal.project.local` settings to use these functions

### Example

Usage:

```hs
selectFunctionAggregate ... = ... do
  ltraceM "functionInfo" function
  ...
```

Output to terminal looks like this:

<img width="524" alt="Screen Shot 2021-08-12 at 10 33 24" src="https://user-images.githubusercontent.com/8547573/129158878-4a5e96ba-30a5-452c-8f33-9eb4b2cc5e2a.png">

### Dependencies

Requires adding the following dependencies:
  - prettyprinter-ansi-terminal-1.1.2 (BSD2)
  - pretty-simple-4.0.0.0 (BSD3)

Question: what is the process for adding new dependencies? How does decisions on this matter happen?

https://github.com/hasura/graphql-engine-mono/pull/2075

GitOrigin-RevId: 490b0f0ca595da319b43e92e190ba50c0b132cd5
This commit is contained in:
Gil Mizrahi 2021-08-12 22:33:01 +03:00 committed by hasura-bot
parent 6b3db83737
commit 904029aa7d
3 changed files with 21 additions and 0 deletions

View File

@ -281,9 +281,11 @@ constraints: any.Cabal ==3.2.0.0,
any.postgresql-libpq ==0.9.4.3,
postgresql-libpq -use-pkg-config,
any.pretty ==1.1.3.6,
any.pretty-simple ==4.0.0.0,
any.pretty-show ==1.10,
any.prettyprinter ==1.7.0,
prettyprinter -buildreadme,
prettyprinter-ansi-terminal ==1.1.2,
any.primitive ==0.7.1.0,
any.primitive-extras ==0.8,
any.primitive-unlifted ==0.1.3.0,

View File

@ -131,6 +131,7 @@ library
, pg-client
, postgresql-binary
, postgresql-libpq
, pretty-simple
, process
, profunctors
, retry

View File

@ -19,6 +19,9 @@ module Hasura.Prelude
, hoistMaybe
, hoistEither
, tshow
-- * Trace debugging
, ltrace
, ltraceM
-- * Efficient coercions
, coerce
, findWithIndex
@ -96,8 +99,11 @@ import qualified Data.Hashable as H
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Encoding.Error as TE
import qualified Data.Text.Lazy as TL
import qualified GHC.Clock as Clock
import Debug.Trace (trace, traceM)
import qualified Text.Pretty.Simple as PS
alphabet :: String
alphabet = ['a'..'z'] ++ ['A'..'Z']
@ -233,3 +239,15 @@ instance (Hashable a) => Hashable (Seq a) where
-- as @foldMap id@, per the documentation in @Data.Foldable@.
fold' :: (Monoid m, Foldable t) => t m -> m
fold' = foldMap' id
-- Fancy trace debugging
-- | Labeled, prettified traceShowId
ltrace :: Show a => String -> a -> a
ltrace lbl x = trace (lbl <> ": " <> TL.unpack (PS.pShow x)) x
{-# warning ltrace "ltrace left in code" #-}
-- | Labeled, prettified traceShowM
ltraceM :: Applicative m => Show a => String -> a -> m ()
ltraceM lbl x = traceM (lbl <> ": " <> TL.unpack (PS.pShow x))
{-# warning ltraceM "ltraceM left in code" #-}