aboutsummaryrefslogtreecommitdiff
path: root/src/Internal/Logger.hs
blob: f1960fb0c07b6e1f923f01c12c5f632c67fb40b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Internal.Logger where

import XMonad
import qualified XMonad.Util.ExtensibleState as XS
import System.IO

data LoggerState =
  LoggerState {
    logHandle :: Maybe Handle
  }

instance Read LoggerState where
  readsPrec i s = map (\(_, s) -> (LoggerState Nothing, s)) (readsPrec i s :: [((), String)])

instance Show LoggerState where
  show _ = show ()

instance ExtensionClass LoggerState where
  initialValue = LoggerState Nothing

logs :: String -> X ()
logs s = do
  LoggerState handle' <- XS.get

  handle <-
    case handle' of
      Nothing -> do
        handle <- io $ openFile "/tmp/xmonad.log" AppendMode
        XS.put $ LoggerState (Just handle)
        return handle

      Just h -> return h
  
  io $ do
    hPutStrLn handle s
    hFlush handle