1-- Copyright (c) 2019 The DAML Authors. All rights reserved.
2-- SPDX-License-Identifier: Apache-2.0
3
4{-# LANGUAGE RankNTypes #-}
5-- | This is a compatibility module that abstracts over the
6-- concrete choice of logging framework so users can plug in whatever
7-- framework they want to.
8module Development.IDE.Types.Logger
9  ( Priority(..)
10  , Logger(..)
11  , logError, logWarning, logInfo, logDebug, logTelemetry
12  , noLogging
13  ) where
14
15import qualified Data.Text as T
16
17
18data Priority
19-- Don't change the ordering of this type or you will mess up the Ord
20-- instance
21    = Telemetry -- ^ Events that are useful for gathering user metrics.
22    | Debug -- ^ Verbose debug logging.
23    | Info  -- ^ Useful information in case an error has to be understood.
24    | Warning
25      -- ^ These error messages should not occur in a expected usage, and
26      -- should be investigated.
27    | Error -- ^ Such log messages must never occur in expected usage.
28    deriving (Eq, Show, Ord, Enum, Bounded)
29
30
31-- | Note that this is logging actions _of the program_, not of the user.
32--   You shouldn't call warning/error if the user has caused an error, only
33--   if our code has gone wrong and is itself erroneous (e.g. we threw an exception).
34data Logger = Logger {logPriority :: Priority -> T.Text -> IO ()}
35
36
37logError :: Logger -> T.Text -> IO ()
38logError x = logPriority x Error
39
40logWarning :: Logger -> T.Text -> IO ()
41logWarning x = logPriority x Warning
42
43logInfo :: Logger -> T.Text -> IO ()
44logInfo x = logPriority x Info
45
46logDebug :: Logger -> T.Text -> IO ()
47logDebug x = logPriority x Debug
48
49logTelemetry :: Logger -> T.Text -> IO ()
50logTelemetry x = logPriority x Telemetry
51
52
53noLogging :: Logger
54noLogging = Logger $ \_ _ -> return ()
55