0 | ||| File-based logging actions.
 1 | module Log4Types.File
 2 |
 3 | import System.File
 4 | import Log4Types.Core
 5 |
 6 | %default total
 7 |
 8 | ||| A LogAction that writes strings to a file handle, one per line.
 9 | |||
10 | ||| File errors are silently ignored. Use this for best-effort logging
11 | ||| where a write failure should not crash the application.
12 | public export
13 | logStringHandle : HasIO io => File -> LogAction io String
14 | logStringHandle h = MkLogAction $ \msg => ignore $ fPutStrLn h msg
15 |
16 | ||| Run a computation with a LogAction writing to a file in append mode.
17 | |||
18 | ||| Opens the file, passes the log action to the callback, and closes
19 | ||| the file when done. Returns `Left` on open failure, `Right` with
20 | ||| the callback's result on success.
21 | export
22 | withLogFile : HasIO io
23 |            => String
24 |            -> (LogAction io String -> io a)
25 |            -> io (Either FileError a)
26 | withLogFile path f =
27 |   withFile path Append
28 |     (\err => pure err)
29 |     (\h => Right <$> f (logStringHandle h))
30 |