on master help sign in
Effectful Logging.

Two logging effects with appropriate tests and documentation have been added. These effects provide: 1. A Simple logger that provides logging in effectful functions based on numerical levels of verbosity. 2. A more advanced logger that adds categorisation of logging statments in addition to levels.

commit59f7f9829f4171128e1afa442df19dc53fdf93f6 authorJan de Muijnck-Hughes <jfdm@st-andrews.ac.uk> dateSun 14 Jun 2015, 11:32 (11 years ago) parent1aa9f0b
13 files changed+238−4
idris.cabal+8 libs/effects/Effect/Logging/Default.idr+57 libs/effects/Effect/Logging/Level.idr+48 libs/effects/Effect/Logging/Simple.idr+48 libs/effects/effects.ipkg+12−4 test/effects005/effects005.idr+14 test/effects005/expected+3 test/effects005/input+10 test/effects005/run+4 test/effects006/effects006.idr+18 test/effects006/expected+2 test/effects006/input+10 test/effects006/run+4
idris.cabal+8
@@ -416,6 +416,14 @@ Extra-source-files:
416416 test/effects004/*.idr
417417 test/effects004/expected
418418 test/effects004/input
419+ test/effects005/run
420+ test/effects005/*.idr
421+ test/effects005/expected
422+ test/effects005/input
423+ test/effects006/run
424+ test/effects006/*.idr
425+ test/effects006/expected
426+ test/effects006/input
419427
420428 test/error001/run
421429 test/error001/*.idr
libs/effects/Effect/Logging/Default.idrnew file+57
@@ -0,0 +1,57 @@
1+-- ------------------------------------------------------------- [ Default.idr ]
2+-- Module : Default.idr
3+-- Copyright : (c) The Idris Community
4+-- License : see LICENSE
5+-- --------------------------------------------------------------------- [ EOH ]
6+
7+||| A logging effect that allows messages to be logged using both
8+||| numerical levels and user specified categories. The higher the
9+||| logging level the grater in verbosity the logging.
10+|||
11+||| In this effect the resource we are computing over is the logging
12+||| level itself and the list of categories to show.
13+|||
14+module Effect.Logging
15+
16+import Effects
17+import public Effect.Logging.Level
18+
19+import Control.IOExcept -- TODO Add IOExcept Logger.
20+
21+||| A Logging effect to log levels and categories.
22+data Logging : Effect where
23+ Log : (Eq a, Show a) =>
24+ (lvl : Nat)
25+ -> (cats : List a)
26+ -> (msg : String)
27+ -> Logging () (Nat,List a) (\v => (Nat,List a))
28+
29+||| The Logging effect.
30+|||
31+||| @cTy The type used to differentiate categories.
32+LOG : (cTy : Type) -> EFFECT
33+LOG a = MkEff (Nat, List a) Logging
34+
35+instance Handler Logging IO where
36+ handle (l,cs) (Log lvl cs' msg) k = do
37+ case lvl <= l of
38+ False => k () (l,cs)
39+ True => do
40+ let res = and $ map (\x => elem x cs') cs
41+ let prompt = if isNil cs then "" else show cs
42+ if res || isNil cs
43+ then do
44+ printLn $ unwords [show lvl, ":", prompt, ":", msg]
45+ k () (l,cs)
46+ else k () (l,cs)
47+
48+||| Log the given message at the given level and assign it the list of categories.
49+|||
50+||| @l The logging level.
51+||| @cs The logging categories.
52+||| @m THe message to be logged.
53+log : (Show a, Eq a) => (l : Nat)
54+ -> (cs : List a) -> (m : String) -> Eff () [LOG a]
55+log lvl cs msg = call $ Log lvl cs msg
56+
57+-- --------------------------------------------------------------------- [ EOF ]
libs/effects/Effect/Logging/Level.idrnew file+48
@@ -0,0 +1,48 @@
1+-- -------------------------------------------------------------- [ Levels.idr ]
2+-- Module : Levels.idr
3+-- Copyright : (c) Jan de Muijnck-Hughes
4+-- License : see LICENSE
5+-- --------------------------------------------------------------------- [ EOH ]
6+||| Common aliases and definitions of Logging Levels.
7+module Effect.Logging.Level
8+
9+-- ---------------------------------------------- [ Nat Derived Logging Levels ]
10+--
11+-- Several aliases have been defined to aide in semantic use of the
12+-- logging levels. These aliases have come from the Log4j family of
13+-- loggers.
14+
15+||| No events will be logged.
16+OFF : Nat
17+OFF = 0
18+
19+||| A severe error that will prevent the application from continuing.
20+FATAL : Nat
21+FATAL = 1
22+
23+||| An error in the application, possibly recoverable.
24+ERROR : Nat
25+ERROR = 2
26+
27+||| An event that might possible lead to an error.
28+WARN : Nat
29+WARN = 3
30+
31+||| An event for informational purposes.
32+INFO : Nat
33+INFO = 4
34+
35+||| A general debugging event.
36+DEBUG : Nat
37+DEBUG = 5
38+
39+||| A fine-grained debug message, typically capturing the flow through
40+||| the application.
41+TRACE : Nat
42+TRACE = 6
43+
44+||| All events should be logged.
45+ALL : Nat
46+ALL = 7
47+
48+-- --------------------------------------------------------------------- [ EOF ]
libs/effects/Effect/Logging/Simple.idrnew file+48
@@ -0,0 +1,48 @@
1+-- -------------------------------------------------------------- [ Simple.idr ]
2+-- Module : Logging.idr
3+-- Copyright : (c) The Idris Community
4+-- License : see LICENSE
5+--------------------------------------------------------------------- [ EOH ]
6+
7+||| A simple logging effect that allows messages to be logged at
8+||| different numerical level. The higher the number the grater in
9+||| verbosity the logging.
10+|||
11+||| In this effect the resource we are computing over is the logging
12+||| level itself.
13+|||
14+module Effect.Logging.Simple
15+
16+import Effects
17+import public Effect.Logging.Level
18+
19+import Control.IOExcept -- TODO Add IO Logging Handler
20+
21+||| A Logging effect that displays a logging message to be logged at a
22+||| certain level.
23+data Logging : Effect where
24+ Log : (lvl : Nat)
25+ -> (msg : String)
26+ -> Logging () Nat (\v => Nat)
27+
28+||| A Logging Effect.
29+LOG : EFFECT
30+LOG = MkEff Nat Logging
31+
32+-- For logging in the IO context
33+instance Handler Logging IO where
34+ handle l (Log lvl msg) k = do
35+ case lvl <= l of
36+ False => k () l
37+ True => do
38+ printLn $ unwords [show lvl, ":", msg]
39+ k () l
40+
41+||| Log `msg` at the given level.
42+|||
43+||| @l The level to log.
44+||| @m The message to log.
45+log : (l : Nat) -> (m : String) -> Eff () [LOG]
46+log lvl msg = call $ Log lvl msg
47+
48+-- --------------------------------------------------------------------- [ EOF ]
libs/effects/effects.ipkg+12−4
@@ -1,9 +1,17 @@
11package effects
22
33opts = "--nobasepkgs -i ../prelude -i ../base"
4−modules = Effects, Effect.Default, Effect.Monad,
54
6− Effect.Exception, Effect.File, Effect.State,
7− Effect.Random, Effect.StdIO, Effect.Select,
8− Effect.Memory, Effect.System, Effect.Trans
5+modules = Effects
6+ , Effect.Default
7+ , Effect.Monad
98
9+ , Effect.Exception
10+ , Effect.File
11+ , Effect.State
12+ , Effect.Random
13+ , Effect.StdIO
14+ , Effect.Select
15+ , Effect.Memory
16+ , Effect.System
17+ , Effect.Trans
test/effects005/effects005.idrnew file+14
@@ -0,0 +1,14 @@
1+import Effects
2+import Effect.Logging.Simple
3+
4+doubleFunc : Nat -> Eff Nat [LOG]
5+doubleFunc x = do
6+ log WARN $ unwords ["Doing the double with", show x ]
7+ pure (x+x)
8+
9+main : IO ()
10+main = do
11+ x <- runInit [ALL] (doubleFunc 3)
12+ printLn x
13+ y <- runInit [OFF] (doubleFunc 4)
14+ printLn y
test/effects005/expectednew file+3
@@ -0,0 +1,3 @@
1+"3 : Doing the double with 3"
2+6
3+8
test/effects005/inputnew file+10
@@ -0,0 +1,10 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+done
10+
test/effects005/runnew file+4
@@ -0,0 +1,4 @@
1+#!/usr/bin/env bash
2+idris $@ effects005.idr -o effects005 -p effects
3+./effects005
4+rm -f effects005 *.ibc
test/effects006/effects006.idrnew file+18
@@ -0,0 +1,18 @@
1+import Effects
2+import Effect.Logging.Default
3+
4+func : Nat -> Eff () [LOG String]
5+func x = do
6+ log WARN Nil $ unwords ["I do nothing with", show x]
7+ pure ()
8+
9+doubleFunc : Nat -> Eff Nat [LOG String]
10+doubleFunc x = do
11+ log WARN ["NumOPS"] $ unwords ["Doing the double with", show x ]
12+ func x
13+ pure (x+x)
14+
15+main : IO ()
16+main = do
17+ x <- runInit [(ALL,["NumOPS"])] (doubleFunc 3)
18+ printLn x
test/effects006/expectednew file+2
@@ -0,0 +1,2 @@
1+"3 : [\"NumOPS\"] : Doing the double with 3"
2+6
test/effects006/inputnew file+10
@@ -0,0 +1,10 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+done
10+
test/effects006/runnew file+4
@@ -0,0 +1,4 @@
1+#!/usr/bin/env bash
2+idris $@ effects006.idr -o effects006 -p effects
3+./effects006
4+rm -f effects006 *.ibc