aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-02 18:33:44 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-02 18:33:44 +0000
commit2421176661c38729d6a9bb5a0c399aeed8dfb0e7 (patch)
tree0e50d8790d4708e440860be5f1f1e7db7ca965da
parentfix crashes caused by code errors (diff)
downloadalive-2421176661c38729d6a9bb5a0c399aeed8dfb0e7.tar.gz
alive-2421176661c38729d6a9bb5a0c399aeed8dfb0e7.zip
add logger
-rw-r--r--copilot.moon20
-rw-r--r--init.moon3
-rw-r--r--logger.moon47
-rw-r--r--scope.moon10
4 files changed, 67 insertions, 13 deletions
diff --git a/copilot.moon b/copilot.moon
index cef664a..e609a24 100644
--- a/copilot.moon
+++ b/copilot.moon
@@ -20,10 +20,19 @@ class Copilot
error "not a file: #{@file}"
patch: =>
- root = assert (program\match slurp @file), "parse error"
- @registry\patch_root root
+ root = program\match slurp @file
+
+ if not root
+ L\error "parse error"
+ return
+
+ @registry\retag root
spit @file, root\stringify!
+ ok, err = pcall @registry\link
+ if not ok
+ L\error "error linking: #{err}"
+
tb = (msg) -> debug.traceback msg, 2
poll: =>
{ :mode, :modification } = (lfs.attributes @file) or {}
@@ -31,11 +40,8 @@ class Copilot
return
if @last_modification < modification
- print "---"
- ok, err = xpcall @patch, tb, @
- if not ok
- print "ERROR: #{err}"
-
+ L\log "#{@file} changed at #{modification}"
+ L\push @\patch
@last_modification = os.time!
:Copilot
diff --git a/init.moon b/init.moon
index bd54613..423bdda 100644
--- a/init.moon
+++ b/init.moon
@@ -1,8 +1,11 @@
-- run from CLI
import clock_gettime, nanosleep, CLOCK_MONOTONIC from require 'posix.time'
+import Logger from require 'logger'
import Registry from require 'registry'
import Copilot from require 'copilot'
+Logger.init!
+
delta = do
gettime = ->
spec = clock_gettime CLOCK_MONOTONIC
diff --git a/logger.moon b/logger.moon
new file mode 100644
index 0000000..9a4ca19
--- /dev/null
+++ b/logger.moon
@@ -0,0 +1,47 @@
+unpack or= table.unpack
+
+class Logger
+ levels = {
+ debug: 0
+ trace: 1
+ log: 2
+ warn: 3
+ error: 4
+ print: 5
+ }
+
+ mklog = (max_level) ->
+ new: (level='log') =>
+ @level = levels[level] or level
+ @prefix = ' '
+
+ for name, level in pairs levels
+ @[name] = (first, ...) =>
+ return unless @level <= level
+ where = debug.traceback nil, 2
+ where = where\match '^.*\n%s+([%w:/%.]+): '
+ print "[#{where}]#{@prefix}#{first}", ...
+
+ if level == levels.print
+ @push = (fn, ...) => fn ...
+
+ push: (fn, ...) =>
+ last = @prefix
+ @prefix ..= ' '
+
+ res = { pcall fn, ... }
+
+ @prefix = last
+
+ if ok = table.remove res, 1
+ unpack res
+ else
+ error unpack res
+
+ init: ->
+ export L
+ L = Logger!
+
+{
+ :Logger
+}
diff --git a/scope.moon b/scope.moon
index bba2db4..3033fe2 100644
--- a/scope.moon
+++ b/scope.moon
@@ -44,23 +44,21 @@ class Scope
new: (@node, @parent) =>
@values = {}
- log: (msg) => -- print msg .. " in #{@}"
-
set_raw: (key, val) => @values[key] = constify val, key
set: (key, val) =>
- @log "setting #{key} = #{val}"
+ L\trace "setting #{key} = #{val}"
@values[key] = val
get: (key, prefix='') =>
- @log "checking for #{key}"
+ L\debug "checking for #{key} in #{@}"
if val = @values[key]
- @log "found #{val}"
+ L\trace "found #{val} in #{@}"
return val
start, rest = key\match '^(.-)/(.*)'
if not start
- return @parent and @parent\get key
+ return @parent and L\push -> @parent\get key
scope = @get start
assert scope and scope.type == 'scope', "cant find '#{prefix}#{start}' for '#{prefix}#{key}'"