0 | module Core.Context.Log
2 | import public Core.Context
6 | import Libraries.Data.StringMap
15 | logString : String -> Nat -> String -> Core ()
16 | logString "" n msg = coreLift $
putStrLn
17 | $
"LOG " ++ show n ++ ": " ++ msg
18 | logString str n msg = coreLift $
putStrLn
19 | $
"LOG " ++ str ++ ":" ++ show n ++ ": " ++ msg
23 | logString' : LogLevel -> String -> Core ()
25 | logString (fastConcat (intersperse "." (topics lvl)) ++ ":")
29 | logging' : {auto c : Ref Ctxt Defs} ->
30 | LogLevel -> Core Bool
33 | pure $
verbosity lvl == 0 || (logEnabled opts && keepLog lvl (logLevel opts))
36 | unverifiedLogging : {auto c : Ref Ctxt Defs} ->
37 | String -> Nat -> Core Bool
38 | unverifiedLogging str Z = pure True
39 | unverifiedLogging str n = do
41 | pure $
logEnabled opts && keepLog (mkUnverifiedLogLevel str n) (logLevel opts)
45 | logging : {auto c : Ref Ctxt Defs} ->
46 | LogTopic -> Nat -> Core Bool
47 | logging s n = unverifiedLogging s.topic n
51 | logTerm : {vars : _} ->
52 | {auto c : Ref Ctxt Defs} ->
53 | LogTopic -> Nat -> Lazy String -> Term vars -> Core ()
55 | = when !(logging s n)
56 | $
do tm' <- toFullNames tm
57 | logString s.topic n $
msg ++ ": " ++ show tm'
60 | log' : {auto c : Ref Ctxt Defs} ->
61 | LogLevel -> Lazy String -> Core ()
63 | = when !(logging' lvl)
64 | $
logString' lvl msg
75 | log : {auto c : Ref Ctxt Defs} ->
76 | LogTopic -> Nat -> Lazy String -> Core ()
78 | = when !(logging s n)
79 | $
logString s.topic n msg
82 | unverifiedLogC : {auto c : Ref Ctxt Defs} ->
83 | String -> Nat -> Core String -> Core ()
84 | unverifiedLogC str n cmsg
85 | = when !(unverifiedLogging str n)
91 | logC : {auto c : Ref Ctxt Defs} ->
92 | LogTopic -> Nat -> Core String -> Core ()
93 | logC s = unverifiedLogC s.topic
102 | logTimeOver : Integer -> Core String -> Core a -> Core a
103 | logTimeOver nsecs str act
104 | = do clock <- coreLift (clockTime Process)
105 | let t = seconds clock * nano + nanoseconds clock
107 | clock <- coreLift (clockTime Process)
108 | let t' = seconds clock * nano + nanoseconds clock
110 | when (time > nsecs) $
113 | coreLift $
putStrLn $
"TIMING " ++ str' ++ ": " ++
114 | show (time `div` nano) ++ "." ++
115 | addZeros (unpack (show ((time `mod` nano) `div` micro))) ++
119 | addZeros : List Char -> String
120 | addZeros [] = "000"
121 | addZeros [x] = "00" ++ cast x
122 | addZeros [x,y] = "0" ++ cast x ++ cast y
123 | addZeros str = pack str
126 | logTimeWhen : {auto c : Ref Ctxt Defs} ->
127 | Bool -> Nat -> Lazy String -> Core a -> Core a
128 | logTimeWhen p lvl str act
130 | then do clock <- coreLift (clockTime Process)
131 | let t = seconds clock * nano + nanoseconds clock
133 | clock <- coreLift (clockTime Process)
134 | let t' = seconds clock * nano + nanoseconds clock
138 | let header = "TIMING " ++ replicate lvl '+' ++ ifThenElse (0 < lvl) " " ""
139 | putStrLn $
header ++ str ++ ": " ++
140 | show (time `div` nano) ++ "." ++
141 | addZeros (unpack (show ((time `mod` nano) `div` micro))) ++
146 | addZeros : List Char -> String
147 | addZeros [] = "000"
148 | addZeros [x] = "00" ++ cast x
149 | addZeros [x,y] = "0" ++ cast x ++ cast y
150 | addZeros str = pack str
152 | logTimeRecord' : {auto c : Ref Ctxt Defs} ->
153 | String -> Core a -> Core a
154 | logTimeRecord' key act
155 | = do clock <- coreLift (clockTime Process)
156 | let t = seconds clock * nano + nanoseconds clock
158 | clock <- coreLift (clockTime Process)
159 | let t' = seconds clock * nano + nanoseconds clock
162 | let tot = case lookup key (timings defs) of
165 | put Ctxt ({ timings $= insert key (False, tot + time) } defs)
171 | logTimeRecord : {auto c : Ref Ctxt Defs} ->
172 | String -> Core a -> Core a
173 | logTimeRecord key act
174 | = do defs <- get Ctxt
176 | case lookup key (timings defs) of
177 | Just (True, t) => act
179 | => do put Ctxt ({ timings $= insert key (True, t) } defs)
180 | logTimeRecord' key act
182 | => logTimeRecord' key act
185 | showTimeRecord : {auto c : Ref Ctxt Defs} ->
188 | = do defs <- get Ctxt
189 | traverse_ showTimeLog (toList (timings defs))
191 | addZeros : List Char -> String
192 | addZeros [] = "000"
193 | addZeros [x] = "00" ++ cast x
194 | addZeros [x,y] = "0" ++ cast x ++ cast y
195 | addZeros str = pack str
197 | showTimeLog : (String, (Bool, Integer)) -> Core ()
198 | showTimeLog (key, (_, time))
199 | = do coreLift $
putStr (key ++ ": ")
201 | coreLift $
putStrLn $
show (time `div` nano) ++ "." ++
202 | addZeros (unpack (show ((time `mod` nano) `div` micro))) ++
206 | logTime : {auto c : Ref Ctxt Defs} ->
207 | Nat -> Lazy String -> Core a -> Core a
208 | logTime lvl str act
209 | = do opts <- getSession
210 | logTimeWhen (maybe False (lvl <=) (logTimings opts)) lvl str act