diff options
Diffstat (limited to 'Log.hs')
-rw-r--r-- | Log.hs | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -6,11 +6,16 @@ import Types import Hash import Memory import JSON +import SessionID import Data.Char import Data.Time.Clock.POSIX import qualified Data.ByteString.Lazy as L import System.IO +import System.Posix +import System.Directory +import System.FilePath +import Control.Exception -- | One item in a log of a debug-me session. -- @@ -49,6 +54,23 @@ type Timestamp = POSIXTime type Logger = AnyMessage -> IO () +logDir :: IO FilePath +logDir = do + home <- homeDirectory <$> (getUserEntryForID =<< getEffectiveUserID) + return $ home </> ".debug-me" </> "log" + +withSessionLogger :: SessionID -> (Logger -> IO a) -> IO a +withSessionLogger sessionid a = bracket setup cleanup go + where + setup = do + dir <- logDir + createDirectoryIfMissing True dir + let logfile = sessionLogFile dir sessionid + putStrLn $ "** debug-me is logging to " ++ logfile + return logfile + cleanup logfile = putStrLn $ "** debug-me session was logged to " ++ logfile + go logfile = withFile logfile WriteMode (a . mkLogger) + withLogger :: FilePath -> (Logger -> IO a) -> IO a withLogger logfile a = withFile logfile WriteMode (a . mkLogger) |