2012-06-11 04:39:09 +00:00
|
|
|
{- log files
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.LogFile where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
import System.Posix
|
|
|
|
|
|
|
|
openLog :: FilePath -> IO Fd
|
|
|
|
openLog logfile = do
|
|
|
|
rotateLog logfile 0
|
|
|
|
openFd logfile WriteOnly (Just stdFileMode)
|
|
|
|
defaultFileFlags { append = True }
|
|
|
|
|
|
|
|
rotateLog :: FilePath -> Int -> IO ()
|
|
|
|
rotateLog logfile num
|
2013-01-15 17:52:35 +00:00
|
|
|
| num > maxLogs = return ()
|
2012-06-11 04:39:09 +00:00
|
|
|
| otherwise = whenM (doesFileExist currfile) $ do
|
|
|
|
rotateLog logfile (num + 1)
|
|
|
|
renameFile currfile nextfile
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
currfile = filename num
|
|
|
|
nextfile = filename (num + 1)
|
|
|
|
filename n
|
|
|
|
| n == 0 = logfile
|
2013-01-15 17:52:35 +00:00
|
|
|
| otherwise = rotatedLog logfile n
|
|
|
|
|
|
|
|
rotatedLog :: FilePath -> Int -> FilePath
|
|
|
|
rotatedLog logfile n = logfile ++ "." ++ show n
|
|
|
|
|
|
|
|
{- Lists most recent logs last. -}
|
|
|
|
listLogs :: FilePath -> IO [FilePath]
|
|
|
|
listLogs logfile = filterM doesFileExist $ reverse $
|
|
|
|
logfile : map (rotatedLog logfile) [1..maxLogs]
|
|
|
|
|
|
|
|
maxLogs :: Int
|
|
|
|
maxLogs = 9
|