@@ -1,88 +1,29 @@
1−-- ------------------------------------------------------------- [ Logging.idr ]
1+-- ------------------------------------------------------------ [ Category.idr ]
2−-- Module : Logging.idr
2+-- Module : Category.idr
3-- Copyright : (c) The Idris Community
3-- Copyright : (c) The Idris Community
4-- License : see LICENSE
4-- License : see LICENSE
5-- --------------------------------------------------------------------- [ EOH ]
5-- --------------------------------------------------------------------- [ EOH ]
7||| A logging effect that allows messages to be logged using both
6||| A logging effect that allows messages to be logged using both
8||| numerical levels and user specified categories. The higher the
7||| numerical levels and user specified categories. The higher the
9||| logging level the grater in verbosity the logging.
8||| logging level the grater in verbosity the logging.
11||| In this effect the resource we are computing over is the logging
10||| In this effect the resource we are computing over is the logging
12||| level itself and the list of categories to show.
11||| level itself and the list of categories to show.
14−module Effect.Logging
12+module Effect.Logging.Category
16import Effects
14import Effects
15+import public Effect.Logging.Level
18%access public
17%access public
20−-- ---------------------------------------------------- [ Log Level Definition ]
19+-- -------------------------------------------------------- [ Logging Resource ]
22−||| Logging levels are natural numbers wrapped in a data type for
25−||| Several aliases have been defined to aide in semantic use of the
26−||| logging levels. These aliases have come from the Log4j/Python
27−||| family of loggers.
28−data LogLevel : Nat -> Type where
32− ||| A fine-grained debug message, typically capturing the flow through
36− ||| A general debugging event.
39− ||| An event for informational purposes.
42− ||| An event that might possible lead to an error.
45− ||| An error in the application, possibly recoverable.
48− ||| A severe error that will prevent the application from continuing.
51− ||| All events should be logged.
54− ||| User defined logging level.
55− CUSTOM : (n : Nat) -> {auto prf : LTE n 70} -> LogLevel n
57−instance Cast (LogLevel n) Nat where
60−instance Show (LogLevel n) where
69− show (CUSTOM n) = unwords ["CUSTOM", show n]
71−instance Eq (LogLevel n) where
74− lvlEq : LogLevel a -> LogLevel b -> Bool
75− lvlEq {a} {b} _ _ = a == b
77−cmpLevel : LogLevel a -> LogLevel b -> Ordering
78−cmpLevel {a} {b} _ _ = compare a b
80||| The Logging details, this is the resource that the effect is
21||| The Logging details, this is the resource that the effect is
81||| defined over.
22||| defined over.
82record LogRes (a : Type) where
23record LogRes (a : Type) where
83 constructor MkLogRes
24 constructor MkLogRes
84− lvl : LogLevel n
25+ getLevel : LogLevel n
85− cats : List a
26+ getCategories : List a
87instance (Show a) => Show (LogRes a) where
28instance (Show a) => Show (LogRes a) where
88 show (MkLogRes l cs) = unwords ["Log Settings:", show l, show cs]
29 show (MkLogRes l cs) = unwords ["Log Settings:", show l, show cs]
@@ -90,6 +31,8 @@ instance (Show a) => Show (LogRes a) where
90instance Default (LogRes a) where
31instance Default (LogRes a) where
91 default = MkLogRes OFF Nil
32 default = MkLogRes OFF Nil
34+-- ------------------------------------------------------- [ Effect Definition ]
93||| A Logging effect to log levels and categories.
36||| A Logging effect to log levels and categories.
94data Logging : Effect where
37data Logging : Effect where
95 ||| Log a message.
38 ||| Log a message.
@@ -126,12 +69,14 @@ data Logging : Effect where
126 (ncats : List a) ->
69 (ncats : List a) ->
127 sig Logging () (LogRes a) (LogRes a)
70 sig Logging () (LogRes a) (LogRes a)
72+-- -------------------------------------------------------------- [ IO Handler ]
129instance Handler Logging IO where
74instance Handler Logging IO where
130 handle st (SetLogLvl nlvl) k = do
75 handle st (SetLogLvl nlvl) k = do
131− let newSt = record {lvl = nlvl} st
76+ let newSt = record {getLevel = nlvl} st
132 k () newSt
77 k () newSt
133 handle st (SetLogCats newcs) k = do
78 handle st (SetLogCats newcs) k = do
134− let newSt = record {cats = newcs} st
79+ let newSt = record {getCategories = newcs} st
135 k () newSt
80 k () newSt
137 handle st (InitLogger l cs) k = do
82 handle st (InitLogger l cs) k = do
@@ -139,25 +84,30 @@ instance Handler Logging IO where
139 k () newSt
84 k () newSt
141 handle st (Log l cs' msg) k = do
86 handle st (Log l cs' msg) k = do
142− case cmpLevel l (lvl st) of
87+ case cmpLevel l (getLevel st) of
143 GT => k () st
88 GT => k () st
144 otherwise => do
89 otherwise => do
145− let res = and $ map (\x => elem x cs') (cats st)
90+ let res = and $ map (\x => elem x cs') (getCategories st)
146− let prompt = if isNil (cats st)
91+ let prompt = if isNil (getCategories st)
147 then unwords [show l, ":"]
92 then unwords [show l, ":"]
148− else unwords [show l, ":", show (cats st), ":"]
93+ else unwords [show l, ":", show (getCategories st), ":"]
149− if res || isNil (cats st)
94+ if res || isNil (getCategories st)
151 putStrLn $ unwords [prompt, msg]
96 putStrLn $ unwords [prompt, msg]
153 else k () st
98 else k () st
101+-- ------------------------------------------------------- [ Effect Descriptor ]
155||| The Logging effect.
103||| The Logging effect.
157||| @cTy The type used to differentiate categories.
105||| @cTy The type used to differentiate categories.
158LOG : (cTy : Type) -> EFFECT
106LOG : (cTy : Type) -> EFFECT
159LOG cTy = MkEff (LogRes cTy) Logging
107LOG cTy = MkEff (LogRes cTy) Logging
109+-- ----------------------------------------------------------- [ Effectful API ]
161||| Change the logging level.
111||| Change the logging level.
163||| @l The new logging level.
113||| @l The new logging level.