{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Network.Bugsnag.StackFrame
( attachBugsnagCode
, currentStackFrame
) where
import Prelude
import Data.Bugsnag
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text, pack, unpack)
import Instances.TH.Lift ()
import Language.Haskell.TH.Syntax
import Network.Bugsnag.CodeIndex
attachBugsnagCode :: CodeIndex -> StackFrame -> StackFrame
attachBugsnagCode :: CodeIndex -> StackFrame -> StackFrame
attachBugsnagCode CodeIndex
index StackFrame
sf =
StackFrame
sf
{ stackFrame_code :: Maybe (HashMap Int Text)
stackFrame_code =
String -> Int -> CodeIndex -> Maybe (HashMap Int Text)
findBugsnagCode
(Text -> String
unpack forall a b. (a -> b) -> a -> b
$ StackFrame -> Text
stackFrame_file StackFrame
sf)
(StackFrame -> Int
stackFrame_lineNumber StackFrame
sf)
CodeIndex
index
}
findBugsnagCode :: FilePath -> Int -> CodeIndex -> Maybe (HashMap Int Text)
findBugsnagCode :: String -> Int -> CodeIndex -> Maybe (HashMap Int Text)
findBugsnagCode String
path Int
n =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (Int, Int) -> CodeIndex -> Maybe [(Int, Text)]
findSourceRange String
path (Int
begin, Int
n forall a. Num a => a -> a -> a
+ Int
3)
where
begin :: Int
begin
| Int
n forall a. Ord a => a -> a -> Bool
< Int
3 = Int
0
| Bool
otherwise = Int
n forall a. Num a => a -> a -> a
- Int
3
currentStackFrame :: Q Exp
currentStackFrame :: Q Exp
currentStackFrame = [|locStackFrame $(qLocation >>= liftLoc)|]
locStackFrame :: Loc -> Text -> StackFrame
locStackFrame :: Loc -> Text -> StackFrame
locStackFrame (Loc String
path String
_ String
_ (Int
ls, Int
cs) (Int, Int)
_) Text
func =
StackFrame
defaultStackFrame
{ stackFrame_file :: Text
stackFrame_file = String -> Text
pack String
path
, stackFrame_lineNumber :: Int
stackFrame_lineNumber = Int
ls
, stackFrame_columnNumber :: Maybe Int
stackFrame_columnNumber = forall a. a -> Maybe a
Just Int
cs
, stackFrame_method :: Text
stackFrame_method = Text
func
, stackFrame_inProject :: Maybe Bool
stackFrame_inProject = forall a. a -> Maybe a
Just Bool
True
,
stackFrame_code :: Maybe (HashMap Int Text)
stackFrame_code = forall a. Maybe a
Nothing
}
liftLoc :: Loc -> Q Exp
liftLoc :: Loc -> Q Exp
liftLoc (Loc String
a String
b String
c (Int
d1, Int
d2) (Int
e1, Int
e2)) =
[|
Loc
$(lift a)
$(lift b)
$(lift c)
($(lift d1), $(lift d2))
($(lift e1), $(lift e2))
|]