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 ++++++++++++++++++++++ Role/Developer.hs | 2 +- Role/User.hs | 2 +- Role/Watcher.hs | 13 +++++++------ debug-me.1 | 4 ++++ 5 files changed, 35 insertions(+), 8 deletions(-) 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) diff --git a/Role/Developer.hs b/Role/Developer.hs index 9e1f40e..726a53d 100644 --- a/Role/Developer.hs +++ b/Role/Developer.hs @@ -46,7 +46,7 @@ run' runner url = do void $ runClientApp app developer :: TMVar (TVar DeveloperState) -> TMChan (Message Entered) -> TMChan AnyMessage -> SessionID -> IO () -developer dsv ichan ochan _ = withLogger "debug-me-developer.log" $ \logger -> do +developer dsv ichan ochan sid = withSessionLogger sid $ \logger -> do (devstate, startoutput) <- processSessionStart ochan logger dsv emitOutput startoutput ok <- authUser ichan ochan devstate logger diff --git a/Role/User.hs b/Role/User.hs index fe679a5..4ecb31f 100644 --- a/Role/User.hs +++ b/Role/User.hs @@ -40,7 +40,7 @@ run os = fromMaybe (ExitFailure 101) <$> connect putStrLn "Others can connect to this session and help you debug by running:" putStrLn $ " debug-me --debug " ++ url hFlush stdout - withLogger "debug-me.log" $ go ochan ichan usv + withSessionLogger sid $ go ochan ichan usv go ochan ichan usv logger = do (cmd, cmdparams) <- shellCommand os runWithPty cmd cmdparams $ \(p, ph) -> do diff --git a/Role/Watcher.hs b/Role/Watcher.hs index a4328f8..7b809f0 100644 --- a/Role/Watcher.hs +++ b/Role/Watcher.hs @@ -15,16 +15,17 @@ run :: WatchOpts -> IO () run = run' watcher . watchUrl watcher :: TMVar (TVar DeveloperState) -> TMChan (Message Entered) -> TMChan AnyMessage -> SessionID -> IO () -watcher dsv _ichan ochan _ = inRawMode $ do - (st, startoutput) <- processSessionStart ochan nullLogger dsv +watcher dsv _ichan ochan sid = withSessionLogger sid $ \logger -> inRawMode $ do + (st, startoutput) <- processSessionStart ochan logger dsv emitOutput startoutput - go st + go logger st where - go st = do + go logger st = do ts <- getPOSIXTime v <- atomically $ getServerMessage ochan st ts case v of Nothing -> return () - Just (o, _msg) -> do + Just (o, msg) -> do + _ <- logger msg emitOutput o - go st + go logger st diff --git a/debug-me.1 b/debug-me.1 index 6b332aa..22f32bc 100644 --- a/debug-me.1 +++ b/debug-me.1 @@ -79,6 +79,10 @@ Include hashes in the graphviz visualization. Run a debug-me server, logging to the specified directory. .IP "--port N" Specify a port for the debug-me server to listen to. +.SH FILES +.IP "~/.debug-me/log/" +Sessions are logged to here. The log file name is displayed when debug-me +exits. .SH SEE ALSO .SH AUTHOR -- cgit v1.2.3