aboutsummaryrefslogtreecommitdiffstats
path: root/alv
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-04-24 09:26:29 +0000
committers-ol <s-ol@users.noreply.github.com>2020-04-24 10:38:23 +0000
commitf189c84d1174713c36d3f2c1d172e1ad76776b47 (patch)
tree4fd1b961ea9a9559563b3f088a6154e2240f69b2 /alv
parentlib/string: fix str/.. (diff)
downloadalive-f189c84d1174713c36d3f2c1d172e1ad76776b47.tar.gz
alive-f189c84d1174713c36d3f2c1d172e1ad76776b47.zip
better logging interface
Diffstat (limited to 'alv')
-rw-r--r--alv/copilot.moon5
-rw-r--r--alv/logger.moon34
-rw-r--r--alv/registry.moon2
3 files changed, 24 insertions, 17 deletions
diff --git a/alv/copilot.moon b/alv/copilot.moon
index e5868bb..9d573c0 100644
--- a/alv/copilot.moon
+++ b/alv/copilot.moon
@@ -22,7 +22,6 @@ class Copilot
-- @tparam string file name/path of the alive file to watch and execute
new: (file) =>
@registry = Registry!
- @eval_stream, @run_stream = io.stderr, io.stdout
@open file if file
open: (file) =>
@@ -43,6 +42,7 @@ class Copilot
@poll!
if @root
+ L\set_time 'run'
@registry\begin_tick!
ok, error = Error.try "updating", ->
@root\tick_io!
@@ -69,10 +69,9 @@ class Copilot
return
if @last_modification < modification
- L.stream = @eval_stream
+ L\set_time 'eval'
L\log "#{@file} changed at #{modification}"
@eval!
- L.stream = @run_stream
@last_modification = os.time!
{
diff --git a/alv/logger.moon b/alv/logger.moon
index 26126ac..4887d67 100644
--- a/alv/logger.moon
+++ b/alv/logger.moon
@@ -37,8 +37,19 @@ class Logger
else
error unpack res
- --- the stream to write to.
- -- @tfield file stream
+ --- set the output time (runtime/evaltime).
+ -- @tparam string time (`'eval'` or `'run'`)
+ set_time: (time) =>
+ @stream = switch time
+ when 'eval' then io.stderr
+ when 'run' then io.stdout
+ else error "invalid time '#{time}'"
+
+ --- write out a message (for internal use).
+ -- @tparam string message
+ put: (message) =>
+ @stream\write message, '\n'
+ @stream\flush!
--- static functions
-- @section static
@@ -46,21 +57,19 @@ class Logger
--- create a new Logger.
-- @classmethod
-- @tparam string level the log-level to log at.
- -- @tparam file stream the stream to write to.
- new: (level='log', @stream=io.stdout) =>
+ new: (level='log') =>
@level = levels[level] or level
@prefix = ''
+ @set_time 'eval'
for name, level in pairs levels
@[name] = (msg) =>
return unless @level <= level
-
- msg = tostring msg
- @stream\write @prefix, msg, '\n'
- if level == levels.error or @level == levels.debug
+ @put if level == levels.error or @level == levels.debug
where = debug.traceback '', 2
- @stream\write where, '\n'
- @stream\flush!
+ "#{msg}\n#{where}"
+ else
+ tostring msg
if level == levels.print
@push = (fn, ...) => fn ...
@@ -78,9 +87,8 @@ class Logger
-- - `'silent'`
--
-- @tparam ?string level the level to initialize the logger at.
- -- @tparam ?file stream the output stream (stdout).
- @init: (...) ->
- L = Logger ...
+ @init: (...) =>
+ L = @ ...
{
:Logger
diff --git a/alv/registry.moon b/alv/registry.moon
index a39a1d7..21f57aa 100644
--- a/alv/registry.moon
+++ b/alv/registry.moon
@@ -113,7 +113,7 @@ class Registry
--- get the active Registry.
--
- -- Raises an erro when there is no active Regsitry.
+ -- Raises an error when there is no active Registry.
--
-- @treturn Registry
@active: -> assert Registry.active_registry, "no active Registry!"