Skip to content

Instantly share code, notes, and snippets.

@joneshf
Forked from cj3kim/gist:c0db32ba27face6fe2a7
Created June 24, 2015 18:08
Show Gist options
  • Save joneshf/77ab2f0598185a284993 to your computer and use it in GitHub Desktop.
Save joneshf/77ab2f0598185a284993 to your computer and use it in GitHub Desktop.
-- CIS 194 Homework 2
module Log where
import Control.Applicative
data MessageType = Info
| Warning
| Error Int
deriving (Show, Eq)
type TimeStamp = Int
data LogMessage = LogMessage MessageType TimeStamp String
| Unknown String
deriving (Show, Eq)
data MessageTree = Leaf
| Node MessageTree LogMessage MessageTree
deriving (Show, Eq)
-- | @testParse p n f@ tests the log file parser @p@ by running it
-- on the first @n@ lines of file @f@.
testParse :: (String -> [LogMessage])
-> Int
-> FilePath
-> IO [LogMessage]
testParse parse n file = take n . parse <$> readFile file
-- | @testWhatWentWrong p w f@ tests the log file parser @p@ and
-- warning message extractor @w@ by running them on the log file
-- @f@.
testWhatWentWrong :: (String -> [LogMessage])
-> ([LogMessage] -> [String])
-> FilePath
-> IO [String]
testWhatWentWrong parse whatWentWrong file
= whatWentWrong . parse <$> readFile file
module LogAnalysis where
import Log
parseMessage :: String -> LogMessage
parseMessage s = LogMessage messageType timestamp message
where wordAry = words s
firstChar = wordAry !! 0
secondChar = wordAry !! 1
messageType = case firstChar of
"I" -> Info
"W" -> Warning
"E" -> (Error (read secondChar :: Int))
timestamp = case messageType of
(Error n) -> read (wordAry !! 2) :: Int
(Info) -> read (wordAry !! 1) :: Int
(Warning) -> read (wordAry !! 1) :: Int
message = case messageType of
(Error n) -> unwords (drop 3 wordAry)
(Info) -> unwords (drop 2 wordAry)
(Warning) -> unwords (drop 2 wordAry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment