1 {-# LANGUAGE DeriveFunctor #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
4 {-# LANGUAGE NamedFieldPuns #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE RecordWildCards #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 {-# OPTIONS_GHC -fno-warn-tabs #-}
10 module Hcompta.Expr.Log where
12 import Control.Applicative (Applicative(..))
13 import Control.Monad (Monad(..))
14 import Control.Monad.Catch (MonadThrow(..), MonadCatch(..), MonadMask(..))
15 import qualified Control.Monad.Classes as MC
16 import qualified Control.Monad.Classes.Proxied as MC
17 import qualified Control.Monad.Classes.Run as MC
18 import Control.Monad.IO.Class (MonadIO(..))
19 import Data.Function (($), (.))
20 import Data.Functor (Functor(..))
22 import Data.Monoid ((<>))
23 import Data.Ord (Ord(..))
24 import Data.String (IsString(..))
25 import Data.Text (Text)
26 import Data.Text.Buildable (Buildable(..))
27 import qualified Data.Time.LocalTime as Time
28 import Prelude (truncate)
30 import Hcompta.Lib.Data.Monoid (Monoid1)
36 { log_time :: Time.ZonedTime
37 , log_facility :: Log_Facility
40 instance Buildable x => Buildable (Log x) where
42 let Time.TimeOfDay h m s =
44 Time.zonedTimeToLocalTime log_time in
47 ":" <> int2 (truncate s::Int) <>
49 build log_facility <> ": " <>
52 int2 i = (if i < 10 then "0" else "") <> build i
54 -- ** Type 'Log_Facility'
59 instance Buildable Log_Facility where
65 ( MC.MonadWriter (Log a) m
67 ) => Log_Facility -> a -> m ()
68 log log_facility log_data = do
69 log_time <- liftIO Time.getZonedTime
78 -- | A 'Monad' transformer to handle different log data types,
79 -- eventually embedded (through class instances) into a single data type
80 -- put in the 'Monad' stack with 'MC.MonadWriter'.
81 newtype LogT w m a = LogT (MC.CustomWriterT' (Log w) m m a)
83 ( Functor, Applicative, Monoid1
84 , Monad, MonadIO, MonadThrow, MonadCatch, MonadMask)
87 :: forall w m a . (Log w -> m ())
90 evalLogWith tellFn a =
91 MC.reify tellFn $ \px ->
93 LogT (MC.CustomWriterT (MC.Proxied a')) -> a' px
95 -- ** Type 'Log_Message'
97 newtype Log_Message = Log_Message Text
98 instance Buildable Log_Message where
99 build (Log_Message x) = build x
100 instance IsString Log_Message where
101 fromString = Log_Message . fromString