Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print with line number and stack trace in Haskell? [duplicate]

I Java I appended this to my print statements and they had a stack trace...

How can we print line numbers to the log in java

public static int getLineNumber() {
    // The second row of the stack trace had the caller file name, etc.
    return Thread.currentThread().getStackTrace()[2];
}

How do I do this is Haskell?

like image 311
Michael Lafayette Avatar asked Feb 08 '16 01:02

Michael Lafayette


2 Answers

One option appears to be the use of a library like loc-th where you can, for example, write an error message with the line information:

{-# LANGUAGE TemplateHaskell #-}
-- app/Main.hs
module Main where

import Debug.Trace.LocationTH

main :: IO ()
main = do
    $failure "Error"
    putStrLn "Hello"

gives me

my-exe: app/Main.hs:10:5-12: Error

It also provides a string which one could look at, in order to determine the line number. However, I'd imagine that's a bit frowned upon, depending on your use-case. For example, I wouldn't want to see this method used to just log line numbers.

There's more on Haskell debugging techniques here.

Honestly, though, maybe this isn't the greatest idea. What are you planning on doing with the line number?

like image 187
Noon Silk Avatar answered Sep 23 '22 06:09

Noon Silk


I think I found a solution:

Debug.Trace: Functions for tracing and monitoring execution.

traceStack :: String -> a -> a Source

like trace, but additionally prints a call stack if one is available.

In the current GHC implementation, the call stack is only availble if the program was compiled with -prof; otherwise traceStack behaves exactly like trace. Entries in the call stack correspond to SCC annotations, so it is a good idea to use -fprof-auto or -fprof-auto-calls to add SCC annotations automatically.

Since: 4.5.0.0

^ https://hackage.haskell.org/package/base-4.8.2.0/docs/Debug-Trace.html

like image 34
Michael Lafayette Avatar answered Sep 25 '22 06:09

Michael Lafayette