Created
January 18, 2014 16:32
-
-
Save savonarola/8492765 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad | |
import qualified Data.ByteString.Lazy.Char8 as B | |
import System.Directory | |
import System.Environment | |
import System.FilePath | |
import System.IO | |
main :: IO () | |
main = do | |
args <- getArgs | |
case args of | |
logFile:outDirName:[] -> splitLogFile logFile outDirName | |
_ -> printUsage | |
printUsage :: IO () | |
printUsage = do | |
progName <- getProgName | |
putStrLn $ "Usage: " ++ progName ++ " input.log output_dir" | |
splitLogFile :: FilePath -> FilePath -> IO () | |
splitLogFile logFile outDirName = do | |
outFiles <- openOutFiles outDirName "out" | |
inputFile <- openFile logFile ReadMode | |
processLines inputFile outFiles | |
hClose inputFile | |
mapM_ hClose outFiles | |
processLines :: Handle -> [Handle] -> IO () | |
processLines inputFile outFiles = do | |
inputLines <- fmap B.lines $ B.hGetContents inputFile | |
mapM_ ((flip processLine) outFiles) inputLines | |
processLine :: B.ByteString -> [Handle] -> IO () | |
processLine line outFiles = do | |
let hour = lineHour line | |
case hour of | |
Just n -> mapM_ (B.hPut (outFiles !! n)) [line, B.pack("\n")] | |
Nothing -> putStrLn $ "Bad string: " ++ (B.unpack (B.take 20 line)) | |
lineHour :: B.ByteString -> Maybe Int | |
lineHour line = case (reads.(B.unpack).(B.take 2).(B.drop 11)) line of | |
[] -> Nothing | |
[(val, _)] -> | |
if val >= 0 && val <= 23 | |
then Just val | |
else Nothing | |
openOutFiles :: FilePath -> FilePath -> IO [Handle] | |
openOutFiles dirName fileNamePrefix = do | |
makeOutDir dirName | |
let filePaths = map (outFileName dirName fileNamePrefix) [0..23] | |
mapM ((flip openFile) WriteMode) filePaths | |
makeOutDir :: FilePath -> IO () | |
makeOutDir dirName = do | |
dirExists <- doesDirectoryExist dirName | |
when dirExists $ do removeDirectoryRecursive dirName | |
createDirectory dirName | |
outFileName :: FilePath -> FilePath -> Int -> FilePath | |
outFileName dirName fileNamePrefix = (combine dirName).((++ ".log").((fileNamePrefix ++ ".") ++)).show |
Author
savonarola
commented
Jan 18, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment