aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/logging.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/world/logging.lux')
-rw-r--r--stdlib/source/library/lux/world/logging.lux66
1 files changed, 66 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/world/logging.lux b/stdlib/source/library/lux/world/logging.lux
new file mode 100644
index 000000000..6f58fd134
--- /dev/null
+++ b/stdlib/source/library/lux/world/logging.lux
@@ -0,0 +1,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))))
+ (at 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
+ (at it log)))))
+
+(def .public (timed ! now it)
+ (All (_ !)
+ (-> (Monad !) (-> Any (! Instant))
+ (-> (Logger !) (Logger !))))
+ (implementation
+ (def (log message)
+ (do !
+ [now (now [])]
+ (at 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))))