1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
module CPM.ErrorLogger
( ErrorLogger
, LogEntry
, LogLevel (..)
, logLevelOf
, levelGte
, getLogLevel, setLogLevel
, setWithShowTime
, (|>=), (|>), (|->), (|>>)
, mapEL
, foldEL
, succeedIO
, failIO
, log
, showLogEntry
, infoMessage, debugMessage, fromErrorLogger
, showExecCmd, execQuietCmd
) where
import Global
import Pretty
import Profile
import System (exitWith, system)
infixl 0 |>=, |>, |>>, |->
type ErrorLogger a = ([LogEntry], Either LogEntry a)
data LogEntry = LogEntry LogLevel String
logLevelOf :: LogEntry -> LogLevel
logLevelOf (LogEntry ll _) = ll
data LogLevel = Quiet
| Info
| Debug
| Error
| Critical
logLevel :: Global LogLevel
logLevel = global Info Temporary
getLogLevel :: IO LogLevel
getLogLevel = readGlobal logLevel
setLogLevel :: LogLevel -> IO ()
setLogLevel level = writeGlobal logLevel level
withShowTime :: Global Bool
withShowTime = global False Temporary
getWithShowTime :: IO Bool
getWithShowTime = readGlobal withShowTime
setWithShowTime :: Bool -> IO ()
setWithShowTime wst = writeGlobal withShowTime wst
(|>=) :: IO (ErrorLogger a) -> (a -> IO (ErrorLogger b)) -> IO (ErrorLogger b)
a |>= f = do
(msgs, err) <- a
mapIO showLogEntry msgs
case err of
Right v -> do
(msgs', err') <- f v
return $ (msgs', err')
Left m -> return $ ([], Left m)
(|>) :: IO (ErrorLogger a) -> IO (ErrorLogger b) -> IO (ErrorLogger b)
a |> f = do
(msgs, err) <- a
mapIO showLogEntry msgs
case err of
Right _ -> do
(msgs', err') <- f
return $ (msgs', err')
Left m -> return $ ([], Left m)
(|->) :: IO (ErrorLogger a) -> IO (ErrorLogger b) -> IO (ErrorLogger a)
a |-> b = do
(msgs, err) <- a
mapIO showLogEntry msgs
case err of
Right _ -> do
(msgs', _) <- b
return $ (msgs', err)
Left m -> return $ ([], Left m)
(|>>) :: IO a -> IO (ErrorLogger b) -> IO (ErrorLogger b)
a |>> b = (a >> succeedIO ()) |> b
mapEL :: (a -> IO (ErrorLogger b)) -> [a] -> IO (ErrorLogger [b])
mapEL _ [] = succeedIO []
mapEL f (x:xs) = do
(msgs, err) <- f x
mapIO showLogEntry msgs
case err of
Right v -> do
(msgs', xs') <- mapEL f xs
case xs' of
Right xs'' -> succeedIO (v:xs'')
Left m' -> return $ (msgs', Left m')
Left m -> return $ ([], Left m)
foldEL :: (a -> b -> IO (ErrorLogger a)) -> a -> [b] -> IO (ErrorLogger a)
foldEL _ z [] = succeedIO z
foldEL f z (x:xs) = do
(msgs, err) <- f z x
mapIO showLogEntry msgs
case err of
Right v -> foldEL f v xs
Left m -> return $ ([], Left m)
showLogEntry :: LogEntry -> IO ()
showLogEntry (LogEntry lvl msg) = do
minLevel <- getLogLevel
if levelGte lvl minLevel
then putStrLn $ pPrint $ lvlText <+> (text msg)
else return ()
where
lvlText = case lvl of
Quiet -> text "QUIET "
Info -> text "INFO "
Debug -> green $ text "DEBUG "
Critical -> red $ text "CRITICAL "
Error -> red $ text "ERROR "
levelGte :: LogLevel -> LogLevel -> Bool
levelGte Debug Debug = True
levelGte Debug Quiet = False
levelGte Debug Info = False
levelGte Debug Error = False
levelGte Debug Critical = False
levelGte Info Debug = True
levelGte Info Info = True
levelGte Info Quiet = False
levelGte Info Error = False
levelGte Info Critical = False
levelGte Quiet Debug = True
levelGte Quiet Quiet = True
levelGte Quiet Info = False
levelGte Quiet Error = False
levelGte Quiet Critical = False
levelGte Error Debug = True
levelGte Error Info = True
levelGte Error Quiet = True
levelGte Error Error = True
levelGte Error Critical = True
levelGte Critical Debug = True
levelGte Critical Info = True
levelGte Critical Quiet = True
levelGte Critical Error = True
levelGte Critical Critical = True
succeed :: a -> ErrorLogger a
succeed v = ([], Right v)
succeedIO :: a -> IO (ErrorLogger a)
succeedIO v = return $ succeed v
fail :: String -> ErrorLogger a
fail msg = ([logMsg], Left logMsg) where logMsg = LogEntry Critical msg
failIO :: String -> IO (ErrorLogger a)
failIO msg = return $ fail msg
log :: LogLevel -> String -> IO (ErrorLogger ())
log lvl msg = do
wst <- getWithShowTime
if wst
then do
runtime <- getProcessInfos >>= return . maybe 0 id . lookup ElapsedTime
return $ ([LogEntry lvl (showTime runtime ++ 's':' ':msg)], Right ())
else
return $ ([LogEntry lvl msg], Right ())
where
showTime t = show (t `div` 1000) ++ "." ++ show ((t `mod` 1000) `div` 10)
infoMessage :: String -> IO ()
infoMessage msg = (log Info msg |> succeedIO ()) >> done
debugMessage :: String -> IO ()
debugMessage msg = (log Debug msg |> succeedIO ()) >> done
fromErrorLogger :: IO (ErrorLogger a) -> IO a
fromErrorLogger a = do
(msgs, err) <- a
mapIO showLogEntry msgs
case err of
Right v -> return v
Left m -> showLogEntry m >> exitWith 1
showExecCmd :: String -> IO Int
showExecCmd cmd = debugMessage ("Executing: " ++ cmd) >> system cmd
execQuietCmd :: (String -> String) -> IO Int
execQuietCmd cmd = do
ll <- getLogLevel
debugMessage $ "Executing: " ++ cmd ""
system $ cmd (if ll == Debug then "" else "-q")
|