packages feed

file-location (empty) → 0.2.0

raw patch · 6 files changed

+195/−0 lines, 6 filesdep +basedep +template-haskellsetup-changed

Dependencies added: base, template-haskell

Files

+ Debug/FileLocation.hs view
@@ -0,0 +1,66 @@+{-# LANGUAGE TemplateHaskell #-}+module Debug.FileLocation+  (debug, debugM, debugMsg, dbg, dbgMsg, trc, ltrace, ltraceM, strace)+  where++import Language.Haskell.TH.Syntax+import Debug.Trace+import FileLocation.Internal (locationToString)++-- | A version of Debug.Trace.trace that just prints a value.+-- This should be included in Debug.Trace+debug :: Show a => a -> a+debug = ltrace "DEBUG"++-- | A version of Debug.Trace.trace that just prints a value and a message.+-- This should be included in Debug.Trace+debugMsg :: Show a => String -> a -> a+debugMsg msg = ltrace ("DEBUG: " ++ msg)++-- | 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)|]++-- | monadic debug - like debug, but works as a standalone line in a monad+-- TODO: TH version with error loaction info+debugM :: (Monad m, Show a) => a -> m a+debugM a = debug a `seq` return a++-- | trace (print on stdout at runtime) a showable expression+-- like debug, but does not print "DEBUG: "+strace :: Show a => a -> a+strace a = trace (show a) a++-- | labelled trace - like strace, with a label prepended+ltrace :: Show a => String -> a -> a+ltrace l a = trace (l ++ ": " ++ show a) a++-- | monadic debug - like debug, but works as a standalone line in a monad+-- TODO: TH version with error loaction info+ltraceM :: (Monad m, Show a) => String -> a -> m a+ltraceM str a = ltrace str a `seq` return a
+ FileLocation.hs view
@@ -0,0 +1,38 @@+{-# LANGUAGE TemplateHaskell #-}+-- | see Debug.FileLocation module for more definitions+module FileLocation+  (err, undef,+   debug, debugM, debugMsg, dbg, dbgMsg, trc, ltrace, ltraceM, strace+  )+  where++import FileLocation.Internal (locationToString)+import Debug.FileLocation (debug, debugM, debugMsg, dbg, dbgMsg, trc, ltrace, ltraceM, strace)+import Debug.Trace (trace)+-- future plans+-- import Control.Exception.FileLocation (thrw, thrwIO)++import Language.Haskell.TH.Syntax++-- | like Prelude.error, but gives the file location+--+-- > $(err "OH NO!)+-- > main:Main main.hs:4:10 OH NO!+err :: String -> Q Exp+err str = do+  loc <- qLocation+  let prefix = (locationToString loc) ++ " "+  [|error (prefix ++ str)|]++-- | like Prelude.undefined, but gives the file location+-- use trace to output the location.+-- this way we still use undefined instead of calling error+--+-- > $(undef)+-- > main:Main main.hs:4:10 undefined+-- > err: Prelude.undefined+undef :: Q Exp+undef = do+  loc <- qLocation+  let prefix = (locationToString loc) ++ " "+  [|trace (prefix ++ "undefined") undefined|]
+ FileLocation/Internal.hs view
@@ -0,0 +1,11 @@+module FileLocation.Internal (locationToString) where++import Language.Haskell.TH.Syntax++-- leaving out the loc_end parameter+locationToString :: Loc -> String+locationToString loc = (loc_package loc) ++ ':' : (loc_module loc) ++ +  ' ' : (loc_filename loc) ++ ':' : (line loc) ++ ':' : (char loc)+  where+    line = show . fst . loc_start+    char = show . snd . loc_start
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Greg Weber++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Greg Weber nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ file-location.cabal view
@@ -0,0 +1,48 @@+Name:                file-location+Version:             0.2.0+Synopsis:            common functions that show file location information+Homepage:            https://github.com/gregwebs/ErrorLocation.hs+License:             BSD3+License-file:        LICENSE+Author:              Greg Weber+Maintainer:          greg@gregweber.info+Category:            Development+Build-type:          Simple+Cabal-version:       >=1.6+Description: +    Common debugging/error/exception functions that give file location information+    .+    > $(err "OH NO!")+    >+    > main:Main main.hs:16:1 OH NO!+    .+    Notice how it displays package:module file:line:character+    It exposes the functions err (error), undef (undefined), and trc (Debug.Trace.trace). All of these behave the same as their normal counterpart but also spit out a location.+    .+    I also included my favorite helper, debug, which is like trace but just show the value.+    .+    > debug [1,2,3]+    >+    > DEBUG: [1,2,3]+    > [1,2,3]+    .+    And The Template Haskell version.+    .+    > $(dbg) [1,2,3]+    >+    > DEBUG main:Main main.hs:1:3 [1,2,3]+    > [1,2,3]+    .+    See module for a listing of all the functions with short descriptions, and the homepage for some more examples https://github.com/gregwebs/ErrorLocation.hs++Source-Repository head+  type: git+  location:    https://github.com/gregwebs/ErrorLocation.hs++Library+  Exposed-modules: FileLocation, Debug.FileLocation+  Other-modules: FileLocation.Internal+  +  -- Packages needed in order to build this package.+  Build-depends: base >= 4 && < 5,+                 template-haskell