journalctl-stream (empty) → 0.1.0.0
raw patch · 5 files changed
+156/−0 lines, 5 filesdep +aesondep +basedep +conduit
Dependencies added: aeson, base, conduit, process, text, time, unix
Files
- changelog.md +2/−0
- journalctl-stream.cabal +20/−0
- lib/Systemd/Journalctl/Stream.hs +110/−0
- license +21/−0
- readme.md +3/−0
+ changelog.md view
@@ -0,0 +1,2 @@+## 0.1.0.0+* First release.
+ journalctl-stream.cabal view
@@ -0,0 +1,20 @@+name: journalctl-stream+category: System+synopsis: Stream logs using journalctl.+description: Stream logs using journalctl. Check the documentation for more details.+version: 0.1.0.0+author: Daniel Casanueva (daniel.casanueva `at` proton.me)+maintainer: Daniel Casanueva (daniel.casanueva `at` proton.me)+license: MIT+license-file: license+extra-source-files: readme.md, changelog.md+build-type: Simple+cabal-version: >= 1.10++library+ hs-source-dirs: lib+ default-language: Haskell2010+ default-extensions: ImportQualifiedPost, OverloadedStrings+ ghc-options: -Wall+ build-depends: base < 4.18, aeson, conduit, process, unix, text, time+ exposed-modules: Systemd.Journalctl.Stream
+ lib/Systemd/Journalctl/Stream.hs view
@@ -0,0 +1,110 @@+----------------------------------------------------------------------------------------------------++-- | Streaming interface to journalctl. Use 'entryStream' to stream+-- journalctl entries as they are created.+--+-- Designed with qualified import in mind.+-- For example, if you import it as @Journal@, then 'Entry' becomes+-- @Journal.Entry@, and 'Exception' becomes @Journal.Exception@.+--+module Systemd.Journalctl.Stream (+ -- * Journal entry+ Entry (..)+ , Cursor+ -- * Streaming+ , entryStream+ -- * Exceptions+ , Exception+ ) where++-- base+import System.IO (Handle)+import Data.Maybe (fromJust)+import Control.Exception qualified as Base+-- text+import Data.Text (Text)+import Data.Text.Encoding (encodeUtf8)+-- unix+import System.Posix.Types (CPid (..), ProcessID)+-- aeson+import Data.Aeson (FromJSON, (.:), (.:?))+import Data.Aeson qualified as JSON+-- time+import Data.Time.Clock (secondsToNominalDiffTime)+import Data.Time.Clock.POSIX (POSIXTime)+-- process+import System.Process qualified as System+-- conduit+import Conduit (MonadResource, MonadThrow, throwM)+import Data.Conduit (ConduitT, (.|))+import Data.Conduit.Combinators qualified as Conduit++-- | A cursor is an opaque text string that uniquely describes+-- the position of an entry in the journal and is portable+-- across machines, platforms and journal files.+newtype Cursor = Cursor Text deriving (Eq, Show)++instance FromJSON Cursor where+ parseJSON = JSON.withText "Cursor" $ pure . Cursor++-- | A journal entry.+data Entry = Entry+ { -- | Process ID.+ entryPID :: ProcessID+ -- | The name of the originating host.+ , entryHostname :: Text+ -- | The cursor for the entry.+ , entryCursor :: Cursor+ -- | The time the entry was received by the journal.+ , entryTimestamp :: POSIXTime+ -- | Unit name, if present.+ , entryUnit :: Maybe Text+ -- | Entry message.+ , entryMessage :: Text+ } deriving Show++newtype AsText a = AsText { asText :: a } deriving Show++instance FromJSON a => FromJSON (AsText a) where+ parseJSON = JSON.withText "AsText" $+ either fail (pure . AsText) . JSON.eitherDecodeStrict . encodeUtf8++instance FromJSON Entry where+ parseJSON = JSON.withObject "Entry" $ \o -> Entry+ <$> (CPid . asText <$> o .: "_PID")+ <*> o .: "_HOSTNAME"+ <*> o .: "__CURSOR"+ <*> (secondsToNominalDiffTime . (/1000000) . asText <$> o .: "__REALTIME_TIMESTAMP")+ <*> o .:? "UNIT"+ <*> o .: "MESSAGE"++-- | Exception raised while streaming entries from journalctl.+data Exception = JSONError String deriving Show++instance Base.Exception Exception where++-- | Stream of journal entries.+entryStream+ :: (MonadResource m, MonadThrow m)+ => Maybe String -- ^ Filter by unit name.+ -> Int -- ^ Number of previous messages to stream.+ -> ConduitT i Entry m ()+entryStream munit n =+ let args :: [String]+ args =+ [ "--follow"+ , "--lines"+ , show n+ , "--output"+ , "json"+ ] +++ (maybe [] (\unit -> ["--unit", unit]) munit)+ hdl :: IO Handle+ hdl = fmap (\(_, h, _, _) -> fromJust h)+ $ System.createProcess+ $ (System.proc "journalctl" args)+ { System.std_out = System.CreatePipe+ }+ in Conduit.sourceIOHandle hdl+ .| Conduit.linesUnboundedAscii+ .| Conduit.mapM (either (throwM . JSONError) pure . JSON.eitherDecodeStrict)
+ license view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2023 Daniel Díaz Casanueva++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ readme.md view
@@ -0,0 +1,3 @@+# journalctl-stream++Simple library for Haskell that calls `journalctl` and streams the results.