diff options
author | Joey Hess <joey@kitenet.net> | 2013-03-01 13:30:48 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-03-01 13:30:48 -0400 |
commit | ba55294acddc83bd8a0a89ff3398c1dec8374a3a (patch) | |
tree | 511c0ef50bc9a090f404348ed040d963f6458795 /Utility | |
parent | dfebe8042742db73a3029bd2d432e1074425035c (diff) |
assistant: Logs are rotated to avoid them using too much disk space.
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.
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/Daemon.hs | 11 | ||||
-rw-r--r-- | Utility/LogFile.hs | 36 |
2 files changed, 25 insertions, 22 deletions
diff --git a/Utility/Daemon.hs b/Utility/Daemon.hs index 185ea3e68..ff13a3b8a 100644 --- a/Utility/Daemon.hs +++ b/Utility/Daemon.hs @@ -8,6 +8,7 @@ module Utility.Daemon where import Common +import Utility.LogFile import System.Posix @@ -40,16 +41,6 @@ daemonize logfd pidfile changedirectory a = do out out = exitImmediately ExitSuccess -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 - {- Locks the pid file, with an exclusive, non-blocking lock. - Writes the pid to the file, fully atomically. - Fails if the pid file is already locked by another process. -} diff --git a/Utility/LogFile.hs b/Utility/LogFile.hs index 1985b7eda..1ff3006fe 100644 --- a/Utility/LogFile.hs +++ b/Utility/LogFile.hs @@ -13,22 +13,24 @@ import System.Posix openLog :: FilePath -> IO Fd openLog logfile = do - rotateLog logfile 0 + rotateLog logfile openFd logfile WriteOnly (Just stdFileMode) defaultFileFlags { append = True } -rotateLog :: FilePath -> Int -> IO () -rotateLog logfile num - | num > maxLogs = return () - | otherwise = whenM (doesFileExist currfile) $ do - rotateLog logfile (num + 1) - renameFile currfile nextfile +rotateLog :: FilePath -> IO () +rotateLog logfile = go 0 where - currfile = filename num - nextfile = filename (num + 1) - filename n - | n == 0 = logfile - | otherwise = rotatedLog logfile n + 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 @@ -40,3 +42,13 @@ listLogs logfile = filterM doesFileExist $ reverse $ 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 |