Skip to content

Instantly share code, notes, and snippets.

@savonarola
Created January 18, 2014 16:32
Show Gist options
  • Save savonarola/8492765 to your computer and use it in GitHub Desktop.
Save savonarola/8492765 to your computer and use it in GitHub Desktop.
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
@savonarola
Copy link
Author

20:26 savonarola@air:~/dev/logsplit>time ./splitgz_osx  mc_all8.log foo
split mc_all8.log into foo
split finished
./splitgz_osx mc_all8.log foo  207,30s user 5,32s system 99% cpu 3:34,31 total
20:30 savonarola@air:~/dev/logsplit>time ./logsplit mc_all8.log foo    
./logsplit mc_all8.log foo  89,38s user 12,37s system 98% cpu 1:43,64 total

20:37 savonarola@air:~/dev/logsplit>du -hs mc_all8.log 
6,3G    mc_all8.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment