{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE NoImplicitPrelude #-}module HWM.Runtime.Logging ( logRoot, logPath, log, logError, )whereimport Data.Time (getCurrentTime)import HWM.Core.Common (Name)import Reludeimport System.Directory (createDirectoryIfMissing)import qualified System.IO as TIOlogRoot :: FilePathlogRoot = ".hwm/logs"logPath :: Name -> FilePathlogPath name = logRoot <> "/" <> toString name <> ".log"log :: (MonadIO m) => Name -> [(Text, Text)] -> Text -> m FilePathlog name table content = do timestamp <- liftIO getCurrentTime let logInfo = [("TIMESTAMP", show timestamp)] let path = logPath name liftIO $ createDirectoryIfMissing True logRoot let boxTop = "┌──────────────────────────────────────────────────────────" boxBottom = "└──────────────────────────────────────────────────────────" rows = map (\(k, v) -> "│ " <> k <> ": " <> v) (table <> logInfo) header = unlines (boxTop : rows <> [boxBottom, "", content, ""]) liftIO $ TIO.appendFile path (toString header) pure pathlogError :: (MonadIO m) => Name -> [(Text, Text)] -> Text -> m FilePathlogError name table = log name (table <> [("TYPE", "ERROR")])