Skip to content

Commit

Permalink
Added Utilities.Timing.logTime, bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
adamConnerSax committed May 23, 2024
1 parent 4504eeb commit eb534fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v0.10.7
* Added timing utilities

v0.10.6
* Added category based logging. For now interpreted in terms of previous logger. Adds ability to raise logging priority for a category.

Expand Down
2 changes: 1 addition & 1 deletion knit-haskell.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: knit-haskell
version: 0.10.6.0
version: 0.10.7.0
synopsis: a minimal Rmarkdown sort-of-thing for haskell, by way of Pandoc
description: knit-haskell is a beginning attempt at bringing some of the benefits of Rmarkdown to Haskell. It includes an effects stack (using <https://github.com/isovector/polysemy#readme polysemy> rather than mtl) which includes logging, a simplified interface to Pandoc and various writer-like effects to intersperse document building with regular code. Also included is a cache (in-memory and persisted to disk) to make caching results of long running computations simple. The cache provides tools for basic dependency tracking. Various helper functions are provided to simplify common operations, making it especially straightforward to build an HTML document from bits of markdown, latex and <http://hackage.haskell.org/package/lucid Lucid> or <http://hackage.haskell.org/package/blaze-html Blaze> html. Support is also included for including <http://hackage.haskell.org/package/hvega hvega> visualizations and diagrams from the <https://archives.haskell.org/projects.haskell.org/diagrams/ diagrams> package. More information is available in the <https://github.com/adamConnerSax/knit-haskell/blob/master/Readme.md readme>.
bug-reports: https://github.com/adamConnerSax/knit-haskell/issues
Expand Down
2 changes: 1 addition & 1 deletion src/Knit/Report.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ import Knit.Report.Input.RST ( addRST
import Knit.Report.Input.Visualization.Hvega
( addHvega, addHvega' )
import Knit.Report.Input.Visualization.Diagrams
import Knit.Utilities.Timing (cpuTimed, withCPUTime, logWithTime)
import Knit.Utilities.Timing (cpuTimed, withCPUTime, logWithTime, logTime)
15 changes: 7 additions & 8 deletions src/Knit/Utilities/Timing.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UnboxedTuples #-} -- This is required for the PrimMonad instance
{-# LANGUAGE UndecidableInstances #-}

module Knit.Utilities.Timing
(
cpuTimed
, withCPUTime
, logWithTime
, logTime
)
where

import Prelude hiding (error)

import qualified Polysemy as P
import Text.Printf (printf)

--import qualified Data.Text as Text
import System.CPUTime (getCPUTime)
Expand All @@ -46,3 +40,8 @@ withCPUTime withTime ma = cpuTimed ma >>= uncurry withTime
logWithTime :: P.Member (P.Embed IO) r => (Text -> P.Sem r ()) -> (a -> Double -> Text) -> P.Sem r a -> P.Sem r a
logWithTime logF logTimeMsg = withCPUTime f where
f a s = logF (logTimeMsg a s) >> pure a

-- | Given a logging function and a way to produce a message from the action result and the time,
-- produce an action which runs that function with that message after the initial action.
logTime :: P.Member (P.Embed IO) r => (Text -> P.Sem r ()) -> Text -> P.Sem r a -> P.Sem r a
logTime logF t = logWithTime logF (\_ s -> t <> " took " <> toText @String (printf "%0.3f" s))

0 comments on commit eb534fa

Please sign in to comment.