git-annex/Utility/LogFile.hs

60 lines
1.1 KiB
Haskell
Raw Normal View History

2012-06-11 04:39:09 +00:00
{- log files
-
- Copyright 2012 Joey Hess <id@joeyh.name>
2012-06-11 04:39:09 +00:00
-
- License: BSD-2-clause
2012-06-11 04:39:09 +00:00
-}
2013-05-10 21:29:59 +00:00
{-# LANGUAGE CPP #-}
2012-06-11 04:39:09 +00:00
module Utility.LogFile where
import Common
#ifndef mingw32_HOST_OS
import System.Posix.Types
#endif
2012-06-11 04:39:09 +00:00
openLog :: FilePath -> IO Handle
2012-06-11 04:39:09 +00:00
openLog logfile = do
rotateLog logfile
openFile logfile AppendMode
2012-06-11 04:39:09 +00:00
rotateLog :: FilePath -> IO ()
rotateLog logfile = go 0
2012-12-13 04:24:19 +00:00
where
go num
| num > maxLogs = return ()
| otherwise = whenM (doesFileExist currfile) $ do
go (num + 1)
rename 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
#ifndef mingw32_HOST_OS
2014-02-11 17:18:59 +00:00
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
#endif