Skip to content

Commit

Permalink
no systemd on macos (or any other non-linux os)
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
danbornside authored and matthewbauer committed Jan 16, 2019
1 parent 73723af commit 0fc22c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 22 additions & 2 deletions backend/Rhyolite/Backend/Logging.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
Expand Down Expand Up @@ -41,7 +42,9 @@ module Rhyolite.Backend.Logging
, RhyoliteLogLevel(..)
, RhyoliteLogAppenderStderr (..)
, RhyoliteLogAppenderFile (..)
#ifdef linux_HOST_OS
, RhyoliteLogAppenderJournald (..)
#endif
, example
) where

Expand All @@ -63,7 +66,11 @@ import qualified Data.Text.Encoding as TE
import qualified Data.Trie.BigEndianPatricia.Base as Trie
import GHC.Generics
import System.Log.FastLogger

#ifdef linux_HOST_OS
import Systemd.Journal
#endif

import Data.Default

newtype LoggingEnv = LoggingEnv { unLoggingEnv :: Loc -> LogSource -> LogLevel -> LogStr -> IO () }
Expand Down Expand Up @@ -105,7 +112,9 @@ data LoggingContext m = LoggingContext
data RhyoliteLogAppender
= RhyoliteLogAppender_Stderr RhyoliteLogAppenderStderr
| RhyoliteLogAppender_File RhyoliteLogAppenderFile
| RhyoliteLogAppender_Journald RhyoliteLogAppenderJournald
#ifdef linux_HOST_OS
| RhyoliteLogAppender_Journald RhyoliteLogAppenderJournald -- journalctl log with syslogIdentifier specified.
#endif
deriving (Generic, Eq, Ord, Show)

instance Default RhyoliteLogAppender where
Expand All @@ -122,9 +131,11 @@ data RhyoliteLogAppenderFile = RhyoliteLogAppenderFile
{ _rhyoliteLogAppenderFile_file :: !FilePath
} deriving (Generic, Eq, Ord, Show)

#ifdef linux_HOST_OS
data RhyoliteLogAppenderJournald = RhyoliteLogAppenderJournald
{ _rhyoliteLogAppenderJournald_syslogIdentifier :: T.Text -- journalctl log with syslogIdentifier specified.
} deriving (Generic, Eq, Ord, Show)
#endif

-- derive a little differently from `Rhyolite.Request.makeJson` so that more
-- things end up as records, so that new fields don't break old configs
Expand All @@ -138,7 +149,9 @@ fmap concat $ traverse (deriveJSON defaultOptions
, ''RhyoliteLogAppender
, ''RhyoliteLogAppenderStderr
, ''RhyoliteLogAppenderFile
#ifdef linux_HOST_OS
, ''RhyoliteLogAppenderJournald
#endif
, ''LoggingConfig
]

Expand All @@ -148,6 +161,7 @@ runLoggingEnv = flip runLoggingT . unLoggingEnv
logToFastLogger :: LoggerSet -> LoggingEnv
logToFastLogger ls = LoggingEnv $ \_loc logSource logLevel logStr -> pushLogStrLn ls (toLogStr (show logLevel) <> toLogStr (show logSource) <> logStr)

#ifdef linux_HOST_OS
logger2journald :: LogLevel -> JournalFields
logger2journald = \case
LevelWarn -> priority Warning
Expand All @@ -156,6 +170,7 @@ logger2journald = \case
LevelError -> priority Error
LevelOther level -> priority Error -- Error because that makes this logger2journald monotone
<> mkJournalField' "PRIORITY_OTHER" level
#endif

class LogAppender a where
getLogContext :: MonadIO m => a -> m (LoggingContext m)
Expand All @@ -168,14 +183,16 @@ instance LogAppender RhyoliteLogAppender where
RhyoliteLogAppender_File (RhyoliteLogAppenderFile filename) -> do
logSet <- liftIO $ newFileLoggerSet defaultBufSize filename
return $ LoggingContext (liftIO (rmLoggerSet logSet)) (logToFastLogger logSet)
#ifdef linux_HOST_OS
RhyoliteLogAppender_Journald cfg -> return $ LoggingContext (return ()) (logToJournalCtl (syslogIdentifier $ _rhyoliteLogAppenderJournald_syslogIdentifier cfg))

#endif

configLogger :: (LogAppender a, MonadIO m) => LoggingConfig a -> m (LoggingContext m)
configLogger (LoggingConfig ls fs) = do
LoggingContext cleaner logger <- getLogContext ls
return $ LoggingContext cleaner $ filterLog (fmap toLogLevel $ maybe M.empty id fs) logger

#ifdef linux_HOST_OS
mkJournalField' :: T.Text -> T.Text -> JournalFields
mkJournalField' k v = HashMap.singleton (mkJournalField k) (TE.encodeUtf8 v)

Expand All @@ -193,6 +210,7 @@ logToJournalCtl syslogId = LoggingEnv $ \loc logSource logLevel logStr -> sendMe
, loc2jf loc
, mkJournalField' "logsource" logSource
]
#endif

-- | match the LogSource on prefixes in m. if found, log only the messages at equal or higher than the
-- matched level. if not found, log only LevelWarn or higher. You can override the default with a zero length prefix, ie `"" := LevelDebug`
Expand Down Expand Up @@ -224,7 +242,9 @@ example f = do
[ LoggingConfig (RhyoliteLogAppender_Stderr $ RhyoliteLogAppenderStderr Nothing) Nothing
, LoggingConfig (RhyoliteLogAppender_File $ RhyoliteLogAppenderFile "/dev/null") Nothing
, LoggingConfig (RhyoliteLogAppender_Stderr $ RhyoliteLogAppenderStderr Nothing) (Just $ M.fromList [("context",RhyoliteLogLevel_Debug)])
#ifdef linux_HOST_OS
, LoggingConfig (RhyoliteLogAppender_Journald $ RhyoliteLogAppenderJournald "foo") Nothing
#endif
]
withLogging @ RhyoliteLogAppender [def] $ do
$(logError) "Err"
Expand Down
4 changes: 3 additions & 1 deletion backend/rhyolite-backend.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ library
, HaskellNet-SSL
, io-streams
, lens
, libsystemd-journal
, lifted-base
, mime-mail
, monad-control
Expand Down Expand Up @@ -60,6 +59,9 @@ library
, vector
, websockets
, websockets-snap
if os(linux)
build-depends:
libsystemd-journal

exposed-modules:
Rhyolite.Backend.Account
Expand Down

0 comments on commit 0fc22c7

Please sign in to comment.