From e833b89e2a1a1c2acbc0eb8bed1760ef0e50f3c5 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 28 Apr 2017 12:50:53 -0400 Subject: log to ~/.debug-me/log/ Make watcher also log and display the name of the logfile at start and end of session. --download still downloads to current directory because that seems less surprising than downloading to elsewhere. Also, the user might want to keep a copy of the local log while downloading the server log. --- Log.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'Log.hs') diff --git a/Log.hs b/Log.hs index ecf0614..cfbffea 100644 --- a/Log.hs +++ b/Log.hs @@ -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) -- cgit v1.2.3