aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/logging.lux
blob: 1baf1eedbf04029831a7a7ace457c527b9684d32 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(.require
 [library
  [lux (.except with)
   [abstract
    [monad (.only Monad do)]]
   [control
    ["[0]" try (.only Try)]]
   [data
    [color
     ["[0]" terminal]]
    [text
     ["%" \\format]]]
   [world
    ["[0]" console (.only Console)]
    [time
     [instant (.only Instant)]]]]])

(type .public (Logger !)
  (Interface
   (is (-> Text
           (! (Try Any)))
       log)))

(with_template [<prefix> <name> <foreground>]
  [(def .public (<name> message it)
     (All (_ !)
       (-> Text (Logger !)
           (! (Try Any))))
     (of it log (terminal.with <foreground>
                  (%.format <prefix> message))))]

  ["[ERROR]   " error terminal.red_foreground]
  ["[WARNING] " warn terminal.yellow_foreground]
  ["[INFO]    " info terminal.white_foreground]
  ["[DEBUG]   " debug terminal.cyan_foreground]
  )

(def .public (with scope it)
  (All (_ !)
    (-> (-> Text Text)
        (-> (Logger !) (Logger !))))
  (implementation
   (def log
     (|>> scope
          (of it log)))))

(def .public (timed ! now it)
  (All (_ !)
    (-> (Monad !) (-> Any (! Instant))
        (-> (Logger !) (Logger !))))
  (implementation
   (def (log message)
     (do !
       [now (now [])]
       (of it log (%.format (terminal.with terminal.green_foreground
                              (%.format "[" (%.instant now) "]"))
                            " "
                            message))))))

(def .public (console it)
  (All (_ !)
    (-> (Console !)
        (Logger !)))
  (implementation
   (def (log message)
     (console.write_line message it))))