1865b28094
This cannot completely guard against a runaway log event, and only runs every hour anyway, but it should avoid most problems with very long-running, active assistants using up too much space.
54 lines
1.1 KiB
Haskell
54 lines
1.1 KiB
Haskell
{- 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
|
|
openFd logfile WriteOnly (Just stdFileMode)
|
|
defaultFileFlags { append = True }
|
|
|
|
rotateLog :: FilePath -> IO ()
|
|
rotateLog logfile = go 0
|
|
where
|
|
go num
|
|
| num > maxLogs = return ()
|
|
| otherwise = whenM (doesFileExist currfile) $ do
|
|
go (num + 1)
|
|
renameFile currfile nextfile
|
|
where
|
|
currfile = filename num
|
|
nextfile = filename (num + 1)
|
|
filename n
|
|
| n == 0 = logfile
|
|
| 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
|
|
|
|
redirLog :: Fd -> IO ()
|
|
redirLog logfd = do
|
|
mapM_ (redir logfd) [stdOutput, stdError]
|
|
closeFd logfd
|
|
|
|
redir :: Fd -> Fd -> IO ()
|
|
redir newh h = do
|
|
closeFd h
|
|
void $ dupTo newh h
|