File: FileLocation.hs

package info (click to toggle)
haskell-file-location 0.4.9.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 112 kB
  • sloc: haskell: 282; ansic: 13; makefile: 2; sh: 1
file content (52 lines) | stat: -rw-r--r-- 1,756 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE TemplateHaskell #-}
-- | functions that help you with debugging.
-- Most would make sense in the Debug.Trace module
-- There are Template Haskell versions that show you file locaiton information
module Debug.FileLocation
  (debug, debugM, debugMsg, dbg, dbgMsg, trc, ltrace, ltraceM, strace, traceId, __LOC__)
  where

import Control.Applicative ((<$>), (<*>), pure)
import Language.Haskell.TH (recConE, litE, stringL, integerL)
import Language.Haskell.TH.Instances
import Language.Haskell.TH.Syntax
import Debug.Util
import Debug.Trace (trace)
import FileLocation.LocationString (locationToString)

-- | TH  version of Debug.Trace.trace that just prints a value.
dbg :: Q Exp
dbg = do
  loc <- qLocation
  let pre = "DEBUG: " ++ (locationToString loc)
  [|(\_x -> ltrace pre _x)|]

-- | TH version  of Debug.Trace.trace that prints a value and a message
-- prefix.
dbgMsg :: String -> Q Exp
dbgMsg msg = do
  loc <- qLocation
  let pre = "DEBUG: " ++ (locationToString loc) ++ ' ' : msg
  [|(\_x -> ltrace pre _x)|]

-- | A TH version of Debug.Trace.trace that prints location information
trc :: String -> Q Exp
trc str = do
  loc <- qLocation
  let prefix = "TRACE: " ++ (locationToString loc) ++ " "
  [|trace (prefix ++ str)|]

-- | A TH monadic version of debug - print a value with location information as a stand alone expression in a monad
dbgM :: Q Exp
dbgM = do
  loc <- qLocation
  let prefix = "DEBUG: " ++ (locationToString loc) ++ " "
  [|(\_x -> ltraceM (prefix ++ show _x) _x)|]

-- | Embed an expression of type Loc containing the location
-- information for the place where it appears.  Could be used in
-- custom Exception types and similar:
--
-- > throw $ MyException $__LOC__
__LOC__ :: Q Exp
__LOC__ = lift =<< location