Compare commits
7 Commits
480073894b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
7849ddc90b
|
|||
|
d522dbaf4f
|
|||
|
a6d4703bcd
|
|||
|
a7bfc69047
|
|||
| c1c18fdd65 | |||
| a43eb4fe62 | |||
| d2dd96b709 |
69
package.yaml
69
package.yaml
@@ -1,13 +1,14 @@
|
||||
name: ris-utils
|
||||
version: 0.0.3
|
||||
homepage: "https://git.fiskhamn.se/steffenomak/ris-utils#readme"
|
||||
license: BSD3
|
||||
author: "Stefan Risberg"
|
||||
maintainer: "steffenomak@gmail.com"
|
||||
copyright: "2023 Stefan Risberg"
|
||||
---
|
||||
name: ris-utils
|
||||
version: 0.0.3
|
||||
homepage: "https://git.fiskhamn.se/steffenomak/ris-utils#readme"
|
||||
license: BSD3
|
||||
author: "Stefan Risberg"
|
||||
maintainer: "steffenomak@gmail.com"
|
||||
copyright: "2023 Stefan Risberg"
|
||||
|
||||
extra-source-files:
|
||||
- README.md
|
||||
- README.md
|
||||
|
||||
# Metadata used when publishing your package
|
||||
# synopsis: Short description of your package
|
||||
@@ -16,34 +17,46 @@ extra-source-files:
|
||||
# To avoid duplicated efforts in documentation and dealing with the
|
||||
# complications of embedding Haddock markup inside cabal files, it is
|
||||
# common to point users to the README.md file.
|
||||
description: Please see the README at <https://git.fiskhamn.se/steffenomak/ris-utils#readme>
|
||||
description: Please see the README at <https://git.fiskhamn.se/steffenomak/ris-utils#readme>
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- text == 2.*
|
||||
- effectful-core >= 2.2.2.0 && < 3
|
||||
- effectful >= 2.2.2.0 && < 3
|
||||
- effectful-th == 1.*
|
||||
- chronos >= 1.1.5 && < 2
|
||||
- torsor == 0.1
|
||||
- unliftio == 0.2.*
|
||||
- co-log-core == 0.3.*
|
||||
- bytestring == 0.11.*
|
||||
- base >= 4.7 && < 5
|
||||
- text == 2.*
|
||||
- effectful-core >= 2.2.2.0 && < 3
|
||||
- effectful >= 2.2.2.0 && < 3
|
||||
- effectful-th == 1.*
|
||||
- chronos >= 1.1.5 && < 2
|
||||
- torsor == 0.1.*
|
||||
- unliftio == 0.2.*
|
||||
- co-log-core == 0.3.*
|
||||
- bytestring == 0.12.*
|
||||
- string-conv == 0.2.*
|
||||
|
||||
ghc-options:
|
||||
- -Wall
|
||||
- -Wcompat
|
||||
- -Widentities
|
||||
- -Wincomplete-record-updates
|
||||
- -Wincomplete-uni-patterns
|
||||
- -Wmissing-export-lists
|
||||
- -Wmissing-home-modules
|
||||
- -Wpartial-fields
|
||||
- -Wredundant-constraints
|
||||
- -Wall
|
||||
- -Wcompat
|
||||
- -Widentities
|
||||
- -Wincomplete-record-updates
|
||||
- -Wincomplete-uni-patterns
|
||||
- -Wmissing-export-lists
|
||||
- -Wmissing-home-modules
|
||||
- -Wpartial-fields
|
||||
- -Wredundant-constraints
|
||||
|
||||
library:
|
||||
source-dirs: src
|
||||
|
||||
tests:
|
||||
ris-utils-test:
|
||||
main: Spec.hs
|
||||
source-dirs: tests
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- ris-utils
|
||||
|
||||
default-extensions:
|
||||
- BangPatterns
|
||||
- BlockArguments
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
cabal-version: 1.12
|
||||
|
||||
-- This file has been generated from package.yaml by hpack version 0.36.0.
|
||||
-- This file has been generated from package.yaml by hpack version 0.38.1.
|
||||
--
|
||||
-- see: https://github.com/sol/hpack
|
||||
|
||||
@@ -56,13 +56,65 @@ library
|
||||
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, bytestring ==0.11.*
|
||||
, bytestring ==0.12.*
|
||||
, chronos >=1.1.5 && <2
|
||||
, co-log-core ==0.3.*
|
||||
, effectful >=2.2.2.0 && <3
|
||||
, effectful-core >=2.2.2.0 && <3
|
||||
, effectful-th ==1.*
|
||||
, string-conv ==0.2.*
|
||||
, text ==2.*
|
||||
, torsor ==0.1
|
||||
, torsor ==0.1.*
|
||||
, unliftio ==0.2.*
|
||||
default-language: Haskell2010
|
||||
|
||||
test-suite ris-utils-test
|
||||
type: exitcode-stdio-1.0
|
||||
main-is: Spec.hs
|
||||
other-modules:
|
||||
Paths_ris_utils
|
||||
hs-source-dirs:
|
||||
tests
|
||||
default-extensions:
|
||||
BangPatterns
|
||||
BlockArguments
|
||||
ConstraintKinds
|
||||
DataKinds
|
||||
DeriveAnyClass
|
||||
DeriveGeneric
|
||||
DuplicateRecordFields
|
||||
FlexibleContexts
|
||||
FlexibleInstances
|
||||
GADTs
|
||||
LambdaCase
|
||||
MultiParamTypeClasses
|
||||
OverloadedLabels
|
||||
OverloadedStrings
|
||||
PolyKinds
|
||||
QuasiQuotes
|
||||
RankNTypes
|
||||
ScopedTypeVariables
|
||||
TemplateHaskell
|
||||
TupleSections
|
||||
TypeApplications
|
||||
TypeFamilies
|
||||
TypeOperators
|
||||
UndecidableInstances
|
||||
StandaloneDeriving
|
||||
ImportQualifiedPost
|
||||
LambdaCase
|
||||
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, bytestring ==0.12.*
|
||||
, chronos >=1.1.5 && <2
|
||||
, co-log-core ==0.3.*
|
||||
, effectful >=2.2.2.0 && <3
|
||||
, effectful-core >=2.2.2.0 && <3
|
||||
, effectful-th ==1.*
|
||||
, ris-utils
|
||||
, string-conv ==0.2.*
|
||||
, text ==2.*
|
||||
, torsor ==0.1.*
|
||||
, unliftio ==0.2.*
|
||||
default-language: Haskell2010
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
{-# LANGUAGE DerivingStrategies #-}
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
{-# LANGUAGE PatternSynonyms #-}
|
||||
|
||||
module Effects.Log (
|
||||
@@ -10,17 +12,17 @@ module Effects.Log (
|
||||
Log,
|
||||
log,
|
||||
LogData (..),
|
||||
ls,
|
||||
) where
|
||||
|
||||
import Colog.Core
|
||||
import Data.Text (Text, justifyLeft)
|
||||
import Data.String (IsString (..))
|
||||
import Data.String.Conv
|
||||
import Data.Text (Text)
|
||||
import Data.Text qualified as T
|
||||
import Data.Text.IO qualified as T
|
||||
import Data.Text.Lazy qualified as LT
|
||||
import Data.Text.Lazy.Builder qualified as T
|
||||
import Data.Text.Encoding qualified as T
|
||||
import Data.Text.Lazy.Encoding qualified as LT
|
||||
import Data.Text.Encoding.Error qualified as T
|
||||
import Effectful
|
||||
import Effectful.Concurrent.QSem
|
||||
import Effectful.Dispatch.Dynamic
|
||||
@@ -30,40 +32,22 @@ import Effects.Time
|
||||
import UnliftIO.Exception (bracket_)
|
||||
import UnliftIO.IO (stderr)
|
||||
import Prelude hiding (log)
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as LB
|
||||
|
||||
import Terminal
|
||||
|
||||
class LogData a where
|
||||
toLog :: a -> T.Builder
|
||||
|
||||
instance LogData T.Builder where
|
||||
toLog = id
|
||||
|
||||
instance LogData Text where
|
||||
toLog = T.fromText
|
||||
|
||||
instance LogData LT.Text where
|
||||
toLog = T.fromLazyText
|
||||
|
||||
instance LogData String where
|
||||
toLog = T.fromString
|
||||
|
||||
instance LogData B.ByteString where
|
||||
toLog = toLog . T.decodeUtf8With T.lenientDecode
|
||||
|
||||
instance LogData LB.ByteString where
|
||||
toLog = toLog . LT.decodeUtf8With T.lenientDecode
|
||||
newtype LogData = LogData T.Builder
|
||||
deriving newtype (IsString)
|
||||
deriving newtype (Semigroup)
|
||||
deriving newtype (Monoid)
|
||||
|
||||
data Log :: Effect where
|
||||
Log :: (LogData a) => Severity -> a -> Log m ()
|
||||
Log :: Severity -> LogData -> Log m ()
|
||||
|
||||
makeEffect ''Log
|
||||
|
||||
runLog ::
|
||||
(Time :> es, Concurrent :> es, IOE :> es) =>
|
||||
LogAction IO T.Builder ->
|
||||
LogAction IO LogData ->
|
||||
Eff (Log : es) a ->
|
||||
Eff es a
|
||||
runLog logger e = do
|
||||
@@ -73,7 +57,7 @@ runLog logger e = do
|
||||
runLogWithQsem ::
|
||||
(Time :> es, Concurrent :> es, IOE :> es) =>
|
||||
QSem ->
|
||||
LogAction IO T.Builder ->
|
||||
LogAction IO LogData ->
|
||||
Eff (Log : es) a ->
|
||||
Eff es a
|
||||
runLogWithQsem lock logger e = do
|
||||
@@ -83,29 +67,43 @@ runLogWithQsem lock logger e = do
|
||||
Log sev msg -> do
|
||||
lock' <- ask
|
||||
bracket_ (waitQSem lock') (signalQSem lock') $ do
|
||||
t <- currentTimeB
|
||||
liftIO $ cmap (addTime t . fmtMessage) logger <& toLog msg `WithSeverity` sev
|
||||
t <- LogData <$> currentTimeB
|
||||
liftIO $ cmap (addTime t . fmtMessage) logger <& msg `WithSeverity` sev
|
||||
)
|
||||
e
|
||||
|
||||
stderrLogger ::
|
||||
(MonadIO m) =>
|
||||
LogAction m T.Builder
|
||||
stderrLogger = LogAction (liftIO . T.hPutStrLn stderr . LT.toStrict . T.toLazyText)
|
||||
LogAction m LogData
|
||||
stderrLogger =
|
||||
LogAction
|
||||
( \(LogData d) ->
|
||||
liftIO . T.hPutStrLn stderr . LT.toStrict . T.toLazyText $ d
|
||||
)
|
||||
|
||||
fmtMessage :: WithSeverity T.Builder -> T.Builder
|
||||
fmtMessage (WithSeverity txt sev) = "[" <> T.fromText (justifyLeft 7 ' ' (colorSev sev)) <> "] - " <> txt
|
||||
fmtMessage :: WithSeverity LogData -> LogData
|
||||
fmtMessage (WithSeverity txt sev) =
|
||||
let sp = T.pack $ replicate (7 - length (show sev)) ' '
|
||||
in "[" <> ls (colorSev sev <> sp) <> "] - " <> txt
|
||||
where
|
||||
colorSev :: Severity -> Text
|
||||
colorSev W = colorNormal Red . tshow $ W
|
||||
colorSev I = colorNormal Green . tshow $ I
|
||||
colorSev W = colorNormal Yellow . tshow $ W
|
||||
colorSev E = colorNormal Red . tshow $ E
|
||||
colorSev a = tshow a
|
||||
|
||||
addTime ::
|
||||
T.Builder ->
|
||||
T.Builder ->
|
||||
T.Builder
|
||||
addTime t msg = (" [" :: T.Builder) <> t <> ("] " :: T.Builder) <> toLog msg
|
||||
LogData ->
|
||||
LogData ->
|
||||
LogData
|
||||
addTime t msg = " [" <> t <> "] " <> msg
|
||||
|
||||
--- Helpers
|
||||
tshow :: Show a => a -> Text
|
||||
tshow :: (Show a) => a -> Text
|
||||
tshow = T.pack . show
|
||||
|
||||
logStr :: (StringConv a Text) => a -> LogData
|
||||
logStr t = LogData (T.fromText $ toS t)
|
||||
|
||||
ls :: (StringConv a Text) => a -> LogData
|
||||
ls = logStr
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
#
|
||||
# resolver: ./custom-snapshot.yaml
|
||||
# resolver: https://example.com/snapshots/2018-01-01.yaml
|
||||
resolver: lts-21.25
|
||||
|
||||
snapshot: lts-24.11
|
||||
|
||||
# User packages to be built.
|
||||
# Various formats can be used as shown in the example below.
|
||||
@@ -30,7 +29,7 @@ resolver: lts-21.25
|
||||
# - auto-update
|
||||
# - wai
|
||||
packages:
|
||||
- .
|
||||
- .
|
||||
# Dependency packages to be pulled from upstream that are not in the resolver.
|
||||
# These entries can reference officially published versions as well as
|
||||
# forks / in-progress versions pinned to a git hash. For example:
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# This file was autogenerated by Stack.
|
||||
# You should not edit this file by hand.
|
||||
# For more information, please see the documentation at:
|
||||
# https://docs.haskellstack.org/en/stable/lock_files
|
||||
# https://docs.haskellstack.org/en/stable/topics/lock_files
|
||||
|
||||
packages: []
|
||||
snapshots:
|
||||
- completed:
|
||||
sha256: a81fb3877c4f9031e1325eb3935122e608d80715dc16b586eb11ddbff8671ecd
|
||||
size: 640086
|
||||
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/25.yaml
|
||||
original: lts-21.25
|
||||
sha256: 468e1afa06cd069e57554f10e84fdf1ac5e8893e3eefc503ef837e2449f7e60c
|
||||
size: 726310
|
||||
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/24/11.yaml
|
||||
original: lts-24.11
|
||||
|
||||
18
tests/Spec.hs
Normal file
18
tests/Spec.hs
Normal file
@@ -0,0 +1,18 @@
|
||||
import Colog.Core
|
||||
import Data.Text qualified as T
|
||||
import Data.Text.Lazy qualified as LT
|
||||
import Data.Text.Lazy.Builder qualified as T
|
||||
import Effects.Log
|
||||
|
||||
gen :: String -> Severity -> String
|
||||
gen str sev =
|
||||
let (LogData d) = fmtMessage $ ls (T.pack str) `WithSeverity` sev
|
||||
in T.unpack . LT.toStrict . T.toLazyText $ d
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn " "
|
||||
putStrLn $ gen "Test debug" D
|
||||
putStrLn $ gen "Test info" I
|
||||
putStrLn $ gen "Test warn" W
|
||||
putStrLn $ gen "Test error" E
|
||||
Reference in New Issue
Block a user