@@ -11,13 +11,14 @@
11{-# LANGUAGE TemplateHaskell #-}
11{-# LANGUAGE TemplateHaskell #-}
12{-# LANGUAGE TypeFamilies #-}
12{-# LANGUAGE TypeFamilies #-}
13{-# LANGUAGE UndecidableInstances #-}
13{-# LANGUAGE UndecidableInstances #-}
14+-- | This module is not meant to be imported directly and may contain
15+-- internal mechanisms that will change without notice.
14module Katip.Core where
16module Katip.Core where
16-------------------------------------------------------------------------------
18-------------------------------------------------------------------------------
17import Control.Applicative as A
19import Control.Applicative as A
18import Control.AutoUpdate
20import Control.AutoUpdate
19import Control.Concurrent
21import Control.Concurrent
21import Control.Monad.Base
22import Control.Monad.Base
22import Control.Monad.Catch
23import Control.Monad.Catch
23import Control.Monad.IO.Class
24import Control.Monad.IO.Class
@@ -47,11 +48,20 @@ import Data.Time
47import GHC.Generics hiding (to)
48import GHC.Generics hiding (to)
48import Language.Haskell.TH
49import Language.Haskell.TH
49import qualified Language.Haskell.TH.Syntax as TH
50import qualified Language.Haskell.TH.Syntax as TH
50import Network.HostName
53import Network.HostName
51import System.Posix
54import System.Posix
52-------------------------------------------------------------------------------
55-------------------------------------------------------------------------------
58+readMay :: Read a => String -> Maybe a
59+readMay s = case [x | (x,t) <- reads s, ("","") <- lex t] of
61+ [] -> Nothing -- no parse
62+ _ -> Nothing -- Ambiguous parse
55-------------------------------------------------------------------------------
65-------------------------------------------------------------------------------
56-- | Represents a heirarchy of namespaces going from general to
66-- | Represents a heirarchy of namespaces going from general to
57-- specific. For instance: ["processname", "subsystem"]. Note that
67-- specific. For instance: ["processname", "subsystem"]. Note that
@@ -116,27 +126,25 @@ renderSeverity s = case s of
118-------------------------------------------------------------------------------
128-------------------------------------------------------------------------------
119−severityText :: Prism' Text Severity
129+textToSeverity :: Text -> Maybe Severity
120−severityText = prism renderSeverity toSev
122− toSev "Debug" = Right DebugS
130+textToSeverity "Debug" = Just DebugS
123− toSev "Info" = Right InfoS
131+textToSeverity "Info" = Just InfoS
124− toSev "Notice" = Right NoticeS
132+textToSeverity "Notice" = Just NoticeS
125− toSev "Warning" = Right WarningS
133+textToSeverity "Warning" = Just WarningS
126− toSev "Error" = Right ErrorS
134+textToSeverity "Error" = Just ErrorS
127− toSev "Critical" = Right CriticalS
135+textToSeverity "Critical" = Just CriticalS
128− toSev "Alert" = Right AlertS
136+textToSeverity "Alert" = Just AlertS
129− toSev "Emergency" = Right EmergencyS
137+textToSeverity "Emergency" = Just EmergencyS
130− toSev x = Left x
138+textToSeverity _ = Nothing
132−instance ToJSON Severity where
133− toJSON s = A.String (s ^. re severityText)
141+instance ToJSON Severity where
142+ toJSON s = A.String (renderSeverity s)
136instance FromJSON Severity where
144instance FromJSON Severity where
137 parseJSON = A.withText "Severity" parseSeverity
145 parseJSON = A.withText "Severity" parseSeverity
139− parseSeverity t = case t ^? severityText of
147+ parseSeverity t = case textToSeverity t of
140 Just x -> return x
148 Just x -> return x
141 Nothing -> fail $ "Invalid Severity " ++ toS t
149 Nothing -> fail $ "Invalid Severity " ++ toS t
@@ -289,13 +297,12 @@ instance FromJSON a => FromJSON (Item a) where
289 <*> (fmap getLocJs <$> o A..: "loc")
297 <*> (fmap getLocJs <$> o A..: "loc")
292−processIDText :: Prism' Text ProcessID
300+processIDToText :: ProcessID -> Text
293−processIDText = prism fromProcessID toProcessID
301+processIDToText = toS . show
295− fromProcessID = toS . show
303+
296− toProcessID t = case toS t ^? _Show of
304+textToProcessID :: Text -> Maybe ProcessID
297− Just i -> Right i
305+textToProcessID = readMay . toS
301newtype ProcessIDJs = ProcessIDJs {
308newtype ProcessIDJs = ProcessIDJs {
@@ -304,13 +311,13 @@ newtype ProcessIDJs = ProcessIDJs {
306instance ToJSON ProcessIDJs where
313instance ToJSON ProcessIDJs where
307− toJSON (ProcessIDJs p) = A.String (p ^. re processIDText)
314+ toJSON (ProcessIDJs p) = A.String (processIDToText p)
310instance FromJSON ProcessIDJs where
317instance FromJSON ProcessIDJs where
311 parseJSON = A.withText "ProcessID" parseProcessID
318 parseJSON = A.withText "ProcessID" parseProcessID
313− parseProcessID t = case t ^? processIDText of
320+ parseProcessID t = case textToProcessID t of
314 Just p -> return $ ProcessIDJs p
321 Just p -> return $ ProcessIDJs p
315 Nothing -> fail $ "Invalid ProcessIDJs " ++ toS t
322 Nothing -> fail $ "Invalid ProcessIDJs " ++ toS t
@@ -515,7 +522,7 @@ registerScribe
515 -> Scribe
522 -> Scribe
516 -> LogEnv
523 -> LogEnv
517 -> LogEnv
524 -> LogEnv
518−registerScribe nm h = logEnvScribes . at nm .~ Just h
525+registerScribe nm h = logEnvScribes %~ M.insert nm h
521-------------------------------------------------------------------------------
528-------------------------------------------------------------------------------
@@ -526,7 +533,7 @@ unregisterScribe
526 -- ^ Name of the scribe
533 -- ^ Name of the scribe
527 -> LogEnv
534 -> LogEnv
528 -> LogEnv
535 -> LogEnv
529−unregisterScribe nm = logEnvScribes . at nm .~ Nothing
536+unregisterScribe nm = logEnvScribes %~ M.delete nm
532-------------------------------------------------------------------------------
539-------------------------------------------------------------------------------