From 14bd71dadd11153b57697b1d8b4fa69e5da1a05c Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 28 May 2023 11:44:48 +0100 Subject: Add better logging --- org-zk.cabal | 1 + src/Main.hs | 21 ++++++++++++++++----- src/Zettel/Render.hs | 21 +++++++++++---------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/org-zk.cabal b/org-zk.cabal index 5170bd1..4165e10 100644 --- a/org-zk.cabal +++ b/org-zk.cabal @@ -35,6 +35,7 @@ executable org-zk , with-utf8 , directory , filepath + , logging mixins: base hiding (Prelude), diff --git a/src/Main.hs b/src/Main.hs index c819300..9619f7a 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -25,6 +25,8 @@ import Control.Exception (ioError) import System.Console.GetOpt import System.IO.Error (userError) import Zettel +import Control.Logging +import qualified Data.Text as T -- data HeaderState = HeaderState -- { headerStateMap :: Map Text ((ZettelId, Text), [Text]), @@ -211,7 +213,7 @@ data Options = Options optCsl :: Maybe FilePath, optIndex :: Maybe FilePath, optOutput :: Maybe FilePath, - optVerbose :: Bool, + optVerbose :: Int, optQuiet :: Bool, optShowVersion :: Bool, optShowHelp :: Bool @@ -226,7 +228,7 @@ defaultOptions = optCsl = Nothing, optIndex = Nothing, optOutput = Nothing, - optVerbose = False, + optVerbose = 0, optQuiet = False, optShowVersion = False, optShowHelp = False @@ -272,7 +274,7 @@ options = Option ['v'] ["verbose"] - (NoArg (\opts -> opts {optVerbose = True})) + (NoArg (\opts -> opts {optVerbose = optVerbose opts + 1})) "Verbose output while processing files", Option ['V', '?'] @@ -294,9 +296,16 @@ compilerOpts argv = (_, _, errs) -> ioError (userError (concat errs ++ usageInfo headerOpts options)) main :: IO () -main = do +main = withStderrLogging $ do (zkOpts, fl) <- getArgs >>= compilerOpts + setLogLevel LevelWarn + when (optVerbose zkOpts == 1) $ setLogLevel LevelInfo + when (optVerbose zkOpts > 1) $ setLogLevel LevelDebug + when (optQuiet zkOpts) $ setLogLevel LevelError + + debug $ "Options: " <> T.pack (show zkOpts) + when (optShowHelp zkOpts) $ do putStrLn $ usageInfo headerOpts options exitSuccess @@ -309,8 +318,10 @@ main = do let pipeline = transcludeMdAll >> linkAll >> wrapZettelGraph >> handleBibliography + when (isNothing (optCsl zkOpts)) . warn $ "CSL file not set, using: ieee.csl" + when (isNothing (optBibliography zkOpts)) . warn $ "Bibliography file not set, using: references.bib" + renderZettelGraphFile - (optVerbose zkOpts) ( fromMaybe "ieee.csl" (optCsl zkOpts) diff --git a/src/Zettel/Render.hs b/src/Zettel/Render.hs index 662742d..8f9fbe5 100644 --- a/src/Zettel/Render.hs +++ b/src/Zettel/Render.hs @@ -19,6 +19,7 @@ import Text.Pandoc.Templates (WithDefaultPartials (..), compileTemplate) import Text.Pandoc.Walk (query) import Text.Pandoc.Writers (writeMarkdown) import Zettel.Types +import Control.Logging zettelIdToLink :: ZettelId -> Inline zettelIdToLink ident = Link mempty [Str $ "#" <> unZettelId ident] ("/" <> unZettelId ident, "") @@ -85,27 +86,27 @@ renderZettel csl bib _ zettel = do >>= writeMarkdown writeOpts else writeMarkdown writeOpts zettel.zettelBody -renderZettelFile :: Bool -> FilePath -> Maybe FilePath -> FilePath -> ZettelId -> Zettel -> IO () -renderZettelFile v csl bib dir ident zettel = do - when v . putStrLn $ "Writing " <> path +renderZettelFile :: FilePath -> Maybe FilePath -> FilePath -> ZettelId -> Zettel -> IO () +renderZettelFile csl bib dir ident zettel = do + log $ "Writing " <> T.pack path t <- renderZettel csl bib ident zettel writeFileText path t where path = dir "zettel" toString (unZettelId ident) <.> "md" -renderBibFile :: Bool -> FilePath -> Maybe FilePath -> FilePath -> BibId -> Zettel -> IO () -renderBibFile v csl bib dir (BibId ident) zettel = do - when v . putStrLn $ "Writing " <> path +renderBibFile :: FilePath -> Maybe FilePath -> FilePath -> BibId -> Zettel -> IO () +renderBibFile csl bib dir (BibId ident) zettel = do + log $ "Writing " <> T.pack path t <- renderZettel csl bib (ZettelId ident) zettel writeFileText path t where path = dir "bib" toString ident <.> "md" -renderZettelGraphFile :: Bool -> FilePath -> Maybe FilePath -> FilePath -> ZettelGraph -> IO () -renderZettelGraphFile v csl bib fp zg = +renderZettelGraphFile :: FilePath -> Maybe FilePath -> FilePath -> ZettelGraph -> IO () +renderZettelGraphFile csl bib fp zg = forM_ (Map.assocs (unZettelGraph zg)) - (uncurry (renderZettelFile v csl bib fp)) + (uncurry (renderZettelFile csl bib fp)) >> forM_ (Map.assocs (zettelGraphBib zg)) - (uncurry (renderBibFile v csl bib fp)) + (uncurry (renderBibFile csl bib fp)) -- cgit