withLogger :: (DynFlags -> DynFlags)
           -> GhcMod ()
           -> GhcMod (Either String String)
withLogger setDF body = ghandle sourceError $ do
    logref <- liftIO $ newLogRef
    wflags <- filter ("-fno-warn" `isPrefixOf`) . ghcOpts <$> options
    withDynFlags (setLogger logref . setDF) $ do
        withCmdFlags wflags $ do body *> (Right <$> readAndClearLogRef logref)
  where
    setLogger logref df = Gap.setLogAction df $ appendLogRef df logref


----------------------------------------------------------------

-- | Converting 'SourceError' to 'String'.
withLogger :: (DynFlags -> DynFlags)
           -> GhcMod ()
           -> GhcMod (Either String String)
withLogger setDF body = ghandle sourceError $ do
    logref <- liftIO $ newLogRef
    wflags <- filter ("-fno-warn" `isPrefixOf`) . ghcOpts <$> options
    withDynFlags (setLogger logref . setDF) $ do
        withCmdFlags wflags $ do body *> (Right <$> readAndClearLogRef logref)
  where
    setLogger logref df = Gap.setLogAction df $ appendLogRef df logref


----------------------------------------------------------------

-- | Converting 'SourceError' to 'String'.
withLogger setDF body = ghandle sourceError $ do
    logref <- liftIO $ newLogRef
    wflags <- filter ("-fno-warn" `isPrefixOf`) . ghcOpts <$> options
    withDynFlags (setLogger logref . setDF) $ do
        withCmdFlags wflags $ do body *> (Right <$> readAndClearLogRef logref)
  where
    setLogger logref df = Gap.setLogAction df $ appendLogRef df logref


----------------------------------------------------------------

-- | Converting 'SourceError' to 'String'.