Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module LogParser where
- import Numeric ()
- data MessageType = Info
- | Warning
- | Error Int
- deriving (Show,Eq)
- type TimeStamp = Int
- data LogMessage = LogMessage MessageType TimeStamp String | Unknown String
- deriving (Show,Eq)
- parseMessage :: String -> LogMessage
- parseMessage st = case words st of
- (errIdent:errCode:timeStamp:msgWords) | Just timeStampInt <- readMaybe timeStamp , Just errCodeInt <- readMaybe errCode, errIdent == "E" ->
- LogMessage (Error errCodeInt) timeStampInt (concat msgWords)
- (msgIdent:timeStamp:msgWords) | Just timeStampInt <- readMaybe timeStamp -> case msgIdent of
- "I" -> LogMessage Info timeStampInt (concat msgWords)
- "W" -> LogMessage Warning timeStampInt (concat msgWords)
- _ -> Unknown st
- _ -> Unknown st
- readMaybe :: (Read a) => String -> Maybe a
- readMaybe s = case reads s of
- [(x,"")] -> Just x
- _ -> Nothing
Advertisement
Add Comment
Please, Sign In to add comment