From 6c3d8c3eaf4f76180238876a10f1e03abc0e443d Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Tue, 3 Jan 2023 23:10:09 -0500 Subject: [PATCH] ENH use log file instead of stderr --- bin/xmobar.hs | 2 +- bin/xmonad.hs | 2 +- lib/Data/Internal/XIO.hs | 17 +++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/bin/xmobar.hs b/bin/xmobar.hs index ae5c4d8..39205a8 100644 --- a/bin/xmobar.hs +++ b/bin/xmobar.hs @@ -65,7 +65,7 @@ parseTest = (long "test" <> short 't' <> help "test dependencies without running") xio :: XOpts -> IO () -xio o = runXIO $ +xio o = runXIO "xmobar.log" $ case o of XDeps -> printDeps XTest -> withDBus_ evalConfig diff --git a/bin/xmonad.hs b/bin/xmonad.hs index 73193ef..b7f8b13 100644 --- a/bin/xmonad.hs +++ b/bin/xmonad.hs @@ -91,7 +91,7 @@ parseTest = (long "test" <> short 't' <> help "test dependencies without running") xio :: XOpts -> IO () -xio o = runXIO $ +xio o = runXIO "xmonad.log" $ case o of XDeps -> printDeps XTest -> undefined diff --git a/lib/Data/Internal/XIO.hs b/lib/Data/Internal/XIO.hs index 005b8c4..dedf5b7 100644 --- a/lib/Data/Internal/XIO.hs +++ b/lib/Data/Internal/XIO.hs @@ -118,7 +118,7 @@ import qualified RIO.Text as T import System.Posix.Files import System.Process.Typed (nullStream) import UnliftIO.Environment -import XMonad.Core (X, io) +import XMonad.Core (X, dataDir, getDirectories, io) import XMonad.Internal.IO import XMonad.Internal.Shell hiding (proc, runProcess) import XMonad.Internal.Theme @@ -131,9 +131,18 @@ import XMonad.Internal.Theme -- | Run feature evaluation(s) with the cache -- Currently there is no easy way to not use this (oh well) -runXIO :: XIO a -> IO a -runXIO x = do - logOpts <- setLogVerboseFormat True . setLogUseTime True <$> logOptionsHandle stderr False +runXIO :: FilePath -> XIO a -> IO a +runXIO logfile x = do + -- TODO this directory will not exist on a fresh system + p <- ( logfile) . dataDir <$> getDirectories + catchIO (withFile p AppendMode $ runXIOInner x) $ \e -> do + print e + putStrLn "could not open log file, falling back to stderr" + runXIOInner x stderr + +runXIOInner :: XIO a -> Handle -> IO a +runXIOInner x h = do + logOpts <- setLogVerboseFormat True . setLogUseTime True <$> logOptionsHandle h False pc <- mkDefaultProcessContext withLogFunc logOpts $ \f -> do p <- getParams